如何使用Python ArgeParse模块添加可选的位置参数组?
我正在尝试实现参数声明,就像 gnu超时实用程序:
超时[option] duration命令[arg]…
在此处,option
可以是任何数量的 option gragonments ,您应该在持续时间
和命令
之前将它们传递给,它们是可选的。如何使用Python argparse
来实现此目标?
帮助输出:
[user@desktop ~]# timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
or: timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.
Mandatory arguments to long options are mandatory for short options too.
--preserve-status
exit with the same status as COMMAND, even when the
command times out
--foreground
when not running timeout directly from a shell prompt,
allow COMMAND to read from the TTY and get TTY signals;
in this mode, children of COMMAND will not be timed out
-k, --kill-after=DURATION
also send a KILL signal if COMMAND is still running
this long after the initial signal was sent
-s, --signal=SIGNAL
specify the signal to be sent on timeout;
SIGNAL may be a name like 'HUP' or a number;
see 'kill -l' for a list of signals
-v, --verbose diagnose to stderr any signal sent upon timeout
--help display this help and exit
--version output version information and exit
I'm trying to achieve argument declaration just like the GNU timeout utility:
timeout [option] duration command [arg]…
Here, the option
could be any number of option arguments, you should pass them before duration
and command
, and they are optional. How you can achieve this using Python argparse
?
Help output:
[user@desktop ~]# timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
or: timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.
Mandatory arguments to long options are mandatory for short options too.
--preserve-status
exit with the same status as COMMAND, even when the
command times out
--foreground
when not running timeout directly from a shell prompt,
allow COMMAND to read from the TTY and get TTY signals;
in this mode, children of COMMAND will not be timed out
-k, --kill-after=DURATION
also send a KILL signal if COMMAND is still running
this long after the initial signal was sent
-s, --signal=SIGNAL
specify the signal to be sent on timeout;
SIGNAL may be a name like 'HUP' or a number;
see 'kill -l' for a list of signals
-v, --verbose diagnose to stderr any signal sent upon timeout
--help display this help and exit
--version output version information and exit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论