天天看點

linux下的getopt以及在python中的使用

一、linux下的解析

1、原型

int getopt(int argc,char * const argv[],const char * optstring);

前兩個參數為main入參,第三個參數為選項及操作數。

2、舉個例子:"a:b:cd:e"

表示-a,-b,-c,-d,-e,冒号表示參數;

一個冒号表示選項後面必帶參數。參數可以和選項寫一起也可以用空格隔開,如 -a123  or -a 123。

兩個冒号表示選項的參數可選。若有參數,則跟選項不可以有空格。

3、getopt()全局變量:

char *optarg ---  目前選項參數字元串

int optind -- argv目前索引值。當getopt()在while中試用時,循環結束,剩下的字串視為操作數,在argv[optind]至argv[argc-1]中找到

int opterr -- 這個變量非零時,getopt()函數為“無效選項”和“缺少參數選項”,并輸出其錯誤資訊。

int optopt -- 當發現無效選項字元之時,getopt()函數或傳回“?”字元,或傳回“:”字元,并且optopt包含了所發現的無效選項字元。

4、代碼

#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
    
    int ch;
    printf("\n\n");
    printf("optind:%d,opterr:%d\n",optind,opterr);
    printf("--------------------------\n");
       while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
       {
        printf("optind: %d\n", optind);
           switch (ch) 
        {
               case 'a':
                       printf("HAVE option: -a\n\n");   
                       break;
               case 'b':
                       printf("HAVE option: -b\n"); 
                       printf("The argument of -b is %s\n\n", optarg);
                       break;
               case 'c':
                       printf("HAVE option: -c\n");
                       printf("The argument of -c is %s\n\n", optarg);
                       break;
               case 'd':
                   printf("HAVE option: -d\n");
                     break;
              case 'e':
                    printf("HAVE option: -e\n");
                    printf("The argument of -e is %s\n\n", optarg);
                  break;
              case '?':
                       printf("Unknown option: %c\n",(char)optopt);
                       break;
               }
       }
      printf("----------------------------\n");
      printf("optind=%d,argv[%d]=%s\n",optind,optind,argv[optind]);

      printf("--------------------------\n");
      for(i=0;i<argc;i++)
      {
          printf("%s\n",argv[i]);
      }
      printf("--------------------------\n");


}
           

二、python下的使用

1、指令行選項有短選項和長選項兩種格式。短選項格式為“-”加上單個字母選項;長選項為“--”加上一個單詞。Python提供getopt子產品支援兩種用法。

2、上例子

import getopt, sys

opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])#"ho:"也可以寫成'-h-o:'
print(opts)

print(args)
           

1)需導入getopt子產品

2)sys.argv 為所有指令行參數以空格為分隔符,儲存的清單,其中第一個為腳本檔案名,是以過濾掉,從第二個參數開始,試用sys.srgv[1:]

3)短格式“ho:”,第一個選項後面不帶參數,第二個選項後面必帶參數

4)長格式“help”沒有“=”後面不帶參數,“output=”後面需帶一個參數

5)函數傳回opts為分析出的格式資訊,args為不屬于格式資訊的剩餘指令行參數

對于短格式,“-”後面緊跟一個選項字母,若有參數可以用空格分開,也可以不用,如:-o,-oa,-obbb,-o bbb, -o "a b"

對于長格式,“--”後面緊跟一個單詞,若有參數,後面需緊跟“=”,再加上參數。“=”前後不能有空格,如:--help=file1

用上面例子輸入運作

1)輸入

python test1.py -h -o ooo --help --output=out file1 file2

輸出

[('-h', ''), ('-o', 'ooo'), ('--help', ''), ('--output', 'out')]

['file1', 'file2']

2)輸入

python test1.py -h -o ooo ooo2 --help --output=out file1 file2

輸出

[('-h', ''), ('-o', 'ooo')]

['ooo2', '--help', '--output=out', 'file1', 'file2']