Вы находитесь на странице: 1из 17

The Linux Environment

Program Arguments getopt() getopt_long() Environment Variables Use of environment variables

Program Arguments

int main(int argc, char *argv[]) $ myprog left right and center argc: 4 argv: {myprog, left, right, and center}

getopt()

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int arg=0; for(arg=0;arg<argc; arg++) { if(argv[arg][0]== '-') { printf("Option %s\n",argv[arg]+1); } else { printf("argument %d: %s\n",arg,argv[arg]); } } exit(0);

#include <unistd.h> int getopt(int argc, char *const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt;

If the option takes a value, that value is pointed to by the external variable optarg. getopt returns -1 when there are no more options to process. A special argument, --, will cause getopt to stop scanning for options. getopt returns ? if there is an unrecognized option, which it stores in the external variable optopt.

If an option requires a value (such as -f in our example) and no value is given, getopt normally returns ?. By placing a colon as the first character of the options string, getopt returns : instead of ? when no value is given.

#include <stdio.h>

#include <unistd.h> #include <stdlib.h> int main(int argc, char *argv[]) { int opt; while((opt = getopt(argc, argv, :if:lr)) != -1) { switch(opt) { case i: case l: case r: printf(option: %c\n, opt); break;

} for(; optind < argc; optind++) printf(argument: %s\n, argv[optind]); exit(0); }

case f: printf(filename: %s\n, optarg); break; case : : printf(option needs a value\n); break; case ?: printf(unknown option: %c\n, optopt); break; }

./argopt -i -lr hi there -f fred.c -q option: i option: l option: r filename: fred.c unknown option: q argument: hi there

getopt_long to create a new version of the example program that can be invoked using long equivalents of options like this: $ ./longopt --initialize --list hi there option: i option: l filename: fred.c ./longopt: invalid option -- q unknown option: q argument: hi there

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #define _GNU_SOURCE #include <getopt.h>

int main(int argc, char *argv[]) { int opt; struct option longopts[] = { {initialize, 0, NULL, i}, {file, 1, NULL, f}, {list, 0, NULL, l}, {restart, 0, NULL, r}, {0,0,0,0}};

while((opt = getopt_long(argc, argv, :if:lr, longopts, NULL)) != -1) { switch(opt) { case i: case l: case r: printf(option: %c\n, opt); break;

case f: printf(filename: %s\n, optarg); break; case :: printf(option needs a value\n); break; case ?: printf(unknown option: %c\n, optopt); break;

} for(; optind < argc; optind++) printf(argument: %s\n, argv[optind]); exit(0);

long option structure is defined in getopt.h and must be included with the constant _GNU_SOURCE, defined to enable the getopt_long functionality.

The

struct option { const char *name; int has_arg; int *flag; int val; };

The members of the structure are shown in the following table. Option Member Description The name of the long option. Abbreviations will be accepted as long name as they cannot be confused with other options. Whether this option takes an argument. Set to 0 for options that do has_arg not take an argument, 1 for options that must have a value, and 2 for those that have an optional argument. Set to NULL to have getopt_long return the value given in val when flag this option is found. Otherwise, getopt_long returns 0 and writes the value of val into the variable pointed to by flag. The value getopt_long is to return for this option. val

Вам также может понравиться