[ 永遠的UNIX::UNIX技術資料的寶庫 ]   GB | BIG5

首頁 > 編程技術 > 其它 > 正文
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)

===更多相關===
 

★  樊強制作 歡迎分享  ★