天天看点

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']