python - how to access nargs of optparse-add_action? -
python - how to access nargs of optparse-add_action? -
i working on 1 requirement project using command line utility:optparse.
suppose if using add_option utility below:
parser.add_option('-c','--categories', dest='categories', nargs=4 ) i wanted add together check -c alternative if user not input 4 arguments.
something this:
if options.categories none: loop_iterate on nargs: options.categories[loop_iterate] = raw_input('enter input') how access nargs of add_option().?
ps:i not want have check using print.help() , exit(-1)
please help.
afaik optparse doesn't provide value in public api via result of parse_args, don't need it. can name constant before using it:
num_categories = 4 # ... parser.add_option('-c', '--categories', dest='categories', nargs=num_categories) # later if not options.categories: options.categories = [raw_input('enter input: ') _ in range(num_categories)] in fact add_option method returns option object have nargs field, do:
categories_opt = parser.add_option(..., nargs=4) # ... if not options.categories: options.categories = [raw_input('enter input: ') _ in range(categories_opt.nargs)] however don't see how improve using costant in first place.
python python-2.7 optparse
Comments
Post a Comment