getopt, getopt_long使用笔记

devtools/2024/9/24 2:10:16/

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.

  • 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 soon as a nonoption argument is  encountered. 

If '+' is


http://www.ppmy.cn/devtools/13137.html

相关文章

Linux下的UDEV机制/守护进程

一. Udev机制概念引入 ( 需要在 etc/udev/rules.d/ 下创建设备的相关规则,不然有可能udev机制生成的设备文件不具备可读可写的权限,adb无法成功通过该设备文件访问设备 ) a. 创建文件夹 sudo vim Xiaomi-audroid.rules b. 添加规则 …

JS实现Promise.all、Promise.race手写

Promise.all 当所有的子Promise都完成,该Promise完成,返回值是全部值得数组。有任何一个失败,该Promise失败,返回值是第一个失败的子Promise结果。 function PromiseAll(arr) {const list Array.from(arr);let cur 0;const da…

Spring三级缓存源码解析

Spring三级缓存 前置知识三级缓存定义SpringBean生命周期 Bean的初始化getSingleton 分析加入一级缓存 CreateBean过程(A)A填充属性BB填充属性A,执行getSingleton(A)B完成初始化 前置知识 三级缓存定义 public class DefaultSingletonBeanRegistry ext…

服务运维篇-通过防火墙抵御渗透扫描

这段时间为应对渗透攻击(GFYL),各服务都要做好端口控制,为此分享和记录工作中的一个方法,希望对各位有这问题的人有所帮助。 环境:Kubernetes(K8S)服务部署,Nginx服务反…

租房管理|基于SprinBoot+vue的租房管理系统(源码+数据库+文档)

租房管理目录 基于SprinBootvue的租房管理系统 一、前言 二、系统设计 三、系统功能设计 前台 后台 管理员 订单信息管理 屋主申诉管理 屋主权限 房源信息管理 订单信息管理 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获…

day18 java ​​​​​​​集合Collection的List和Set

Collection分成List和Set|------Collection|-------List : 存储的元素是有序的并且可以重复|--------ArrayList|--------LinkedList|--------Vector|-------Set : 存储的元素是无序的并且不可以重复|--------HashSet|--------LinkedHashSet|--------TreeSet List接口 List常…

MyBatisPlus自定义SQL

✅作者简介:大家好,我是Leo,热爱Java后端开发者,一个想要与大家共同进步的男人😉😉🍎个人主页:Leo的博客💞当前专栏: 循序渐进学SpringBoot ✨特色专栏: MySQL学习 🥭本文内容:MyBatisPlus自定义SQL 📚个人知识库: Leo知识库,欢迎大家访问 目录 1.前言☕…

Jsp 中的getServletContext全局数据共享

servletContext作用于不同用户之上 1. 一个用户将数据保存到了servletContext中, // getcontext的servlet程序 Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context this.get…