Using getopt in C with non-option arguments

37,142

Solution 1

getopt sets the optind variable to indicate the position of the next argument.

Add code similar to this after the options loop:

if (argv[optind] == NULL || argv[optind + 1] == NULL) {
  printf("Mandatory argument(s) missing\n");
  exit(1);
}

Edit:

If you want to allow options after regular arguments you can do something similar to this:

while (optind < argc) {
  if ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) {
    // Option argument
    switch (c) {
        case 'i': {
            i = (int)atol(optarg);
        }
        case 'd': {
            d = (int)atol(optarg);
        }
        case 'b':
            buf = 1;
            break;
        case 't':
            time = 1;
            break;
        case 'w':
            w = (int)atol(optarg);
            break;
        case 'h':
            h = (int)atol(optarg);
            break;
        case 's':
            s = (int)atol(optarg);
            break;
        default:
            break;
    }
    else {
        // Regular argument
        <code to handle the argument>
        optind++;  // Skip to the next argument
    }
}

Solution 2

Really good example could be found here: GNU Libc The code:

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

int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;

opterr = 0;

while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c)
{
case 'a':
    aflag = 1;
    break;
case 'b':
    bflag = 1;
    break;
case 'c':
    cvalue = optarg;
    break;
case '?':
    if (optopt == 'c')
    fprintf (stderr, "Option -%c requires an argument.\n", optopt);
    else if (isprint (optopt))
    fprintf (stderr, "Unknown option `-%c'.\n", optopt);
    else
    fprintf (stderr,
        "Unknown option character `\\x%x'.\n",
        optopt);
    return 1;
default:
    abort ();
}

printf ("aflag = %d, bflag = %d, cvalue = %s\n",
    aflag, bflag, cvalue);

for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}

It allows to have options before and after arguments. I did compile and run test example:

$ ./a.out aa ff bb -a -ctestparam hello
aflag = 1, bflag = 0, cvalue = testparam
Non-option argument aa
Non-option argument ff
Non-option argument bb
Non-option argument hello

Solution 3

According to https://www.man7.org/linux/man-pages/man3/getopt.3.html

By default, getopt() permutes the contents of argv as it scans, so that eventually all the nonoptions are at the end. Two other scanning modes are also implemented. If the first character of optstring is '+' or the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a nonoption argument is encountered. If the first character of optstring is '-', then each nonoption argv-element is handled as if it were the argument of an option with character code 1. (This is used by programs that were written to expect options and other argv-elements in any order and that care about the ordering of the two.) The special argument "--" forces an end of option-scanning regardless of the scanning mode.

Solution 4

int main(int argc, char** argv) {
    
    
  char* inputfile;
  char* outputfile;
  char* output_file_type;
  char* color_red;
  char* color_blue;
  char* color_green;
  
  
  int opt;
  
  if (argv[optind] == NULL || argv[optind + 1] == NULL) {
  printf("Mandatory argument(s) missing\n");
  exit(1);
    }
 
  
  while((opt = getopt(argc, argv, ":i:o:r:g:b:t:")) != -1){
    switch(opt){
      case 'i':
        inputfile = optarg;
        printf("Input file : %s\n",inputfile);
        break;  
      case 'o':
        outputfile = optarg;
        printf("Output File: %s\n",outputfile);
        break;
      case 't':
        output_file_type = optarg;
        printf("Output File type: %s\n", output_file_type);
        break;
      case 'r':
        color_red = optarg;
        printf("Color Red: %s\n",color_red);
        break;
      case 'g':
        color_green = optarg;
        printf("Color Green: %s\n",color_green);
        break;
      case 'b':
        color_blue = optarg;
        printf("Color Blue: %s\n",color_blue);
        break;
      case ':':
        printf("option needs a value\n");
        break;
      case '?':
        printf("unknown option: %c\n", optopt);
        break;
    }
  
  }
  
  for (; optind < argc; optind++){
      
      
       printf("Given extra arguments: %s\n", argv[optind]);
     
   
  }  


    return (EXIT_SUCCESS);
}

Run commands:

gcc main.c -o image
./image -i ./resource/input_file.bmp -o ./resource/output_file.bmp -t BPM -r 10 -g 24 -b 40

output:

Input file : ./resource/input_file.bmp
Output File: ./resource/output_file.bmp
Output File type: BPM
Color Red: 10
Color Green: 24
Share:
37,142

Related videos on Youtube

Conor Taylor
Author by

Conor Taylor

Updated on January 02, 2022

Comments

  • Conor Taylor
    Conor Taylor over 2 years

    I'm making a small program in C that deals with a lot of command line arguments, so I decided to use getopt to sort them for me.

    However, I want two non-option arguments (source and destination files) to be mandatory, so you have to have them as arguments while calling the program, even if there's no flags or other arguments.

    Here's a simplified version of what I have to handle the arguments with flags:

    while ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) {
        switch (c) {
            case 'i': {
                i = (int)atol(optarg);
            }
            case 'd': {
                d = (int)atol(optarg);
            }
            case 'b':
                buf = 1;
                break;
            case 't':
                time = 1;
                break;
            case 'w':
                w = (int)atol(optarg);
                break;
            case 'h':
                h = (int)atol(optarg);
                break;
            case 's':
                s = (int)atol(optarg);
                break;
            default:
                break;
        }
    }
    

    How do I edit this so that non-option arguments are also handled?

    I also want to be able to have the non-options either before or after the options, so how would that be handled?

  • Conor Taylor
    Conor Taylor almost 11 years
    OK, but my loop will exit if the mandatory arguments come before the optional ones, so only the mandatory ones will be processed and not the optional ones. How do i fix this?
  • Klas Lindbäck
    Klas Lindbäck almost 11 years
    It is common to require that options come before arguments. Just specify it in the man page.
  • Conor Taylor
    Conor Taylor almost 11 years
    Yeah I know but for example with the ssh command, the -p flag can come either before or after the mandatory username@server argument. I just wanna know how to do that
  • Klas Lindbäck
    Klas Lindbäck almost 11 years
    @Conor I've added an outline on how to do it, see the edit above.
  • Evan Senter
    Evan Senter almost 10 years
    Your edit code was very helpful, thanks. A little silly that I have to force continued parsing by micro-managing a global variable, but it gets the job done!
  • gatopeich
    gatopeich almost 7 years
    Should check that argc > optind before accessing argv[optind]!
  • Klas Lindbäck
    Klas Lindbäck almost 7 years
    @gatopeich argv[argc] is guaranteed by the C standard to be NULL, so the code I provided should work just fine.
  • gatopeich
    gatopeich almost 7 years
    Thanks, I did not know that. Still it is strictly safer and more efficient to compare argc with optind: if ( optind+2 > argc) { <missing 2 arguments> }.
  • ishmael
    ishmael almost 5 years
    This does not work using Mac OSX's version of getopt.
  • Peter
    Peter almost 5 years
    The example you provided under EDIT clearly doesn't work (at least on Linux), I can't believe nobody has noticed that since this was first posted.
  • keniee van
    keniee van about 4 years
    in linux glibc, optind will points to mandatory arguments even they are behind options after getopt()
  • code-kobold
    code-kobold over 3 years
    While your citation may inform about the issue at hand, pls. elaborate on your answer to address the question "How do I edit this so that non-option arguments are also handled?"