Don't parse options after the last positional argument

后端 未结 3 469
野的像风
野的像风 2020-12-17 10:55

I\'m writing a wrapper around the ssh command line client. After the first positional argument that\'s part of command, all further options should

相关标签:
3条回答
  • 2020-12-17 11:27

    Another option is to use parse_known_args, which stops parsing when an unknown argument is encountered.

    0 讨论(0)
  • 2020-12-17 11:42

    I had the same problem. I found the solution on the argparse bug tracker: http://code.google.com/p/argparse/issues/detail?id=52

    The solution is simple: replace nargs='+' (or '*') with nargs=argparse.REMAINDER. This special value is not documented, but it does what you want.

    0 讨论(0)
  • 2020-12-17 11:42

    I think your best bet to start solving these issues is to try out -- after all your optional args. -- is a pseudo-arg that tells ArgumentParser that everything after is a positional argument. Docs are here

    As for prevent arguments from being parsed after a certain point, you can pass part of argv to parse_args. That combined with some introspection can be used to limit what is parsed.

    0 讨论(0)
提交回复
热议问题