如何解析 Bash 中的命令行参数?
比方说,我有一个用这一行调用的脚本:
./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile
或这一行:
./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile
可接受的解析方式是什么,以便在每种情况下(或两者的某种组合) $v
, $f
和 $d
将全部设置为 true
并且 $outFile
将等于 /fizz/一些其他文件?
Say, I have a script that gets called with this line:
./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile
or this one:
./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile
What's the accepted way of parsing this such that in each case (or some combination of the two) $v
, $f
, and $d
will all be set to true
and $outFile
will be equal to /fizz/someOtherFile
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
Bash 空格分隔(例如,
--option argument
)复制粘贴上面的块的输出
使用
Bash Equals-Separated(例如,
--option=argument
)输出复制粘贴上面的块
用法
为了更好地理解
${i#*=}
在 本指南。它在功能上等同于`sed 's/[^=]*=//' <<< "$i"`
调用不必要的子进程或`echo "$i" | sed 's/[^=]*=//'`
调用两个不必要的子进程。将 bash 与 getopt[s]
getopt(1) 限制一起使用(较旧的、相对较新的
getopt
版本):较新的
getopt
版本没有这些限制。有关更多信息,请参阅这些文档。POSIX getopts
此外,POSIX shell 和其他 shell 提供的 getopts 没有这些限制。我提供了一个简单的
getopts
示例。复制粘贴上面的块的输出
用法
getopts
的优点是:dash
等其他 shell 中工作。-vf filename
。getopts
的缺点是它只能处理短选项(-h
,而不是--help
),而无需额外的代码。有一个 getopts 教程 解释了所有语法和变量的含义。在 bash 中,还有
help getopts
,它可能提供信息。Bash Space-Separated (e.g.,
--option argument
)Output from copy-pasting the block above
Usage
Bash Equals-Separated (e.g.,
--option=argument
)Output from copy-pasting the block above
Usage
To better understand
${i#*=}
search for "Substring Removal" in this guide. It is functionally equivalent to`sed 's/[^=]*=//' <<< "$i"`
which calls a needless subprocess or`echo "$i" | sed 's/[^=]*=//'`
which calls two needless subprocesses.Using bash with getopt[s]
getopt(1) limitations (older, relatively-recent
getopt
versions):More recent
getopt
versions don't have these limitations. For more information, see these docs.POSIX getopts
Additionally, the POSIX shell and others offer
getopts
which doen't have these limitations. I've included a simplisticgetopts
example.Output from copy-pasting the block above
Usage
The advantages of
getopts
are:dash
.-vf filename
in the typical Unix way, automatically.The disadvantage of
getopts
is that it can only handle short options (-h
, not--help
) without additional code.There is a getopts tutorial which explains what all of the syntax and variables mean. In bash, there is also
help getopts
, which might be informative.无答案展示了增强的 getopt。 得票最高的答案具有误导性:它要么忽略
-vfd
样式短选项(OP 要求)或位置参数后的选项(OP 也要求);并且它忽略解析错误。相反:getopt
。1getopt_long 一起使用()
GNU glibc 的 C 函数。getopt
无法执行此操作)script.sh -o outFile file1 file2 -v
(getopts
不执行此操作)=
样式的长选项:script.sh --outfile=fileOut --infile fileIn
(如果自解析,则允许两者都很长)-vfd
(如果自解析则真正有效)-oOutfile
或-vfdoOutfile
getopt --test
→ 返回值 4.getopt
或 shell 内置的getopts
的用途有限。以下调用
均返回
以下
myscript
1 增强的 getopt 在大多数“bash 系统”上可用,包括 Cygwin;在 OS X 上尝试
brew install gnu-getopt
,brew install util-linux
或sudo port install getopt
2 POSIX
exec()
约定没有可靠的方法在命令行参数中传递二进制 NULL;这些字节过早地结束了争论3 第一个版本发布于1997年或更早(我只追溯到1997年)
No answer showcases enhanced getopt. And the top-voted answer is misleading: It either ignores
-vfd
style short options (requested by the OP) or options after positional arguments (also requested by the OP); and it ignores parsing-errors. Instead:getopt
from util-linux or formerly GNU glibc.1getopt_long()
the C function of GNU glibc.getopt
can’t do this)script.sh -o outFile file1 file2 -v
(getopts
doesn’t do this)=
-style long options:script.sh --outfile=fileOut --infile fileIn
(allowing both is lengthy if self parsing)-vfd
(real work if self parsing)-oOutfile
or-vfdoOutfile
getopt --test
→ return value 4.getopt
or shell-builtingetopts
are of limited use.The following calls
all return
with the following
myscript
1 enhanced getopt is available on most “bash-systems”, including Cygwin; on OS X try
brew install gnu-getopt
,brew install util-linux
orsudo port install getopt
2 the POSIX
exec()
conventions have no reliable way to pass binary NULL in command line arguments; those bytes prematurely end the argument3 first version released in 1997 or before (I only tracked it back to 1997)
deploy.sh
用法:
deploy.sh
Usage:
来自 digitalpeer.com 稍作修改:
用法
myscript.sh -p=my_prefix -s=dirname -l=libname
为了更好地理解
${i#*=}
搜索本指南中的“子字符串删除”。它在功能上等同于`sed 's/[^=]*=//' <<< "$i"`
调用不必要的子进程或`echo "$i" | sed 's/[^=]*=//'`
调用两个不必要的子进程。From digitalpeer.com with minor modifications:
Usage
myscript.sh -p=my_prefix -s=dirname -l=libname
To better understand
${i#*=}
search for "Substring Removal" in this guide. It is functionally equivalent to`sed 's/[^=]*=//' <<< "$i"`
which calls a needless subprocess or`echo "$i" | sed 's/[^=]*=//'`
which calls two needless subprocesses.这个解决方案:
-n arg
和--name=arg
This solution:
-n arg
and--name=arg
getopt()
/getopts()
是一个不错的选择。从此处复制:getopt()
/getopts()
is a good option. Copied from here:我发现在脚本中编写可移植解析的问题非常令人沮丧,以至于我编写了 Argbash - 一个 FOSS 代码生成器,可以为您的脚本生成参数解析代码,此外它还有一些不错的功能:
https://argbash.dev
I have found the matter to write portable parsing in scripts so frustrating that I have written Argbash - a FOSS code generator that can generate the arguments-parsing code for your script plus it has some nice features:
https://argbash.dev
我使用之前的答案作为起点来整理我旧的临时参数解析。然后我重构了以下模板代码。它使用 = 或空格分隔参数来处理长参数和短参数,以及分组在一起的多个短参数。最后,它将所有非 param 参数重新插入到 $1,$2.. 变量中。
I used the earlier answers as a starting point to tidy up my old adhoc param parsing. I then refactored out the following template code. It handles both long and short params, using = or space separated arguments, as well as multiple short params grouped together. Finally it re-inserts any non-param arguments back into the $1,$2.. variables.
尽快:另一个 Shell 参数解析器
编辑说明:现在具有纯 POSIX shell 代码并且不含麸质!
TL;DR
该解析器仅使用 POSIX 兼容的 shell 代码来处理以下格式的选项:
-o [ARG]
、-abo [ARG]
、--opt [ARG]
或--opt=[ARG]
,其中ARG
是可选参数。它可以处理混合的选项和参数,还可以使用“--
”来强制将其后面的任何参数视为位置参数。这是一个最小版本,只要命令正确,它就可以工作,即它几乎不执行任何检查。您可以将其粘贴到 sh 脚本 - 它不会作为函数工作 - 并且替换您的选项定义。
示例输出
描述
我受到相对简单的@bronson的回答的启发,并试图尝试改进它(不增加太多的复杂性)。
此解析器实现使用模式匹配、参数扩展和 shell 自己的位置参数作为输出限制队列来循环和处理参数。结果如下:
-o [ARG]
、-abo [ARG]
、--long-option [ARG]
中的任何一个 and--long-option=[ARG]
选项样式被接受;$@
中;--
强制剩余参数被视为位置参数;getopt(s)
或外部实用程序;可移植性
此代码经过测试和验证,可以与以下版本的相当新的版本一起使用:
bash
、dash
、mksh
、ksh93
、yash
、zsh
和 BusyBox 的ash
(均使用其标准可执行路径调用,而不是作为/bin/sh
>)。如果您发现错误或它不适用于特定的 POSIX 兼容 shell,请发表评论。
PS: 我知道...与 <
0x01030307
的二进制值可能会破坏逻辑。但是,如果有人在命令行中传递二进制参数,这个问题应该是他们最后关心的问题。ASAP: Another Shell Argument Parser
Edit note: now with pure POSIX shell code and gluten free!
TL;DR
This parser uses only POSIX compliant shell code to process options in these formats:
-o [ARG]
,-abo [ARG]
,--opt [ARG]
or--opt=[ARG]
, whereARG
is an optional argument. It can handle intermixed options and arguments, and also "--
" to force any argument after it to be treated as positional.Here is a minimal version that works as long as the command is correct, i.e. it doesn't perform almost any checks. You can paste it at the top of your sh script —it won't work as a function— and substitute your option definitions.
Sample outputs
Description
I was inspired by the relatively simple answer by @bronson and tempted to try to improve it (without adding too much complexity).
This parser implementation uses pattern matching, parameter expansion and the shell's own positional parameters as an output-restricted queue to loop over and process arguments. Here's the result:
-o [ARG]
,-abo [ARG]
,--long-option [ARG]
and--long-option=[ARG]
styles of options are accepted;$@
after the loop;--
to force remaining arguments to be treated as positional;getopt(s)
or external utilities;Portability
This code was tested and verified to work with a reasonably recent version of:
bash
,dash
,mksh
,ksh93
,yash
,zsh
and BusyBox'sash
(all called with their standard executable paths, not as/bin/sh
).If you find a bug or that it doesn't work with a particular POSIX compatible shell, please leave a comment.
PS: I know... An argument with the binary value of
0x01030307
could break the logic. However, if anyone is passing around binary arguments in a command-line, this issue should be their last concern.此示例演示如何使用
getopt
和eval
以及HEREDOC
和shift
处理带或不带短参数和长参数随后的所需值。 switch/case 语句也简洁且易于理解。上面脚本中最重要的几行是:
简短、切题、可读,并且几乎可以处理所有内容(恕我直言)。
希望对某人有帮助。
This example shows how to use
getopt
andeval
andHEREDOC
andshift
to handle short and long parameters with and without a required value that follows. Also the switch/case statement is concise and easy to follow.The most significant lines of the script above are these:
Short, to the point, readable, and handles just about everything (IMHO).
Hope that helps someone.
这允许您同时拥有空格分隔的选项/值以及相等的定义值。
运行脚本
因此,您可以使用:以及:
,并且两者应该具有相同的最终结果。
优点:
允许 -arg=value 和 -arg value
适用于可在 bash 中使用的任何参数名称
纯 bash。无需学习/使用 getopt 或 getopts
缺点:
无法组合参数
This allows you to have both space separated options/values, as well as equal defined values.
So you could run your script using:
as well as:
and both should have the same end result.
PROS:
Allows for both -arg=value and -arg value
Works with any arg name that you can use in bash
Pure bash. No need to learn/use getopt or getopts
CONS:
Can't combine args
扩展@bruno-bronosky的答案,我添加了一个“预处理器”来处理一些常见的格式:
--longopt=val
扩展为--longopt val
- xyz
转换为-x -y -z
--
指示标志结束Expanding on @bruno-bronosky's answer, I added a "preprocessor" to handle some common formatting:
--longopt=val
into--longopt val
-xyz
into-x -y -z
--
to indicate the end of flags如果您正在制作可与其他实用程序互换的脚本,那么以下灵活性可能会很有用。
要么:
或者:
这是代码:
If you are making scripts that are interchangeable with other utilities, below flexibility may be useful.
Either:
Or:
Here is the code:
我认为这个使用起来很简单:
调用示例:
I think this one is simple enough to use:
Invocation example:
另一个选项解析器(生成器)
一个优雅的 shell 脚本选项解析器(完全支持所有 POSIX shell)
https://github.com/ko1nksm/getoptions (更新:v3.3.0 于 2021 年 5 月发布) 02)
getoptions 是一个用 POSIX 兼容的 shell 脚本编写的新选项解析器(生成器),于 2020 年 8 月发布。它适合那些想要支持的人shell 脚本中的 POSIX / GNU 风格选项语法。
支持的语法为
-a
、+a
、-abc
、-vvv
、-p VALUE
、-pVALUE
、--flag
、--no-flag
、--with-flag
>,--无标志
,--param VALUE
、--param=VALUE
、--option[=VALUE]
、--no-option
代码> <代码>--。它支持子命令、验证、缩写选项和自动帮助生成。并适用于所有 POSIX shell(dash 0.5.4+、bash 2.03+、ksh88+、mksh R28+、zsh 3.1.9+、yash 2.29+、busybox ash 1.1.3+ 等)。
它解析以下参数:
以及自动帮助生成。
它也是一个选项解析器生成器,生成以下简单的选项解析代码。如果您使用生成的代码,则不需要
getoptions
。 实现真正的可移植性和零依赖性。Yet another option parser (generator)
An elegant option parser for shell scripts (full support for all POSIX shells)
https://github.com/ko1nksm/getoptions (Update: v3.3.0 released on 2021-05-02)
getoptions is a new option parser (generator) written in POSIX-compliant shell script and released in august 2020. It is for those who want to support the POSIX / GNU style option syntax in your shell scripts.
The supported syntaxes are
-a
,+a
,-abc
,-vvv
,-p VALUE
,-pVALUE
,--flag
,--no-flag
,--with-flag
,--without-flag
,--param VALUE
,--param=VALUE
,--option[=VALUE]
,--no-option
--
.It supports subcommands, validation, abbreviated options, and automatic help generation. And works with all POSIX shells (dash 0.5.4+, bash 2.03+, ksh88+, mksh R28+, zsh 3.1.9+, yash 2.29+, busybox ash 1.1.3+, etc).
It's parses the following arguments:
And automatic help generation.
It is also an option parser generator, generates the following simple option parsing code. If you use the generated code, you won't need
getoptions
. Achieve true portability and zero dependency.如果 #1 您已经安装了 getopts 并且 #2 您打算在同一平台上运行它,那么 getopts 效果很好。 OSX 和 Linux(例如)在这方面的表现有所不同。
这是一个(非 getopts)解决方案,支持等于、不等于和布尔标志。例如,您可以通过以下方式运行脚本:
getopts works great if #1 you have it installed and #2 you intend to run it on the same platform. OSX and Linux (for example) behave differently in this respect.
Here is a (non getopts) solution that supports equals, non-equals, and boolean flags. For example you could run your script in this way:
我给你的函数
parse_params
将从命令行解析参数。--all
等于-all
等于all=all
)下面的脚本是复制粘贴工作示范。请参阅
show_use
函数以了解如何使用parse_params
。限制:
-d 1
)--any-param
和-anyparam
是等效的eval $(parse_params "$@")
必须在 bash function 内部使用(它在全局范围内不起作用)I give you The Function
parse_params
that will parse params from the command line.--all
equals-all
equalsall=all
)The script below is a copy-paste working demonstration. See
show_use
function to understand how to useparse_params
.Limitations:
-d 1
)--any-param
and-anyparam
are equivalenteval $(parse_params "$@")
must be used inside bash function (it will not work in the global scope)我想提交我的项目: https://github.com/flyingangel/argparser
就这么简单。环境将填充与参数同名的变量
I wanna submit my project : https://github.com/flyingangel/argparser
Simple as that. The environment will be populated with variables with the same name as the arguments
根据这里的其他答案,这是我的版本:
用法:
Based on other answers here, this my version:
Usage:
这就是我在函数中所做的,以避免破坏 getopts 在堆栈中较高位置同时运行:
This is how I do in a function to avoid breaking getopts run at the same time somewhere higher in stack:
我想提供我的选项解析版本,它允许以下操作:
还允许这样做(可能不需要):
您必须在使用之前决定是否在选项上使用 = 。这是为了保持代码干净。
I'd like to offer my version of option parsing, that allows for the following:
Also allows for this (could be unwanted):
You have to decide before use if = is to be used on an option or not. This is to keep the code clean(ish).
有多种方法可以解析cmdline args(例如GNU getopt(不可移植)与BSD(MacOS)getopt与getopts) - 都有问题。该解决方案
=
分隔符-vxf
--color
与--color=always
),--
表示选项结束,但示例:任何
There are several ways to parse cmdline args (e.g. GNU getopt (not portable) vs BSD (MacOS) getopt vs getopts) - all problematic. This solution
=
separator between option and argument-vxf
--color
vs--color=always
),--
to signal end of options, andExamples: Any of
保留未处理参数的解决方案。包括演示。
这是我的解决方案。它非常灵活,与其他程序不同,不需要外部包并干净地处理剩余参数。
用法是:
./myscript -flag flagvariable -otherflag flagvar2
您所要做的就是编辑 validflags 行。它在前面添加一个连字符并搜索所有参数。然后它将下一个参数定义为标志名称,例如
主代码(简短版本,详细的示例,下面还有一个错误输出的版本):
带有内置回显演示的详细版本:
最后一个,如果传递了 invalid 参数。
优点:它的作用非常好。它保留了未使用的参数,而这里的许多其他解决方案则没有。它还允许调用变量而无需在脚本中手动定义。如果没有给出相应的参数,它还允许预填充变量。 (参见详细示例)。
缺点:无法解析单个复杂的参数字符串,例如 -xcvf 将作为单个参数进行处理。不过,您可以轻松地在我的代码中编写额外的代码来添加此功能。
Solution that preserves unhandled arguments. Demos Included.
Here is my solution. It is VERY flexible and unlike others, shouldn't require external packages and handles leftover arguments cleanly.
Usage is:
./myscript -flag flagvariable -otherflag flagvar2
All you have to do is edit the validflags line. It prepends a hyphen and searches all arguments. It then defines the next argument as the flag name e.g.
The main code (short version, verbose with examples further down, also a version with erroring out):
The verbose version with built in echo demos:
Final one, this one errors out if an invalid -argument is passed through.
Pros: What it does, it handles very well. It preserves unused arguments which a lot of the other solutions here don't. It also allows for variables to be called without being defined by hand in the script. It also allows prepopulation of variables if no corresponding argument is given. (See verbose example).
Cons: Can't parse a single complex arg string e.g. -xcvf would process as a single argument. You could somewhat easily write additional code into mine that adds this functionality though.
这是我的方法 - 使用正则表达式。
-qwerty
-q -w -e
--qwerty
=
来提供属性,但属性匹配直到遇到连字符 + 空格“分隔符”,所以在--q=qwe ty
qwe ty
是一个属性,-oa -op attr ibute --option=att ribu te -- option attribute --option att-ribute
是有效的脚本:
Here is my approach - using regexp.
-qwerty
-q -w -e
--qwerty
=
to provide attributes, but attribute matches until encountering hyphen+space "delimiter", so in--q=qwe ty
qwe ty
is one attribute-o a -op attr ibute --option=att ribu te --op-tion attribute --option att-ribute
is validscript:
混合位置参数和基于标志的参数
--param=arg (等于分隔)
在位置参数之间自由混合标志:
可以通过相当简洁的方法来完成:
--param arg (空格分隔)
不混合
- 通常会更清楚-flag=value
和--flag value
样式。读起来有点冒险,但仍然
有效
Mixing positional and flag-based arguments
--param=arg (equals delimited)
Freely mixing flags between positional arguments:
can be accomplished with a fairly concise approach:
--param arg (space delimited)
It's usualy clearer to not mix
--flag=value
and--flag value
styles.This is a little dicey to read, but is still valid
Source
请注意,
getopt(1)
是 AT&T 的一个短暂错误。getopt 创建于 1984 年,但在 1986 年就被埋没了,因为它实际上并不可用。
getopt(1)
手册页仍然提到"$*"
而不是,这证明了
,它于 1986 年与内置的getopt
已经过时了。 >"$@"getopts(1)
shell 一起添加到 Bourne Shell,以便处理内部带有空格的参数。顺便说一句:如果您对解析 shell 脚本中的长选项感兴趣,可能有兴趣了解 libc (Solaris) 和 ksh93 的
getopt(3)
实现添加了统一的长选项实现,支持长选项作为短选项的别名。这会导致ksh93
和Bourne Shell
通过getopts
为长选项实现统一的接口。取自 Bourne Shell 手册页的长选项示例:
getopts "f:(file)(input-file)o:(output-file)" OPTX "$@"
显示选项别名的长度可以在 Bourne Shell 和 ksh93 中使用。
请参阅最近的 Bourne Shell 的手册页:
http://schillix.sourceforge。 net/man/man1/bosh.1.html
以及 OpenSolaris 中 getopt(3) 的手册页:
http://schillix.sourceforge.net/man/man3c/getopt.3c.html< /a>
最后是 getopt(1) 手册页,用于验证过时的 $*:
http://schillix.sourceforge.net/man/man1/getopt.1.html
Note that
getopt(1)
was a short living mistake from AT&T.getopt was created in 1984 but already buried in 1986 because it was not really usable.
A proof for the fact that
getopt
is very outdated is that thegetopt(1)
man page still mentions"$*"
instead of"$@"
, that was added to the Bourne Shell in 1986 together with thegetopts(1)
shell builtin in order to deal with arguments with spaces inside.BTW: if you are interested in parsing long options in shell scripts, it may be of interest to know that the
getopt(3)
implementation from libc (Solaris) andksh93
both added a uniform long option implementation that supports long options as aliases for short options. This causesksh93
and theBourne Shell
to implement a uniform interface for long options viagetopts
.An example for long options taken from the Bourne Shell man page:
getopts "f:(file)(input-file)o:(output-file)" OPTX "$@"
shows how long option aliases may be used in both Bourne Shell and ksh93.
See the man page of a recent Bourne Shell:
http://schillix.sourceforge.net/man/man1/bosh.1.html
and the man page for getopt(3) from OpenSolaris:
http://schillix.sourceforge.net/man/man3c/getopt.3c.html
and last, the getopt(1) man page to verify the outdated $*:
http://schillix.sourceforge.net/man/man1/getopt.1.html
我已经编写了一个 bash 助手来编写一个不错的 bash 工具
项目主页: https://gitlab.mbedsys.org /mbedsys/bashopts
示例:
将提供帮助:
享受:)
I have write a bash helper to write a nice bash tool
project home: https://gitlab.mbedsys.org/mbedsys/bashopts
example:
will give help:
enjoy :)
假设我们创建一个名为
test_args.sh
的 shell 脚本,如下所示运行以下命令后:
输出将是:
Assume we create a shell script named
test_args.sh
as followAfter we run the following command:
The output would be:
这是一个 getopts,它用最少的代码实现了解析,并允许您使用带子字符串的 eval 定义在一种情况下希望提取的内容。
基本上
eval "local key='val'"
将变量声明为局部变量,而不是此处大多数答案的全局变量。
称为:
${k:3} 基本上是一个子字符串,用于从键中删除第一个
---
。Here is a getopts that achieves the parsing with minimal code and allows you to define what you wish to extract in one case using eval with substring.
Basically
eval "local key='val'"
Declares the variables as locals instead of globals as most answers here.
Called as:
The ${k:3} is basically a substring to remove the first
---
from the key.我想分享我为解析选项所做的事情。
这里的答案没有满足我的一些需求,所以我不得不想出这个:https://github。 com/MihirLuthra/bash_option_parser
这支持:
假设我们有一个名为
fruit
的用法如下:-e
不带参数-c
需要两个参数,即如何剪切和为什么剪切fruit
本身至少有一个参数。
适用于apple
、orange
等子选项(类似于具有子选项git
>commit、push
等)因此要解析它:
现在如果有任何使用错误,可以使用
option_parser_error_msg
打印出来,如下所示:立即检查如果传递了一些选项,
也可以通过将 $shift_count 传递给 parse_options_detailed 来完成子选项解析,这使得它在移位参数到达子选项的参数后开始解析。此示例对此进行了演示。
自述文件和示例中提供了详细说明
在存储库中。
I wanted to share what I made for parsing options.
Some of my needs were not fulfilled by the answers here so I had to come up with this: https://github.com/MihirLuthra/bash_option_parser
This supports:
Let's say we have a command named
fruit
with usage as follows:-e
takes no args-c
takes two args i.e. how to cut and why to cutfruit
itself takes at least one argument.<command>
is for suboptions likeapple
,orange
etc. (similar togit
which has suboptionscommit
,push
etc. )So to parse it:
Now if there was any usage error, it can be printed using
option_parser_error_msg
as follows:To check now if some options was passed,
Suboption parsing can also be done by passing
$shift_count
toparse_options_detailed
which makes it start parsing after shifting args to reach args of suboption. It is demonstrated in this example.A detailed description is provided in the readme and examples
in the repository.