Getopt可选参数?

问题描述 投票:6回答:4

我有一个程序,你输入一个选项-d然后在选项后是否提供非可选参数,做一些事情。 继承我的代码:

#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>

#define OPT_LIST "d::" 

int main (int argc, char *argv[])
{
    int c;
    char string[] = "blah";

    while ((c = getopt (argc, argv, OPT_LIST)) != -1)
    {
        switch (c)
        {
            case 'd':
                    printf("%s\n", optarg);
                    break;

            case '?':
                fprintf(stderr, "invalid option\n");
                exit(EXIT_FAILURE);
        }   
    }
}

因此,如果在选项后输入非可选参数,则会打印参数。但是如果用户没有提供非可选参数,我希望它打印出char“string”(这就是为什么我把双冒号放在OPT_LIST中)。但我不知道如何做到这一点,所以任何帮助将不胜感激。

下面是我运行程序时会发生什么:

user:desktop shaun$ ./arg -d hello
hello
user:desktop shaun$ ./arg -d 
./arg: option requires an argument -- d
invalid option

我正在使用C语言运行带有OS X的Mac。

c arguments option getopt optional
4个回答
14
投票

“选项的可选值”功能只是一个GNU libc扩展,POSIX不需要,并且可能只是由Mac OS X附带的libc实现。

options参数是一个字符串,它指定对此程序有效的选项字符。此字符串中的选项字符后面可以跟冒号(':'),表示它需要一个必需的参数。如果选项字符后跟两个冒号('::'),则其参数是可选的;这是一个GNU扩展。

https://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html

事实上,POSIX.1-2008第12.2节“实用语法指南”明确禁止此功能:

准则7:选项参数不应是可选的。

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02


8
投票

根据getopt文档,如果带参数的选项没有,则返回:。它还使用匹配参数设置optopt

因此,使用:

int main (int argc, char *argv[])
{
    int c;
    while ((c = getopt (argc, argv, "d:f:")) != -1)
    {
        switch (c)
        {
            case 'd':
            case 'f':
                printf("option -%c with argument '%s'\n", c, optarg);
                break;
            case ':':
                switch (optopt)
                {
                case 'd':
                    printf("option -%c with default argument value\n", optopt);
                    break;
                default:
                    fprintf(stderr, "option -%c is missing a required argument\n", optopt);
                    return EXIT_FAILURE;
                }
                break;
            case '?':
                fprintf(stderr, "invalid option: -%c\n", optopt);
                return EXIT_FAILURE;
        }
    }
    return EXIT_SUCCESS;
}

4
投票

也许我误解了这个问题。我正在阅读它,好像用户想要过度使用getopt的默认错误处理。如果是这种情况,不应该在他们的OPT_LIST的开头有一个:我认为上面的代码是好的,但是,我认为它仍然会打印出来

./arg: option requires an argument -- d

为了抑制这种情况,我们不需要:在OPT_LIST的开头?例如,更改此:

while ((c = getopt (argc, argv, "d:f:")) != -1)

对此:

while ((c = getopt (argc, argv, ":d:f:")) != -1)

如我错了请纠正我。


0
投票

试试这个解决方案这个对我有用。将选项'z'视为带有可选参数的选项。

int c;
opterr = 0; //if (opterr != 0) (which it is by default), getopt() prints its own error messages for invalid options and for missing option arguments.
while ((c = getopt (argc, argv, "x:y:z:")) != -1)
    switch (c)
    {
        case 'x':
        case 'y':
        case 'z':
            printf("OK ... option -%c with argument '%s'\n", c, optarg);
            break;

        case '?':
            if (optopt == 'x' || optopt == 'y')
                fprintf (stderr, "ERR ... Option -%c requires an argument.\n", optopt);
            else if(optopt == 'z' && isprint(optopt))
            {
                printf("OK ... option '-z' without argument \n");
                break;
            }
            else if (isprint (optopt))
                fprintf (stderr, "ERR ... Unknown option `-%c'.\n", optopt);
            else
                fprintf (stderr, "ERR ... Unknown option character `\\x%x'.\n", optopt);
            return -1;
        default: ;
  }

这里有些例子:

./prog -x
    ERR ... Option -x requires an argument.

./prog -y
    ERR ... Option -x requires an argument.

./prog -z
    OK ... option '-z' without argument

./prog -x aaa
    OK ... option -x with argument 'aaa'

./prog -y bbb -x aaa
    OK ... option -y with argument 'bbb'
    OK ... option -x with argument 'aaa'

./prog -x aaa -y bbb -z
    OK ... option -x with argument 'aaa'
    OK ... option -y with argument 'bbb'
    OK ... option '-z' without argument

./prog -x aaa -y bbb -z ccc
    OK ... option -x with argument 'aaa'
    OK ... option -y with argument 'bbb'
    OK ... option -z with argument 'ccc'
© www.soinside.com 2019 - 2024. All rights reserved.