Bash 脚本中的可选参数
我正在尝试编写一个具有可选参数的函数,例如,可选参数后面跟着类似 -x
的内容,
my_function man_arg1 man_arg2 -n opt_arg1 -o opt_arg2 -x opt_arg3
我希望它也支持像
my_function man_arg1 man_arg2 -x opt_arg
在我看到的其他问题中 这样的调用人们建议使用 getopts 但在这些答案中似乎您必须在调用函数时指定所有可选参数?另外,我似乎仍然不清楚如何使用 getopts 来实现这一点。
I am trying to write a function that has optional arguments in a way such that the optional arguments are follow by something like -x
, for example
my_function man_arg1 man_arg2 -n opt_arg1 -o opt_arg2 -x opt_arg3
and I want this to also support callings like
my_function man_arg1 man_arg2 -x opt_arg
In other questions I see people suggesting using getopts but in those answers it seems like you have to specify all the optional arguments when calling the function? Also it still seems unclear to me how you would use getopts for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否正确理解了这个问题,抱歉,如果我不回答它...
您可以使用(例如)
getopt(1)
,如下所示,这将允许- x
选项位于任何地方。请注意,可选参数 (
man_arg*
) 也可以位于任何位置。示例:
编辑:按照问题的要求将代码重写为函数。
I am not sure I understood the question correctly, sorry if I don't answer it...
You could use (for example)
getopt(1)
, as below, which will allow-x
option to be anywhere.Please note that optional arguments (
man_arg*
) can also be anywhere.Examples:
EDIT: Rewrote the code into a function, as asked in question.
POSIX 约定是命令的非选项参数必须位于所有选项和选项参数之后。 POSIX
getopts
实用程序是围绕此约定设计的,因此如果您不想坚持约定的参数顺序,那么getopts
不太适合。在这种情况下,也许仍然可以使 getopts 工作,但我不会尝试这样做。如果您愿意依赖 GNU 版本的
getopt
实用程序(不要与getopts
混淆,带有s
),那么它就可以满足您的需求。默认情况下,它识别命令行上任何位置的选项,包括非选项参数之后的选项。可能看起来像这样:但请注意,您可以在没有
getopt
的情况下执行相同的操作。getopt
(或getopts
) 为您做的是:-abc
相当于-a -b -c< /code>; 仅适用于不需要参数的选项)
x
时,-xfoo
相当于-x foo
接受论证)--
用于指示仅跟随非选项参数getopt仅限
)如果您对其中任何一个都不感兴趣,或者您愿意自己开发您想要的那些,或者如果您不能依赖 GNU 版本的 < code>getopt 那么你可以使用类似于上述操作无需首先使用
getopt
来调整参数列表,也不涉及getopts
。POSIX convention is that non-option arguments to a command must come after all options and option arguments. The POSIX
getopts
utility is designed around this convention, so if you don't want to insist on conventional argument order thengetopts
is not a good fit. It might still be possible to makegetopts
work in that case, but I wouldn't try to do so.If you are willing to depend on the GNU version of the
getopt
utility (not to be confused withgetopts
, with ans
), then it can accommodate you. By default, it recognizes options anywhere on the command line, including after non-option arguments. That might look like this:But note that you can do much the same without
getopt
. Whatgetopt
(orgetopts
) does for you is:-abc
equivalent to-a -b -c
; only for options that do not require arguments)-xfoo
equivalent to-x foo
when optionx
takes an argument)--
for indicating that only non-option arguments followgetopt
only)If you're not interested in any of that, or if you're willing to roll your own for the ones of those that you want, or if you can't rely on having the GNU version of
getopt
then you can use a loop similar to the above without first usinggetopt
to massage the argument list, and without involvinggetopts
.