C/C++—— 分析命令行参数的getopt()函数使用介绍

函数介绍:

首先输入命令:man 3 getopt 查看getopt函数的man手册介绍

#include <unistd.h>

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

extern char *optarg;
extern int optind, opterr, optopt; 

函数说明 getopt()用来分析命令行参数。参数argc和argv分别代表参数个数和内容,跟main()函数的命令行参数是一样的。

下面用一个例子来输出main函数中的argc,argv[ ]内容:

#include <stdio.h>


int main(int argc, char* argv[])
{
    int i = 0;
    printf("共%d个命令行参数\n", argc);
    for(i = 0; i < argc; ++i){
        printf("第%d个参数是: %s\n", i+1, argv[i]);
    }

    return 0;
}
输出:

root@linux_ever:~/linux_ever/work_test# ./getopt
共1个命令行参数
第1个参数是: ./getopt
root@linux_ever:~/linux_ever/work_test# ./getopt -a -h help
共4个命令行参数
第1个参数是: ./getopt
第2个参数是: -a
第3个参数是: -h
第4个参数是: help

参数 optstring为选项字符串, 告知 getopt()可以处理哪个选项以及哪个选项需要参数,
如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。
如果在处理期间遇到了不符合optstring指定的其他选项getopt()将显示一个错误消息,并将全域变量optarg设为“?”字符,如果不希望getopt()打印出错信息,则只要将全域变量opterr设为0即可。

getopt() 所设置的全局变量包括:
optarg---指向当前选项的参数的指针(选项字母:后的参数)。
optind——再次调用 getopt() 时的下一个 argv 指针的索引。 optopt——最后一个未知选项。

返回值:

If  an  option  was  successfully  found, then getopt() returns the option character.  If all command-line options have been parsed, then getopt() returns -1.  If getopt() encounters an option character that was not in optstring, then '?' is returned.  If getopt() encounters an option with  a missing  argument,  then  the  return  value  depends  on  the  first character in optstring: if it is ':', then ':' is returned; otherwise '?' is returned.

大概意思就是:

如果一个选项成功被找到,则getopt返回该选项字符,比如a, b, c....
如果所有的选项都被找到,则getopt返回-1。
如果getopt函数遇到了一个在optstring参数中没有指定的选项,则返回'?'。
如果在-a之后忘了输出参数,则返回':'(此时optstring中有字符a:),如果不是a:,则返回'?'


optstring中的指定的内容的意义(例如getopt(argc, argv, "ab:c:de::");)
1.单个字符,表示选项,(如上例中的abcde各为一个选项)
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。(如上例中的b:c:)
3 单个字符后跟两个冒号,表示该选项后可以跟一个参数,也可以不跟。如果跟一个参数,参数必须紧跟在选项后不能以空格隔开。
该参数的指针赋给optarg。(如上例中的e::,如果没有跟参数,则optarg = NULL),但是在实例中测试却是::没什么作用。。


实例:

/*************************************************************************
	> File Name: getopt.c
	> Author: 
	> Mail: 
	> Created Time: 2016年03月31日 星期四 17时18分23秒
 ************************************************************************/

#include <stdio.h>
#include <unistd.h>


int main(int argc, char* argv[])
{
    printf("*****************************************\n");
    int i = 0;
    printf("共%d个命令行参数\n", argc);
    for(i = 0; i < argc; ++i){
        printf("第%d个参数是: %s\n", i+1, argv[i]);
    }
    printf("*****************************************\n");

    char ch;

    while((ch = getopt(argc, argv, "a:bc::h")) != -1){
        //printf("optind = %d, optopt = %d\n", optind, optopt);

        switch(ch){
            case 'a':
                printf("选项是%c, 选项内容: %s\n", ch, optarg);
                break;
            case 'b':
                printf("选项是%c, 选项内容: %s\n", ch, optarg);
                break;
            case 'c':
                printf("选项是%c, 选项内容: %s\n", ch, optarg);
                break;
            case 'h':
                printf("选项是%c\n", ch);
                printf("-a optstring\n");
                printf("-b\n");
                printf("-c optstring or NULL\n");
                printf("-h");
                break;
            default:
                printf("other option: %c\n", ch);
                break;
        }
    }
    return 0;
}

输出:

root@linux_ever:~/linux_ever/work_test# ./getopt -h
*****************************************
共2个命令行参数
第1个参数是: ./getopt
第2个参数是: -h
*****************************************
选项是h
-a optstring
-b
-c optstring or NULL
-hroot@linux_ever:~/linux_ever/work_test# ./getopt -a linux_ever
*****************************************
共3个命令行参数
第1个参数是: ./getopt
第2个参数是: -a
第3个参数是: linux_ever
*****************************************
选项是a, 选项内容: linux_ever
root@linux_ever:~/linux_ever/work_test# ./getopt -a linux_ever -b
*****************************************
共4个命令行参数
第1个参数是: ./getopt
第2个参数是: -a
第3个参数是: linux_ever
第4个参数是: -b
*****************************************
选项是a, 选项内容: linux_ever
选项是b, 选项内容: (null)
root@linux_ever:~/linux_ever/work_test# ./getopt -a linux_ever -b -c ever         
*****************************************
共6个命令行参数
第1个参数是: ./getopt
第2个参数是: -a
第3个参数是: linux_ever
第4个参数是: -b
第5个参数是: -c
第6个参数是: ever
*****************************************
选项是a, 选项内容: linux_ever
选项是b, 选项内容: (null)
选项是c, 选项内容: (null)


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值