求助getopt函数的使用方法
程序代码:
#include <stdio.h> #include <stdlib.h> #include "getopt.h" /***************************************************************************** * DEFINES */ /** * flags for different command-line options * * these options don't do anything - there's just here * as examples */ #define FLAG_INTERACT 0x0001 /* interactive mode */ #define FLAG_FORCE 0x0002 /* force mode */ #define FLAG_RECURSIVE 0x0004 /* recursive mode */ /***************************************************************************** * GLOBALS */ int flags = 0; /* store flags here */ int verbose = 5; /* verbosity level */ const char* in_fname = NULL; /* input filename */ const char* out_fname = NULL; /* output filename */ /***************************************************************************** * help */ void help() { printf( "getopt test program\n" "Usage: test [OPTION] [INPUT]\n" " INPUT set input filename (doesn't do anything)\n" " -h help menu (this screen)\n" " -i interactive mode (doesn't do anything)\n" " -f force mode (doesn't do anything)\n" " -r recursive mode (doesn't do anything)\n" " -o filename set output filename (doesn't do anything)\n" ); } /***************************************************************************** * MAIN */ int main(int argc, char* argv[]) { /* check arguments */ while(1) { int c = getopt(argc, argv, "-ifrhv::o:"); if(c == -1) break; switch(c) { case 'i': flags |= FLAG_INTERACT; break; case 'f': flags |= FLAG_FORCE; break; case 'r': flags |= FLAG_RECURSIVE; break; case 'h': help(); exit(0); case 'o': out_fname = optarg; break; case 1: in_fname = optarg; break; #ifdef DEBUG default: printf("Option '%c' (%d) with '%s'\n", c, c, optarg); #endif } } #ifdef DEBUG printf("optind at %d; argv[optind] = '%s'\n", optind, argv[optind]); #endif /* print out what we got */ if(flags & FLAG_INTERACT) printf("in interactive mode\n"); else printf("not in interactive mode\n"); if(flags & FLAG_FORCE) printf("in force mode\n"); else printf("not in force mode\n"); if(flags & FLAG_RECURSIVE) printf("in recursive mode\n"); else printf("not in recursive mode\n"); if(in_fname) printf("input filename: %s\n", in_fname); else printf("no input filename\n"); if(out_fname) printf("output filename: %s\n", out_fname); else printf("no output filename\n"); return 0; }上述红色代码中switch选项中的 1 什么时候成立呢 ?