python,argparse,python-2.x,optparse
Based on discussions that I've seen on the Python bug issue site, optparse is not really deprecated. Don't expect further development, but it isn't going to disappear any time soon. All of argparse is contained in one file, argparse.py. So you could grab that from almost anywhere, and put it...
python,command-line,command-line-arguments,argparse,optparse
Using argparse subcommand parsers p = argparse.ArgumentParser() subparsers = p.add_subparsers() option1_parser = subparsers.add_parser('option1') # Add specific options for option1 here, but here's # an example option1_parser.add_argument('param1') option1_parser.set_defaults(func=do_option1) option2_parser = subparsers.add_parser('option2') # Add specific options for option1 here option2_parser.set_defaults(func=do_option2) option3_parser = subparsers.add_parser('option3') # Add specific options for option3 here...
Yes, it seems that long options are case-insensitive. This is by convention, I imagine. Never seen a tool with case-sensitive long names. See the source: https://github.com/ruby/ruby/blob/b4974e71dcb32d430d7d686c5de247218991ec6c/lib/optparse.rb#L1408 You can copy and modify source of OptionParser, but you probably should not do this. :)...
Add this before you define your OptionParser: ARGV << '-h' if ARGV.empty? ...
The type parameter has to be an actual type, not the name of a type. parser.add_argument('count', action='store', type=int) Note that I removed the " around int....
python,python-2.7,python-3.x,optparse
Are you, by any chance, trying to accept a variable number of values for this option? That is, use the 'rawinput' to set nargs, which is then used to parse the command line? The optparse documentation has an example of using a custom callback to handle a variable number of...
python,linux,terminal,optparse,optionparser
Might I suggest argparse? I'm not sure if this is supported in OptionParser, but I would suggest using a triple quote i.e: parser = OptionParser() parser.add_option('--s', dest='s' type='string' help=''' With triple quotes I can directly put in anything including line spaces. \n will appear as a string rather than a...
python,python-2.7,argparse,optparse
In optparse, choice type is just a special form of string. From the optparse documenattion: "choice" options are a subtype of "string" options. The choices option attribute (a sequence of strings) defines the set of allowed option arguments. You don't need to specify this as a type in argparse; all...
AFAIK optparse doesn't provide that value in the public API via the result of parse_args, but you don't need it. You can simply name the 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: ') for _...
The options can be combined as you want. The program run with -tb import optparse, sys parser = optparse.OptionParser(usage='python %prog -t -b -q', prog=sys.argv[0], ) parser.add_option('-t','--tt', action="store_true", help="Blah",dest="t") parser.add_option('-b','--bb', action="store_true", help="Blah",dest="b") parser.add_option('-q','--qq', action="store_true", help="Blah",dest="q") options, args = parser.parse_args() print options produces {'q': None, 'b': True, 't': True} ...
If it's solely for displaying the options, one way I can think of is use the format_help of optparse and put it in the epilog of argparse, for example: In [303]: foo = OptionParser() In [304]: foo.add_option("-f", "--file", dest="filename",help="read data from FILENAME") In [305]: foo.add_option("-v", "--verbose",action="store_true", dest="verbose") In [311]: bar...