I am working on one requirement for my project using command line utility:optparse.
Suppose if I am using add_option utility like below:
parser.add_option('-c','--categories', dest='Categories', nargs=4 )
I wanted to add check
for -c
option if user does not input 4 arguments.
something like this:
if options.Categories is None:
for loop_iterate on nargs:
options.Categories[loop_iterate] = raw_input('Enter Input')
How to access nargs of add_option().?
PS:I do not want to have check using print.help()
and do exit(-1)
Please somebody help.
AFAIK
optparse
doesn't provide that value in the public API via the result ofparse_args
, but you don't need it. You can simply name the constant before using it:In fact the
add_option
method returns theOption
object which does have thenargs
field, so you could do:However I really don't see how this is better than using a costant in the first place.