问题描述
我只想接受长格式的选项的可选参数 选项。
假设我定义了-v
,--verbose
选项,并为argp提供了可选参数:
static struct argp_option opt_global[] =
{
{ "verbose",'v',"level",OPTION_ARG_OPTIONAL,"Increase or set the verbosity level.",-1},...
}
我现在只想在以长选项形式给出时接受可选参数。
所以
--verbose=4
将接受4
作为详细的可选参数。
但是对于简短的选项形式,不应接受可选参数:
| Command Line | Handled as |
|==============|===================|
| -vv | -v -v |
| -vx | -v -x |
| -v4 | -v -4 |
| Command Line | Handled as |
|==============|===================|
| -vo file | -v -o file |
| -vofile | -v -o file |
无论如何,我还是希望帮助输出看起来像这样:
-v,--verbose[=level] Increase or set the verbosity level.
这对GNU argp可行吗?
通过阅读文档并玩耍,我会假设“否”,但也许我 错过了什么。
解决方法
从argp docs(强调是我的):
int键
当前选项提供给选项解析器的整数键。 如果键的值是可打印的ASCII字符(即isascii(键)为true),则还会指定一个简短的选项'-char',其中char是带有代码键的ASCII字符
换句话说,要设置不带短格式的选项,请使用非ascii字符的键。
例如,在您的情况下,可能是:
/* This is not an ascii value so no short form */
#define OPT_VERBOSE_KEY 500
static struct argp_option opt_global[] =
{
{ "verbose",OPT_VERBOSE_KEY,"level",OPTION_ARG_OPTIONAL,"Increase or set the verbosity level.",-1},...
}