简体   繁体   中英

Using getopt in C with non-option arguments

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?

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
    }
}

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

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.

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

The GNU Libc example is also not working for MinGW-W64 7.1.0. The non-option arguments are not shifted to the end so that the parsing stops after the first non-option arguments.

So the default permutation option seems not to work.

The author of Mead's Guide to getopt states

If you want to have getopt parse and return the non-option arguments in the while loop (in the order specified), you must direct it to do so by putting a minus (-) in front of the optstring.

The example provided was "-:a:b:X" where the minus (-) "disables getopt from moving all non-option arguments to the end of the command line" and the colon (:) "disables getopt from displaying error messages".

If a non-option argument is found, then getopt will return an integer value of 1.

I'm going to go ahead and throw my answer in. I've tested it, and it complies with getopt as described here on Linux . Note, this answer only allows options first, then non-options after . But this is normal for a lot of CLI tools.

I'm adding this answer now because I found the EDIT in the answer from Klas did not work.


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


int main (int argc, char * const argv[])
{
    int c;
    
    // Process option arguments
    while (-1 != (c = getopt(argc, argv, "abc:"))) {
         switch (c) {
     case 'a': printf("option: a\n"); break;
     case 'b': printf("option: b\n"); break;
     case 'c': printf("option: c with arg: \"%s\"\n", optarg); break;
     default:
         printf("unknown arg: %2X\n", optopt);
     }
    }
    
    // Process remaining arguments
    for (int i = optind; i < argc; ++i) {
        printf("non-option: %s\n", argv[i]);
    }
    
    return EXIT_SUCCESS;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM