前言

在linux中,经常需要各种命令,通常情况下都会带各种参数,而这些参数是如何解析的呢?通常使用GNU C提供的函数getopt、getopt_long、getopt_long_only函数来解析命令行参数。

一、关于命令行参数

命令行参数可以分为两类,一类是短选项,一类是长选项,短选项在参数前加一杠"-",长选项在参数前连续加两杠"--",如下表(ls 命令参数)所示,其中-a,-A,-b都表示短选项,--all,--almost-all, --author都表示长选项。他们两者后面都可选择性添加额外参数。比如--block-size=SIZE,SIZE便是额外的参数。

二、getopt_long函数

getopt函数只能处理短选项,而getopt_long函数两者都可以,可以说getopt_long已经包含了getopt_long的功能。因此,这里就只介绍getopt_long函数。而getopt_long与getopt_long_only的区别很小,等介绍完getopt_long,在提起会更好。

#include <unistd.h>
extern char *optarg;
extern int optind, opterr, optopt;
#include <getopt.h>
int getopt(int argc, char * const argv[],const char *optstring);
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);

参数以及返回值介绍(以上三个函数都适用):

1、argc和argvmain函数的两个参数一致。

2、optstring: 表示短选项字符串。

    形式如“a:b::cd:“,分别表示程序支持的命令行短选项有-a、-b、-c、-d,冒号含义如下:
    (1)只有一个字符,不带冒号——只表示选项, 如-c 
    (2)一个字符,后接一个冒号——表示选项后面带一个参数,如-a 100
    (3)一个字符,后接两个冒号——表示选项后面带一个可选参数,即参数可有可无,如果带参数,则选项与参数直接不能有空格
        形式应该如-b200

3、longopts:表示长选项结构体。结构如下:

struct option
{  const char *name;  int         has_arg;  int        *flag;  int         val;
};
eg:static struct option longOpts[] = {{ "daemon", no_argument, NULL, 'D' },{ "dir", required_argument, NULL, 'd' },{ "out", required_argument, NULL, 'o' },{ "log", required_argument, NULL, 'l' },{ "split", required_argument, NULL, 's' },{ "http-proxy", required_argument, &lopt, 1 },{ "http-user", required_argument, &lopt, 2 },{ "http-passwd", required_argument, &lopt, 3 },{ "http-proxy-user", required_argument, &lopt, 4 },{ "http-proxy-passwd", required_argument, &lopt, 5 },{ "http-auth-scheme", required_argument, &lopt, 6 },{ "version", no_argument, NULL, 'v' },{ "help", no_argument, NULL, 'h' },{ 0, 0, 0, 0 }};

(1)name:表示选项的名称,比如daemon,dir,out等。

(2)has_arg:表示选项后面是否携带参数。该参数有三个不同值,如下:

a: no_argument(或者是0)时 ——参数后面不跟参数值,eg: --version,--help
b: required_argument(或者是1)时 ——参数输入格式为:--参数 值 或者 --参数=值。eg:--dir=/home

           c: optional_argument(或者是2)时  ——参数输入格式只能为:--参数=值

(3)flag:这个参数有两个意思,空或者非空。

a:如果参数为空NULL,那么当选中某个长选项的时候,getopt_long将返回val值。
                   eg,可执行程序 --help,getopt_long的返回值为h.             
           b:如果参数不为空,那么当选中某个长选项的时候,getopt_long将返回0,并且将flag指针参数指向val值。

eg: 可执行程序 --http-proxy=127.0.0.1:80 那么getopt_long返回值为0,并且lopt值为1。

(4)val:表示指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值val。

4、longindex:longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。

5、全局变量

(1)optarg:表示当前选项对应的参数值。

(2)optind:表示的是下一个将被处理到的参数在argv中的下标值。

(3)opterr:如果opterr = 0,在getopt、getopt_long、getopt_long_only遇到错误将不会输出错误信息到标准输出流。opterr在非0时,向屏幕输出错误。

(4)optopt:表示没有被未标识的选项。

6、返回值

(1)如果短选项找到,那么将返回短选项对应的字符。

(2)如果长选项找到,如果flag为NULL,返回val。如果flag不为空,返回0

(3)如果遇到一个选项没有在短字符、长字符里面。或者在长字符里面存在二义性的,返回“?”

(4)如果解析完所有字符没有找到(一般是输入命令参数格式错误,eg: 连斜杠都没有加的选项),返回“-1”

(5)如果选项需要参数,忘了添加参数。返回值取决于optstring,如果其第一个字符是“:”,则返回“:”,否则返回“?”。

注意

(1)longopts的最后一个元素必须是全0填充,否则会报段错误

(2)短选项中每个选项都是唯一的。而长选项如果简写,也需要保持唯一性。

