[ 永远的UNIX::UNIX技术资料的宝库 ]

首页 > 编程技术 > 其它 > 正文
Linux程式设计-20.getopt
http://www.openchess.org/noitatsko/programming/ (2001-05-27 13:04:00)


getopt在UNIX下的命令列程式特别好用,特别是在你有许多参数要加入时。一般来说,你可以透过「man 3 getopt」来获得其说明。 

--------------------------------------------------------------------------------

int getopt(int argc, char * const argv[],const char *optstring); 
extern char *optarg; 
extern int optind, opterr, optopt; 

--------------------------------------------------------------------------------

例:这个例是从manpage抄出来的,蛮不清处的。我会再找时间写一系列例。 
#include  

       int 
       main (argc, argv) 
            int argc; 
            char **argv; 
       { 
         int c; 
         int digit_optind = 0; 

         while (1) 
           { 
             int this_option_optind = optind ? optind : 1; 
             int option_index = 0; 
             static struct option long_options[] = 
             { 
               {"add", 1, 0, 0}, 
               {"append", 0, 0, 0}, 
               {"delete", 1, 0, 0}, 
               {"verbose", 0, 0, 0}, 
               {"create", 1, 0, 'c'}, 
               {"file", 1, 0, 0}, 
               {0, 0, 0, 0} 
             }; 

             c = getopt_long (argc, argv, "abc:d:012", 
                        long_options, &option_index); 
             if (c == -1) 
            break; 

             switch (c) 
               { 
               case 0: 
                 printf ("option %s", long_options[option_index].name); 
                 if (optarg) 
                   printf (" with arg %s", optarg); 
                 printf ("\n"); 
                 break; 

               case '0': 
               case '1': 
               case '2': 
                 if (digit_optind != 0 && digit_optind != this_option_optind) 
                   printf ("digits occur in two different argv-elements.\n"); 
                 digit_optind = this_option_optind; 
                 printf ("option %c\n", c); 
                 break; 

               case 'a': 
                 printf ("option a\n"); 
                 break; 

               case 'b': 
                 printf ("option b\n"); 
                 break; 

               case 'c': 
                 printf ("option c with value `%s'\n", optarg); 
                 break; 

               case 'd': 
                 printf ("option d with value `%s'\n", optarg); 
                 break; 

               case '?': 
                 break; 

               default: 
                 printf ("?? getopt returned character code 0%o ??\n", c); 
               } 
           } 

         if (optind < argc) 
           { 
             printf ("non-option ARGV-elements: "); 
             while (optind < argc) 
             printf ("%s ", argv[optind++]); 
             printf ("\n"); 
           } 

         exit (0); 
       } 
(http://www.fanqiang.com)
    进入【UNIX论坛

相关文章
Linux程式设计-31.工作群资讯管理(grp) (2001-05-27 22:08:00)
Linux程式设计-30.使用者资讯管理(pwd) (2001-05-27 21:04:00)
Linux程式设计-29.时间处理 (2001-05-27 20:10:01)
Linux程式设计-28.GNU Make (2001-05-27 19:00:00)
Linux程式设计-27.GNU Debugger (2001-05-27 18:08:01)
Linux程式设计-26.PIPE (2001-05-27 17:04:00)
Linux程式设计-25.Message Queues (2001-05-27 16:10:00)
Linux程式设计-24.Semaphores (2001-05-27 15:00:00)
Linux程式设计-23.共享记忆体(Shared Memory) (2001-05-27 14:08:00)
Linux程式设计-20.getopt (2001-05-27 13:04:00)

===更多相关===
 

★  樊强制作 欢迎分享  ★