Python argparse: Is there a way to specify a range in nargs?

You could do this with a custom action: import argparse def required_length(nmin,nmax): class RequiredLength(argparse.Action): def __call__(self, parser, args, values, option_string=None): if not nmin<=len(values)<=nmax: msg=’argument “{f}” requires between {nmin} and {nmax} arguments’.format( f=self.dest,nmin=nmin,nmax=nmax) raise argparse.ArgumentTypeError(msg) setattr(args, self.dest, values) return RequiredLength parser=argparse.ArgumentParser(prog=’PROG’) parser.add_argument(‘-f’, nargs=”+”, action=required_length(2,3)) args=parser.parse_args(‘-f 1 2 3’.split()) print(args.f) # [‘1’, ‘2’, ‘3’] try: args=parser.parse_args(‘-f 1 … Read more

python argparse – either both optional arguments or else neither one

I believe that the best way to handle this is to post-process the returned namespace. The reason that argparse doesn’t support this is because it parses arguments 1 at a time. It’s easy for argparse to check to see if something was already parsed (which is why mutually-exclusive arguments work), but it isn’t easy to … Read more

argparse with required subcommands

There was a change in 3.3 in the error message for required arguments, and subcommands got lost in the dust. http://bugs.python.org/issue9253#msg186387 There I suggest this work around, setting the required attribute after the subparsers is defined. parser = ArgumentParser(prog=’test’) subparsers = parser.add_subparsers() subparsers.required = True subparsers.dest=”command” subparser = subparsers.add_parser(“foo”, help=”run foo”) parser.parse_args() update A related … Read more

Arguments that are dependent on other arguments with Argparse

You can use subparsers in argparse import argparse parser = argparse.ArgumentParser(prog=’PROG’) parser.add_argument(‘–foo’, required=True, help=’foo help’) subparsers = parser.add_subparsers(help=’sub-command help’) # create the parser for the “bar” command parser_a = subparsers.add_parser(‘bar’, help=’a help’) parser_a.add_argument(‘bar’, type=int, help=’bar help’) print(parser.parse_args())

In Python argparse, is it possible to have paired –no-something/–something arguments?

Well, none of the answers so far are quite satisfactory for a variety of reasons. So here is my own answer: class ActionNoYes(argparse.Action): def __init__(self, opt_name, dest, default=True, required=False, help=None): super(ActionNoYes, self).__init__([‘–‘ + opt_name, ‘–no-‘ + opt_name], dest, nargs=0, const=None, default=default, required=required, help=help) def __call__(self, parser, namespace, values, option_string=None): if option_string.starts_with(‘–no-‘): setattr(namespace, self.dest, False) else: … Read more

Is it possible to use argparse to capture an arbitrary set of optional arguments?

This is kind of a hackish way, but it works well: Check, which arguments are not added and add them import argparse parser = argparse.ArgumentParser() parser.add_argument(“foo”) parser.add_argument(“-bar”, type=int) # parser can have any arguments, whatever you want! parsed, unknown = parser.parse_known_args() # this is an ‘internal’ method # which returns ‘parsed’, the same as what … Read more