三、测试(自行测试)

1、官网给出测试用例。

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>int
main(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",     required_argument, 0,  0 },{"append",  no_argument,       0,  0 },{"delete",  required_argument, 0,  0 },{"verbose", no_argument,       0,  0 },{"create",  required_argument, 0, 'c'},{"file",    required_argument, 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(EXIT_SUCCESS);
}

2、自己项目相关一个例子。

#include<stdio.h>
#include <getopt.h>
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;void showUsage() {//cout << "Usage: " << PACKAGE_NAME << " [options] URL ..." << endl;cout << "Options:" << endl;cout << " -d, --dir=DIR              The directory to store downloaded file." << endl;cout << " -o, --out=FILE             The file name for downloaded file." << endl;cout << " -l, --log=LOG              The file path to store log. If '-' is specified," << endl;cout << "                            log is written to stdout." << endl;cout << " -D, --daemon               Run as daemon." << endl;cout << " -s, --split=N              Download a file using s connections. s must be" << endl;cout << "                            between 1 and 5. If this option is specified the" << endl;cout << "                            first URL is used, and the other URLs are ignored." << endl;cout << " --http-proxy=HOST:PORT     Use HTTP proxy server. This affects to all" << endl;cout << "                            URLs." << endl;cout << " --http-user=USER           Set HTTP user. This affects to all URLs." << endl;cout << " --http-passwd=PASSWD       Set HTTP password. This affects to all URLs." << endl;cout << " --http-proxy-user=USER     Set HTTP proxy user. This affects to all URLs" << endl;cout << " --http-proxy-passwd=PASSWD Set HTTP proxy password. This affects to all URLs." << endl;cout << " --http-auth-scheme=SCHEME  Set HTTP authentication scheme. Currently, BASIC" << endl;cout << "                            is the only supported scheme." << endl;cout << " -v, --version              Print the version number and exit." << endl;cout << " -h, --help                 Print this message and exit." << endl;cout << "URL:" << endl;cout << " You can specify multiple URLs. All URLs must point to the same file" << endl;cout << " or a download fails." << endl;cout << "Examples:" << endl;cout << " Download a file by 1 connection:" << endl;cout << "  aria2c http://AAA.BBB.CCC/file.zip" << endl;cout << " Download a file by 2 connections:" << endl;cout << "  aria2c -s 2 http://AAA.BBB.CCC/file.zip" << endl;cout << " Download a file by 2 connections, each connects to a different server." << endl;cout << "  aria2c http://AAA.BBB.CCC/file.zip http://DDD.EEE.FFF/GGG/file.zip" << endl;cout << "Reports bugs to <tujikawa at rednoah dot com>" << endl;
}int main(int argc, char* argv[]) {bool stdoutLog = false;string logfile;string dir;string ufilename;int split = 0;bool daemonMode = false;int c;while(1) {int optIndex = 0;int lopt;static struct option longOpts[] = {{ "daemon", no_argument, NULL, 'D' },{ "dir", required_argument, NULL, 'd' },{ "out", required_argument, NULL, 'o' },{ "log", required_argument, NULL, 'l' },{ "split", required_argument, NULL, 's' },{ "http-proxy", required_argument, &lopt, 1 },{ "http-user", required_argument, &lopt, 2 },{ "http-passwd", required_argument, &lopt, 3 },{ "http-proxy-user", required_argument, &lopt, 4 },{ "http-proxy-passwd", required_argument, &lopt, 5 },{ "http-auth-scheme", required_argument, &lopt, 6 },{ "version", no_argument, NULL, 'v' },{ "help", no_argument, NULL, 'h' },{ 0, 0, 0, 0 }};c = getopt_long(argc, argv, "Dd:o:l:s:vh", longOpts, &optIndex);printf("返回值: %c\n",c);if(c == -1) {break;}switch(c) {case 0:{switch(lopt) {case 1: {printf("1: %s\n",optarg);break;}case 2:printf("2: %s\n",optarg);break;case 3:printf("3: %s\n",optarg);break;case 4:printf("4: %s\n",optarg);break;case 5: printf("5: %s\n",optarg);break;case 6:printf("6: %s\n",optarg);break;}break;}case 'D':printf("D: %s\n",optarg);break;case 'd':printf("d: %s\n",optarg);break;case 'o':printf("o: %s\n",optarg);break;case 'l':printf("l: %s\n",optarg);break;case 's':printf("s: %s\n",optarg);break;case 'v':printf("s: %s\n",optarg);//showVersion();exit(0);case 'h':showUsage();exit(0);default:showUsage();exit(1);}}return 0;
}

四、参考文章

[1].https://blog.csdn.net/cashey1991/article/details/7942809

[2].https://www.jianshu.com/p/7a0a8d882787

浅谈linux的命令行解析参数之getopt_long函数相关推荐

  1. Python命令行解析:sys.argv[]函数的简介、案例应用之详细攻略

    Python命令行解析:sys.argv[]函数的简介.案例应用之详细攻略 目录 sys.argv[]函数的简介 sys.argv[]函数的案例应用 1.基础测试 2.进阶用法 3.sys.argv[ ...

  2. linux c 命令行解析 getopt getopt_long optarg optind opterr optopt 简介

    目录 getopt 函数 测试代码 getopt_long函数 代码示例 getopt 函数 头文件 #include<unistd.h> 定义函数 int getopt(int argc ...

  3. 一个自建的Java命令行解析参数类ArgumentsParser

    文章目录 1 简介 2 参数格式定义 3 类功能介绍 3.1 初始化 3.2 主要函数 4 源代码 5 输出结果 6 小结 1 简介 在前文中,介绍了一个Python中的 argparse.Argum ...

  4. python 命令行解析函数_python命令行解析之parse_known_args()函数和parse_args()使用区别介绍...

    在python中,命令行解析的很好用, 首先导入命令行解析模块 import argparse import sys 然后创建对象 parse=argparse.ArgumentParser() 然后 ...

  5. Linux glib命令行解析GOptionEntry使用

    1.安装glib //安装依赖库 sudo apt-get install libffi-dev -y//安装glib # sudo apt-get install libglib2.0-dev -y ...

  6. C语言linux getopt_long()函数(命令行解析)(getopt、getopt_long_only)(短选项 -,长选项 --)(option结构体)(optind、optarg变量)

    参考文章:浅谈linux的命令行解析参数之getopt_long函数 文章目录 前言 一.关于命令行参数 二.getopt_long函数 参数以及返回值介绍(以上三个函数都适用): 1.argc和ar ...

  7. Python命令行解析:IDE内点击Run运行代码直接得出结果、基于TF flags(或argparse、sys.argv)在Dos内命令行(一条命令)调用代码文件得出结果

    Python命令行解析:IDE内点击Run运行代码直接得出结果.基于TF flags(或argparse.sys.argv)在Dos内命令行(一条命令)调用代码文件得出结果 目录 命令行解析 T1.采 ...

  8. linux中whoami命令的作用是,浅谈linux中的whoami与 who指令

    浅谈linux中的whoami与 who指令 whoami 功能说明: 显示用户名称 语法: whoami 补充说明: 显示自身的用户名称,本指令相当于执行  id -un 指令 whoami 与 w ...

  9. Linux命令删除find,浅谈Linux下通过find命令进行rm文件删除的小技巧

    我们经常会通过find命令进行批量操作,如:批量删除旧文件.批量修改.基于时间的文件统计.基于文件大小的文件统计等,在这些操作当中,由于rm删除操作会导致目录结构变化,如果要通过find结合rm的操作 ...

最新文章

  1. python主辅线程_python主线程捕获子线程的方法
  2. python教程怎么读_python怎么读二进制文件
  3. android 4.0之前版本号出现JSONException异常
  4. 两线怎么接三线插座图_水温传感器怎么判断好坏
  5. Druid Spring JDBC Servlet 实现登录功能
  6. mac 下安装pip
  7. Cordova(PhoneGap) 环境搭建与基础
  8. 蓝桥杯入门练习1-4(python)
  9. 数据结构之基于Java的最优二叉树实现
  10. 使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB)
  11. mongo 多条件筛选_Excel筛选功能,你确定这些小技巧你都玩明白了
  12. 7-2 换硬币 (20 分)
  13. Atitit. 软件GUI按钮与仪表盘--web服务器区--获取apache配置文件路径 linux and apache的启动、停止、重启
  14. redies基本数据结构
  15. db2数据库常见问题处理
  16. Statistical Language Model笔记+几个简单平滑算法
  17. Simpson公式、复化梯形公式、Cotes公式以及Romberg公式
  18. 编译原理复习 第一章 概述
  19. 200 OK (from cache) 与 304 Not Modified
  20. 全面云化的变革悄然而至,IPLOOK助力加速云网融合

热门文章

  1. 16.target和currenTarget的区别?
  2. openresty 页面静态化及多级缓存
  3. Github每日精选(第5期):文档管理工具paperless-ngx和dzzoffice
  4. 学习EMACScript6新特性
  5. 全球及中国PIR运动探测器行业研究及十四五规划分析报告
  6. C#单个去水印软件 - 解决方案
  7. java.net.malf,得到java异常:java.net.MalformedURLException:没有协议
  8. CJSON的介绍以及使用
  9. 搭建Linux开发环境遇到的问题
  10. 员工薪资管理系统在netbeans中的全部代码_北京哪家人力资源管理系统比较靠谱...