2
在我的脚本中我尝试包装bazaar可执行文件。当我阅读某些为bzr而设计的选项时,我的脚本会对此作出反应。在任何情况下,所有的参数都会被赋予bzr可执行文件。当然,我不想指定bzr可以在 我的脚本中处理的所有参数。Python argpase:处理未知数量的参数/选项/ etc
那么,有没有办法用argpase处理未知数量的参数?
我的代码目前看起来是这样的:
parser = argparse.ArgumentParser(help='vcs')
subparsers = parser.add_subparsers(help='commands')
vcs = subparsers.add_parser('vcs', help='control the vcs',
epilog='all other arguments are directly passed to bzr')
vcs_main = vcs.add_subparsers(help='vcs commands')
vcs_commit = vcs_main.add_parser('commit', help="""Commit changes into a
new revision""")
vcs_commit.add_argument('bzr_cmd', action='store', nargs='+',
help='arugments meant for bzr')
vcs_checkout = vcs_main.add_parser('checkout',
help="""Create a new checkout of an existing branch""")
的NARGS选项让尽可能多的论据,我当然要。但不是另一个未知的可选参数(如--fixes或--unchanged)。
这似乎也有关: http://stackoverflow.com/questions/6488752/dont-parse-options-after-the-last-positional-argument – 2013-03-02 18:28:46