Python 中使用 argparse 所需的命令行选项
我有以下命令行参数场景。如果有一个特定的选项,那么应该还有一些其他必需的选项。例如,如果有 --create 那么应该有 --name。另外,如果有 --remove 那么应该有 --id。是否可以用argparse来实现这个场景?或者其他什么东西?
I have the following scenario for command line argument. If there is a particular option then there should be some other required options. For example if there is -- create then there should be --name. Also if there is --remove then there should be --id. Is it possible to implement this scenario with argparse? or someother thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要您不介意
create子命令来完成此操作code> 和
remove
前面没有连字符。无论如何,这可能是有道理的,因为这些动词经常被用作动作而不是选项。This can be done with subcommands as long as you don't mind
create
andremove
not being preceded with hyphens. This may make sense anyway, since those verbs are often used as actions rather than options.可选是隐式的,必须指定必需的:
http://docs.python.org/library/ argparse.html#required
也就是说,似乎没有一个用于参数“依赖项”的内置机制,正如我认为您想要实现的那样。这将是您的应用程序的要求。
Optional is implicit, required must be specified:
http://docs.python.org/library/argparse.html#required
That said, there doesn't appear to be a built-in mechanism for argument "dependencies", as I think you would like to implement. This would be a requirement for your application.