Python argparse optional sub-arguments

后端 未结 4 1609
温柔的废话
温柔的废话 2020-12-17 17:09

I\'d like to have an argument to my program that has some required parameters along with some optional parameters. Something like this:

[--print text [color          


        
相关标签:
4条回答
  • 2020-12-17 17:51

    According to Devin Jeanpierre's answer, it seems that using '+' (one or more) instead of '*' would do what you are trying to achieve. (PS: I would've just commented in his answer if I had enough points)

    0 讨论(0)
  • 2020-12-17 18:06

    that will work for single arg:

    parser.add_argument('--write_google', nargs='?', const='Yes',
                        choices=['force', 'Yes'],
                        help="write local changes to google")
    
    0 讨论(0)
  • 2020-12-17 18:08

    Reading the source code (start in take_action), I believe what you want is impossible. All argument parsing and passing to actions is done based on nargs, and nargs is either a number, OPTIONAL ("?"), ZERO_OR_MORE ("*"), ONE_OR_MORE ("+"), PARSER, or REMAINDER. This must be determined before the Action object (which handles the input) even sees what it's getting, so it can't dynamically figure out nargs.

    I think you'll need to live with a workaround. I would maybe have --foo-x x, --foo-y y, and --foo-z z, and perhaps also --foo x y z.

    0 讨论(0)
  • 2020-12-17 18:09

    How about

    def printText(args):
      print args
    
    parser = argparse.ArgumentParser()
    subparser = parser.add_subparsers()
    printer = subparser.add_parser('print')
    printer.add_argument('text')
    printer.add_argument('color', nargs='?')
    printer.add_argument('size', type=int, nargs='?')
    printer.set_defaults(func=printText)
    
    cmd = parser.parse_args()
    cmd.func(cmd)
    

    Then you get something like this:

    $ ./test.py -h
    usage: test.py [-h] {print} ...
    
    positional arguments:
      {print}
    
    $ ./test.py print -h
    usage: test.py print [-h] text [color] [size]
    
    positional arguments:
      text
      color
      size
    
    $ ./test.py print text
    Namespace(color=None, func=<function printText at 0x2a96150b90>, size=None, text='text')
    
    $ ./test.py print text red
    Namespace(color='red', func=<function printText at 0x2a96150b90>, size=None, text='text')
    
    $ ./test.py print text red 12
    Namespace(color='red', func=<function printText at 0x2a96150b90>, size=12, text='text')
    
    0 讨论(0)
提交回复
热议问题