getopt, getopt_long使用笔记

An element of argv that starts with '-' (and is  not exactly  "-" or "--") is an option element.

The characters of this element (aside from the initial '-') are option characters.

以'-’开头的字符(注意!不是字符串!!)就是命令行参数选项

通常通过在循环中调用 getopt 来解析命令行选项。每次调用时 getopt 会返回找到的下一个短选项,如果遇到无法识别的选项则返回 '?'。当没有更多短选项时它返回 -1,并且设置全局变量 optind 的值指向 **argv 中所有段选项之后的第一个元素。

  • optarg :表示当前选项的参数值

A legitimate option character  is  any  visible one  byte  ascii character that is not '-', ':', or ';'. 

If such a character is followed by a colon, the option requires an argument;

getopt() places a pointer  to  the  following text  in  the  same  argv‐element, or the text of the following argv‐element, in optarg.

Two  colons  mean  an option takes an optional arg;

  • optind :表示的是下一个将被处理到的参数在 argv 中的下标值。
  • The variable optind is the index of the next element to be processed in argv.
  • The system initializes this value to 1. // 初始化值为1,下一次调用getopt时,从optind存储的位置重新开始检查选项。
  • argv[0]是函数名称,所有参数从argv[1]开始,所以optind被初始化设置指为1。
  • 当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在argv[optind]至argv[argc-1]中可以找到。

  • opterr :如果 opterr = 0,在 getoptgetopt_longgetopt_long_only 遇到错误将不会输出错误信息到标准输出流。opterr 在非0时,向屏幕输出错误

If  the  caller  has set the global variable opterr to zero, then getopt() does not print an error message.  The caller can determine that there was an error by testing whether the function return value is '?'. 

(By  default, opterr has a nonzero value.)// 初始化值为非0

  • optopt :存储了当前发现的无效选项字符。当 getopt 函数返回 '?' 以指示发现了无效选项时,检查 optopt 来获取该选项字符

By default, getopt() prints an error message on standard error, places the erroneous option character in optopt, and returns '?' as the function result.

// 当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt中, getopt返回'?’。

getopts命令支持两种错误报告模式,详细错误报告模式和抑制错误报告模式。在OPTSTRING之前加冒号是抑制错误报告模式 (:OPTSTRING); 不加是详细错误报告模式。

如果 optstring是以冒号开头:的,命令行当中出现了optstring当中没有的参数将不会提示错误信息。比如出现没有定义的-x不会报"option requires an argument -- x",需要脚本捕捉(:)。

 在命令行选项参数再也检查不到optstring中包含的选项时,返回-1。

错误的调用程序,要么是命令行选项无效,要么是缺少选项参数,正常情况下,getopt()会为这两种情况输出自己的出错信息,并且不区分的均返回’?’。可以采用以下两种方法来更改getopt()函数的出错信息输出行为:

在调用getopt()之前,将opterr设置为0, getopt()函数发现错误的时候强制不输出任何消息。

如果optstring参数的第一个字符是冒号,例如”:ngl:”,那么getopt()会保持沉默,并根据错误情况返回不同字符,如下:
            “无效选项” —— getopt()返回’?’,并且optopt包含了无效选项字符(这是正常的行为)。
            “缺少选项参数” —— getopt()返回’:’

如果optstring参数的第一个字符是冒号,例如”:ngl:”,那么getopt()会保持沉默,并根据错误情况返回不同字符,如下:

    “无效选项” —— getopt()返回’?’,并且optopt包含了无效选项字符(这是正常的行为)。
    “缺少选项参数” —— getopt()返回’:’

If  there  are no more option characters, getopt() returns -1.  Then optind is the index in argv of the first argv‐element that is not an option.

If  the  first character (following any optional '+' or '-' ) of optstring is a colon (':'), then getopt() likewise does not print an error message.  In addition, it returns ':' instead of  '?'  to  indicate  a missing option argument

在'+'指定的扫描模式下,getopt遇到第一个non-option parameter后,就停止解析,把在此之后的内容都作为non-option parameters;'-'指定的扫描模式下,getopt在遇到non-option parameters仍会继续解析,但会把non-option parameters保留在其原本的位置上,不会收集到输出的最后

If the first character of optstring is '+' or the environment variable POSIXLY_CORRECT is set, then option processing stops as

  • 22
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

issta

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值