linux系统编程——编程杂项——命令行参数

时间:2021-09-01 15:06:47   收藏:0   阅读:20

1. 规范

根据规范,我们使用 - 表示 option,option后面可以加关联值arg

wc -l

为了遵守该规则,linux提供了getopt getopt_long函数

2. getopt

       int getopt(int argc, char * const argv[],
                  const char *optstring);
getopt(argc, argv, "if:lr");

上面说明,有选项: -i -l -r -f ,其中-f后要紧跟一个文件名参数。

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

int main(int argc, char *argv[])
{
        int opt;


        while ((opt = getopt(argc, argv, "if:lr"))      != -1) {
                switch (opt) {
                        case ‘i‘:
                        case ‘l‘:
                        case ‘r‘:
                                printf("option : %c\n", opt);
                                break;

                        case ‘f‘:
                                printf("filename : %s\n", optarg);
                                break;

                        case ‘:‘:
                                printf("option needs a value\n");
                                break;

                        case ‘?‘:
                                printf("unknow option : %c\n", optopt);
                                break;

                }
        }

        for (; optind < argc; optind++) {
                printf("remaining arguments : %s\n", argv[optind]);
        }

        return 0;
}
[root@VM-0-12-centos test]# ./a.out -i -f ./test.c -f asdf asdf
option : i
filename : ./test.c
filename : asdf
argument : asdf

3. getopt_long

getopt_long接受 双划线(--)的长参数,更明确 参数含义

./longopt --init --list ‘hi there‘ --file fred.c -q
option: i
option: l
filename: fred.c
./longopt: invaild option -- q
unknown option: q
argument: hi there

长选项可以缩写,关联选项可以用--option=value

./longopt --init -l --file=fred.c ‘hi there‘
option i
option l
filename: fred.c
argument: hi there
       int getopt_long(int argc, char * const argv[],
                  const char *optstring,
                  const struct option *longopts, int *longindex);

       int getopt_long_only(int argc, char * const argv[],
                  const char *optstring,
                  const struct option *longopts, int *longindex);

getopt_long 大部分参数和getopt一样,只是多了两个参数:
@longopts : 描述长选项
@longindex: 作为optind长选项版本使用

           struct option {
               const char *name;
               int         has_arg;
               int        *flag;
               int         val;
           };

@name : 长选项的名称
@has_arg : 该选项是否带参数,0表示不带,1表示必须带,2表示可选参数
@flag : 设置为NULL表示找到该选项时,getopt_long返回在成员val里给出的值,否则getopt_long返回0,并将val值写入flag指向的变量
@val : getopt_long 为该选项返回的值

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

#define _GNU_SOURCE
#include <getopt.h>

int main(int argc, char *argv[])
{
        int opt;

        struct option longopts[] = {
                {"init", 0, NULL, ‘i‘},
                {"file", 1, NULL, ‘f‘},
                {"list", 0, NULL, ‘l‘},
                {"restart", 0, NULL, ‘r‘},
                {0,0,0,0}
        };



        while ( (opt = getopt_long(argc, argv, ":if:lr", longopts, NULL)) != -1) {
                switch (opt) {
                        case ‘i‘:
                        case ‘l‘:
                        case ‘r‘:
                                printf("option : %c\n", opt);
                                break;
                        case ‘f‘:
                                printf("filename : %s\n", optarg);
                                break;
                        case ‘:‘:
                                printf("option needs a value\n");
                                break;
                        case ‘?‘:
                                printf("unknown option : %c\n", optopt);
                                break;

                }
        }

        for (; optind < argc; optind++) {
                printf("remaining argument : %s\n", argv[optind]);
        }

        return 0;
}

原文:https://www.cnblogs.com/yangxinrui/p/15209031.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!