之前和C中的争吵后处理的命令行选项 [英] Handling command line options before and after an argument in C
问题描述
到目前为止,我一直在使用的 getopt_long
解析选项命令行C程序。
So far I've been using getopt_long
to parse options for a command line C program.
有没有办法阻止 getopt_long
解析当它击中一个非选项参数?如果没有,什么是用C来处理这个的最佳方式?
Is there a way to stop getopt_long
parsing when it hits a non-option argument? If not, what's the best way to handle this in C?
要举一个例子,我想以同样的方式来处理命令的混帐和一个命令之前有一般的参数,经过特定的命令参数是:
To give an example, I'd like to handle commands in a similar way to git, and have general arguments before a command, and command-specific arguments after it:
git [general options] <command> [command options]
例如:
git --bare commit -a
git -p --bare status -s
-p
和 - 裸
是通用的选项,可与所有命令中使用,而 -a
是特定的提交
命令, -s
具体在状态
命令。
-p
and --bare
are general options, and can be used with all commands, whereas -a
is specific to the commit
command, and -s
specific to the status
command.
使用 getopt_long
将尝试首先解析所有的选项,然后离开要处理非选项参数。我想最好要停止解析一次,我打了一个非选项(即命令),然后将余下的参数到特定的命令选项解析器。
Using getopt_long
will try and parse all the options first, and then leave the non-option arguments to be handled. I'd ideally like to stop parsing once I hit a non-option (i.e. the command), and then pass the remaining arguments to a command-specific option parser.
推荐答案
在GNU的Getopt手册说:
The GNU Getopt manual says:
POSIX要求以下行为:第一个非选项阻止了选项处理。该模式通过两种设置环境变量POSIXLY_CORRECT或加号('+')开头的选项参数字符串选择。
POSIX demands the following behavior: The first non-option stops option processing. This mode is selected by either setting the environment variable POSIXLY_CORRECT or beginning the options argument string with a plus sign (‘+’).
这篇关于之前和C中的争吵后处理的命令行选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!