getopt usage with/without option

时间秒杀一切 提交于 2019-12-11 03:12:08

问题


I'm writing a simple code making use of *argv[] parameter. I'd like to know whether I can use getopt() function for the following intent.

./myprogram -a PATH
./myprogram PATH

The program can either take merely PATH (e.g. /usr/tmp) or take -a option in addition to PATH. Can getopt() be used for this state? If can, how?


回答1:


The program can either take merely PATH (e.g. /usr/tmp) or take option in addition to PATH. Can getopt() be used for this state? If can, how?

Certainly. I'm not sure where you even see a potential issue, unless its that you don't appreciate POSIX's and getopt()'s distinction between options and arguments. They are related, but not at all the same thing.

getopt() is fine with the case that no options are in fact specified, and it gives you access to the non-option arguments, such as PATH appears to be for you, regardless of how many options are specified. The usual usage model is to call getopt() in a loop until it returns -1 to indicate that no more options are available from the command line. At each step, the global variable optind variable provides the index of the next argv element to process, and after getopt() (first) returns -1, optind provides the index of the first non-option argument. In your case, that would be where you expect to find PATH.

int main(int argc, char *argv[]) {
    const char options[] = "a";
    _Bool have_a = 0;
    char *the_path;
    int opt;

    do {
        switch(opt = getopt(argc, argv, options)) {
            case -1:
                the_path = argv[optind];
                // NOTE: the_path will now be null if no path was specified,
                //       and you can recognize the presence of additional,
                //       unexpected arguments by comparing optind to argc
                break;
            case 'a':
                have_a = 1;
                break;
            case '?':
                // handle invalid option ...
                break;
            default:
                // should not happen ...
                assert(0);
                break;
        }
    } while (opt >= 0);
}



回答2:


Using an optstring of "a" allows an argument of -a to act as a flag.
optind helps detect that only one additional argument is present.
The program can be executed as ./program -a path or ./program path

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    char op = ' ';//default value
    int opt;

    while ((opt = getopt(argc, argv, "a")) != -1)//optstring allows for -a argument
    {
        switch (opt)
        {
        case 'a':
            op = 'a';//found option, set op
            break;
        default:
            fprintf(stderr, "%s: unknown option %c\n", argv[0], optopt);
            return 1;
        }
    }
    if ( optind + 1 != argc)//one argument allowed besides optstring values
    {
        fprintf(stderr, "Usage: %s [-a] PATH\n", argv[0]);
        return 1;
    }

    printf("%s %c\n", argv[optind], op);
    return 0;
}


来源:https://stackoverflow.com/questions/55044365/getopt-usage-with-without-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!