告诉 getopt 还没有完成解析选项
Tell getopt it's not done parsing option
我正在使用 getopt 来解析选项。我解析的选项之一 (-a
) 有一个强制参数。
我的 getopt 调用如下所示:
while((c = getopt(argc, argv, "a:b")) != -1) {
switch(c) {
case 'a':
foo = atoi(optarg);
break;
...
}
}
这是我的问题:
假设有人跑步 ./my-command -a100b
。我希望我的程序告诉我有一个带有参数 100 的 -a
选项和 一个 -b
选项。但是,getopt 将其解析为带有参数 100b
.
的 -a
选项
如何告诉 getopt 尚未完成解析 -a100b
?我想告诉 getopt 从字符 b
开始解析,以便它识别出有一个 -b
选项。
Suppose someone runs ./my-command -a100b
. I want my program to tell
that there is a -a
option with an argument 100 and a -b
option.
However, getopt parses this as a -a
option with an argument 100b
.
是的,确实如此。这就是 getopt()
解析的选项语言的工作原理。
How can I tell getopt that it's not done parsing -a100b
? I want to
tell getopt to start parsing at the character b
, so that it recognizes
that there is a -b
option.
你不能。 POSIX-standard getopt()
和我所知道的任何扩展版本(例如 GNU's)都不支持这样的事情。特别是,POSIX specficiation for getopt
部分表示,
The getopt()
function is a command-line parser that shall follow
Utility Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 in XBD Utility
Syntax Guidelines.
Those guidelines 是关于命令行选项及其格式的。与您的问题最相关的是:
Guideline 3:
Each option name should be a single alphanumeric character (the alnum character classification) from the portable character set. [...]
Guideline 4:
All options should be preceded by the '-' delimiter character.
Guideline 5:
One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when
grouped behind one '-' delimiter.
Guideline 6:
Each option and option-argument should be a separate argument, except as noted in Utility Argument Syntax, item (2).
特别注意准则 5:当您将选项组合在一起时,只有最后一个可能是带有参数的选项。这允许该组的尾部或下一个完整参数被解释为参数,这就是您看到的 getopt
所做的。
当然,如果您希望您的程序接受不符合 POSIX 指南的选项,那么您可以自由地让它这样做,但您可能需要执行自己的选项解析.也许你可以获得 getopt
的帮助,但你需要在顶部添加一个重要的自定义解析层。从头开始自己动手可能更容易。
我正在使用 getopt 来解析选项。我解析的选项之一 (-a
) 有一个强制参数。
我的 getopt 调用如下所示:
while((c = getopt(argc, argv, "a:b")) != -1) {
switch(c) {
case 'a':
foo = atoi(optarg);
break;
...
}
}
这是我的问题:
假设有人跑步 ./my-command -a100b
。我希望我的程序告诉我有一个带有参数 100 的 -a
选项和 一个 -b
选项。但是,getopt 将其解析为带有参数 100b
.
-a
选项
如何告诉 getopt 尚未完成解析 -a100b
?我想告诉 getopt 从字符 b
开始解析,以便它识别出有一个 -b
选项。
Suppose someone runs
./my-command -a100b
. I want my program to tell that there is a-a
option with an argument 100 and a-b
option. However, getopt parses this as a-a
option with an argument100b
.
是的,确实如此。这就是 getopt()
解析的选项语言的工作原理。
How can I tell getopt that it's not done parsing
-a100b
? I want to tell getopt to start parsing at the characterb
, so that it recognizes that there is a-b
option.
你不能。 POSIX-standard getopt()
和我所知道的任何扩展版本(例如 GNU's)都不支持这样的事情。特别是,POSIX specficiation for getopt
部分表示,
The
getopt()
function is a command-line parser that shall follow Utility Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 in XBD Utility Syntax Guidelines.
Those guidelines 是关于命令行选项及其格式的。与您的问题最相关的是:
Guideline 3: Each option name should be a single alphanumeric character (the alnum character classification) from the portable character set. [...]
Guideline 4: All options should be preceded by the '-' delimiter character.
Guideline 5: One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when grouped behind one '-' delimiter.
Guideline 6: Each option and option-argument should be a separate argument, except as noted in Utility Argument Syntax, item (2).
特别注意准则 5:当您将选项组合在一起时,只有最后一个可能是带有参数的选项。这允许该组的尾部或下一个完整参数被解释为参数,这就是您看到的 getopt
所做的。
当然,如果您希望您的程序接受不符合 POSIX 指南的选项,那么您可以自由地让它这样做,但您可能需要执行自己的选项解析.也许你可以获得 getopt
的帮助,但你需要在顶部添加一个重要的自定义解析层。从头开始自己动手可能更容易。