Many times I find myself write a cli that takes two/more positional arguments. Something like mycp file1 file2 [options] I have to write extra code everytime to show correct usage/hints to user if he invokes command this way mycp file1 Positional arguments are required ones unlike optional arguments.
The solution below lets cli writer add a positional argument so parser can generate usage friendlier to positional args.
Some inspiration: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/573441
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import sys
from optparse import OptionParser, Option, SUPPRESS_HELP
class PAOptionParser(OptionParser, object):
def __init__(self, *args, **kw):
self.posargs = []
super(PAOptionParser, self).__init__(*args, **kw)
def add_posarg(self, *args, **kw):
pa_help = kw.get("help", "")
kw["help"] = SUPPRESS_HELP
o = self.add_option("--%s" % args[0], *args[1:], **kw)
self.posargs.append((args[0], pa_help))
def get_usage(self, *args, **kwargs):
self.usage = "%%prog %s [options]\n\nPositional Arguments:\n %s" % \
(' '.join(["<%s>" % arg[0] for arg in self.posargs]), '\n '.join(["%s: %s" % (arg) for arg in self.posargs]))
return super(self.__class__, self).get_usage(*args, **kwargs)
def parse_args(self, *args, **kwargs):
args = sys.argv[1:]
args0 = []
for p, v in zip(self.posargs, args):
args0.append("--%s" % p[0])
args0.append(v)
args = args0 + args
options, args = super(self.__class__, self).parse_args(args, **kwargs)
if len(args) < len(self.posargs):
msg = 'Missing value(s) for "%s"\n' % ", ".join([arg[0] for arg in self.posargs][len(args):])
self.error(msg)
return options, args
if __name__ == '__main__':
#parser = PAOptionParser("My usage str")
parser = PAOptionParser()
parser.add_posarg("Foo", help="Foo usage")
parser.add_posarg("Bar", dest="bar_dest")
parser.add_posarg("Language", dest='tr_type', type="choice", choices=("Python", "Other"))
parser.add_option('--stocksym', dest='symbol')
values, args = parser.parse_args()
print values, args
# python mycp.py -h
# python mycp.py
# python mycp.py foo
# python mycp.py foo bar
#
# python mycp.py foo bar lava
# Usage: pa.py <Foo> <Bar> <Language> [options]
# Positional Arguments:
# Foo: Foo usage
# Bar:
# Language:
#
# pa.py: error: option --Language: invalid choice: 'lava' (choose from 'Python', 'Other')
|
If you want full support for positional arguments, take a look at argparse (http://argparse.python-hosting.com/), where your code would look something like:
You should avoid using super(self.__class__, self) for calling parent class method.
example:
output:
If you replace first parameter of super with self.__class__ then you will get
Yes, as you point out using
self
this way isn't safe. Agree. Even though self.__class__ is effectively same as D (or B or A for that matter). Have any explanation to that?On side note, if we take multiple inheritance out this works well. Like D inheriting 'C and A' and C inherits A, is brain exploding.
While I have modified recipe as per Vijayendra's solution, as Steven mentions argparse is definitely a better solution. I have recently used it :)