cargumentscommand-line-argumentsgetoptargp

getopt usage with/without option


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?


Solution

  • 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);
    }