I have a program with string literals to specify optional command line arguments. How to process these optional command line arguments in c? -
below syntax command line arguments.
./main [-var1 value] [-var2 value] [-var3 value] var1,var2 , var3 strings. ex: var1 aaa, var2 bbb, , var3 ccc.
however similar case single characters options, example a, b , c shown below
./main [-a value] [-b value] [-c value] getopt(argc, argv, "a:b:c:"); can used. there similar way process var1,var2 , var3 program.
man getopt friend!
getopt historical function processes single letter options, extended 2 companions:
getopt_long: accept single letter options command starting 1 single dash (-a) or multi-letter ones starting 2 (--action)getopt_long_only: accept multi-letters options starting 1 or 2 dashes (--actionor-action)
(following extracts man page)
the signature latter 2 :
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex); where longopts pointer first element of array of struct option declared in as
struct option { const char *name; int has_arg; int *flag; int val; }; (the last element of array has filled zeros)
if longindex not null, points variable set index of long option relative longopts.
Comments
Post a Comment