Use getopt() function in C/C++ to parse command line arguments
The getopt() function is a builtin function in C and is used to parse command line arguments.
Just include <unistd.h>
Options that require additional parameters such as file name, add a colon after the option letter like this "f:"
If you want to pass 2 parameters(ex: -f filename1 -g filename2) those requireadditional parameters, use like this "f:g:"
If you just want to set or unset the flag, just add the letter like this "hv".
If you combine these two conditions, the result optstring whould be "hvf:" .
Let's compile and test the source(arg1.cpp).
The getopt () function is relatively easy to use. Normal command-line utilities support long-name options that make the options more obvious. The getopt_long function is used for this purpose.
For using getopt_long, you need to include additional header file getopt.h
Let's compile and test the source(arg2.cpp).
As you can see, both --option and --option are available when using getopt_long.
Just include <unistd.h>
Usage 1
Let's suppose you want to pass -h, -v, -f filename parameters to the program.#include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { int opt; while((opt = getopt(argc, argv, "hvf:")) != -1) { switch(opt) { case 'h': printf("option: h is set\n"); break; case 'v': printf("option: v is set\n"); break; case 'f': printf("option h filename: %s\n", optarg); break; } } return 0; }
Options that require additional parameters such as file name, add a colon after the option letter like this "f:"
If you want to pass 2 parameters(ex: -f filename1 -g filename2) those requireadditional parameters, use like this "f:g:"
If you just want to set or unset the flag, just add the letter like this "hv".
If you combine these two conditions, the result optstring whould be "hvf:" .
Let's compile and test the source(arg1.cpp).
root@spypiggy-ubuntu:/usr/local/src/study/cpp/arg# g++ arg1.cpp root@spypiggy-ubuntu:/usr/local/src/study/cpp/arg# ./a.out -h option: h is set root@spypiggy-ubuntu:/usr/local/src/study/cpp/arg# ./a.out -h -v option: h is set option: v is set root@spypiggy-ubuntu:/usr/local/src/study/cpp/arg# ./a.out -h -v -f Hello option: h is set option: v is set option h filename: Hello
Usage 2
There is another function similar to the getopt function. This function is getopt_long.The getopt () function is relatively easy to use. Normal command-line utilities support long-name options that make the options more obvious. The getopt_long function is used for this purpose.
For using getopt_long, you need to include additional header file getopt.h
#include <stdio.h> #include <stdlib.h> #include <getopt.h> int main (int argc, char **argv) { int c; while (1) { static struct option long_options[] = { {"verbose", no_argument, 0, 'v'}, {"help, no_argument, 0, 'h'}, {"file", required_argument, 0, 'f'}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; c = getopt_long (argc, argv, "vhf:", long_options, &option_index); if (c == -1) break; switch (c) { case 'v': printf ("option --verbose (-v) is set \n"); break; case 'h': printf ("option --help (-h) is set \n"); break; case 'f': printf ("option --file (-f) with value `%s'\n", optarg); break; default: abort (); } } return 0; }
Let's compile and test the source(arg2.cpp).
root@spypiggy-ubuntu:/usr/local/src/study/cpp/arg# g++ argc2.cpp root@spypiggy-ubuntu:/usr/local/src/study/cpp/arg# ./a.out --verbose --help option --verbose (-v) is set option --help (-h) is set root@spypiggy-ubuntu:/usr/local/src/study/cpp/arg# ./a.out --verbose --help --file Hello option --verbose (-v) is set option --help (-h) is set option --file (-f) with value `Hello' root@spypiggy-ubuntu:/usr/local/src/study/cpp/arg# ./a.out -v -h --file Hello option --verbose (-v) is set option --help (-h) is set option --file (-f) with value `Hello'
댓글
댓글 쓰기