zoukankan      html  css  js  c++  java
  • Linux命令选项及参数解析 getopt() getopt_long() 函数

    假如程序命令行启动时,需要指定一系列参数,那么,getopt()与getopt_long()是你的不二选择。

    作为曾经还在手写的孩纸,我发现这个之后,泪流满面。。

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

    若选项在optstring中,返回选项字符,否则返回-1;与该选项对应的参数保存在变量optarg中

    包含在unistd.h,argc和argv与main(int argc, char *argv[])的参数相对应,

    optstring是选项字符集,表现为在启动命令行中'-'后面的首个字符,例如:ls -l -a, 'a'和'l'即在optstring中。

    optstring的格式规范如下:

    1) 单个字符为参数选项

    2) 选项字符后面跟':',表示该选项必须具备参数,参数与选项之间以空格分隔,例如:start -f flile

    3) 选项字符后面跟'::', 表示该选项必须具备参数,参数紧跟选项,二者连接在一起,此为GNU的扩充,例如:start -ffile

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

    若选项在optstring中,返回选项字符,否则返回-1;与该选项对应的参数保存在变量optarg中

    包含在getopt.h中,getopt_long()可以看成是支持长选项的getopt(),argc和argv与main(int argc, char *argv[])的参数相对应,

    长选项是以"--"开头的,举个例子:"-h"与"--help"

    20世纪90年代,Unix应用程序开始支持长选项,Linux是类Unix系统,因此兼容长选项

    getopt_long()的前3个参数与getopt()相同,第4个参数是指向option结构的数组,option结构被称为“长选项表”。longindex参数如果没有设置为NULL,那么它就指向一个变量,这个变量会被赋值为寻找到的长选项在longopts中的索引值,这可以用于错误诊断。

    option结构在getopt.h中的声明如下:
    struct option {
      const char *name;
      int has_args;
      int *flag;
      
    int val;
    }; 

    name:选项字符串

    has_args:三种参数类型,no_argument表示无参数(0),required_argument表示需要参数(1),optional_argument表示参数可选(2)

    flag:如果为NULL,getopt_long()返回该结构val字段中的数值;如果不为NULL,getopt_long()会使得它所指向的变量中填入val字段中的数值,并且getopt_long()返回0;通常flag设置为NULL,val设置为与该长选项对应的短选项

    val:发现了长选项时的返回值,或者flag不是NULL时载入*flag中的值。典型情况下,若flag不是NULL,那么val是个真/假值,譬如1 或0;另一方面,如果flag是NULL,那么val通常是字符常量,若长选项与短选项一致,那么该字符常量应该与optstring中出现的这个选项的参数相同。

    以下是个示例:

     1 #include <unistd.h>
    2 #include <getopt.h>
    3
    4 struct option opts[] = {
    5 {"config", required_argument, NULL, 'f'},
    6 {"help", no_argument, NULL, 'h'},
    7 {"version", no_argument, NULL, 'v'}
    8 };
    9
    10 int32_t main(int32_t argc, char **argv) {
    11 char *configfile = NULL;
    12 int32_t opt = 0;
    13 //while ((opt = getopt(argc, argv, "f:hv")) != -1) {
    14 while ((opt = getopt_long(argc, argv, "f:hv", opts, NULL)) != -1) {
    15 switch (opt) {
    16 case 'f':
    17 configfile = strdup(optarg);
    18 break;
    19 case 'h':
    20 LOG1("Usage: ./chatserver -hv | [-f configure_file]");
    21 LOG1(" -f --config configure file");
    22 LOG1(" -h --help help information");
    23 LOG1(" -v --version help information");
    24 return 0;
    25 case 'v':
    26 LOG1("version 1.0.0");
    27 return 0;
    28 default:
    29 LOG1("Usage: ./chatserver -hv | [-f configure_file]");
    30 LOG1(" -f --config configure file");
    31 LOG1(" -h --help help information");
    32 LOG1(" -v --version help information");
    33 return -1;
    34 }
    35 }
    36 return 0;
    37 }



  • 相关阅读:
    初学maven五节
    Nexus如何上传自己的jar包 (upload artifactory)
    通过maven获得开源项目simple4依赖jar包的几种具体操作
    maven系列笔记(安装、下载jar、搭建私服)
    如何 提高企业网站大数据量 效率
    maven archetype:generate 的进一步理解
    为什么 Git 比 SVN 好
    使用仓库管理器——Sonatype Nexus的九大理由
    IT项目管理工具探讨之_项目群管理
    MAVEN准备建立自己的开发环境
  • 原文地址:https://www.cnblogs.com/caosiyang/p/2417689.html
Copyright © 2011-2022 走看看