getopt解析

2024-05-27 16:58
文章标签 解析 getopt

本文主要是介绍getopt解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

getopt详解

       The  getopt()  function  parses  the  command-line  arguments.  Its arguments argc and argv are the argument count and array aspassed to the main() function on program invocation.  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.  If getopt() iscalled repeatedly, it returns successively each of the option characters from each of the option elements.The variable optind is the index of the next element to be processed in argv.  The system initializes this  value  to  1.   Thecaller can reset it to 1 to restart scanning of the same argv, or when scanning a new argument vector.If getopt() finds another option character, it returns that character, updating the external variable optind and a static vari‐able nextchar so that the next call to getopt() can resume the scan with the following option character or argv-element.If there are no more option characters, getopt() returns -1.  Then optind is the index in argv of the first  argv-element  thatis not an option.optstring  is  a  string  containing  the legitimate option characters.  If such a character is followed by a colon, the optionrequires an argument, so getopt() places a pointer to the following text in the same argv-element, or the text of the followingargv-element,  in optarg.  Two colons mean an option takes an optional arg; if there is text in the current argv-element (i.e.,in the same word as the option name itself, for example, "-oarg"), then it is returned in optarg, otherwise optarg  is  set  tozero.   This  is  a  GNU extension.  If optstring contains W followed by a semicolon, then -W foo is treated as the long option--foo.  (The -W option is reserved by POSIX.2 for implementation extensions.)  This behavior is a GNU extension, not  availablewith libraries before glibc 2.By  default,  getopt()  permutes  the  contents of argv as it scans, so that eventually all the nonoptions are at the end.  Twoother modes are also implemented.  If the first character of optstring is '+' or the environment  variable  POSIXLY_CORRECT  isset,  then option processing stops as soon as a nonoption argument is encountered.  If the first character of optstring is '-',then each nonoption argv-element is handled as if it were the argument of an option with character code 1.  (This  is  used  byprograms that were written to expect options and other argv-elements in any order and that care about the ordering of the two.)The special argument "--" forces an end of option-scanning regardless of the scanning mode.If getopt() does not recognize an option character, it prints an error message to stderr, stores the character in  optopt,  andreturns '?'.  The calling program may prevent the error message by setting opterr to 0.If  getopt()  finds an option character in argv that was not included in optstring, or if it detects a missing option argument,it returns '?' and sets the external variable optopt to the actual option character.  If the  first  character  (following  anyoptional  '+'  or  '-'  described  above) of optstring is a colon (':'), then getopt() returns ':' instead of '?' to indicate amissing option argument.  If an error was detected, and the first character of optstring is not a colon, and the external vari‐able opterr is nonzero (which is the default), getopt() prints an error message.getopt_long() and getopt_long_only()The getopt_long() function works like getopt() except that it also accepts long options, started with two dashes.  (If the pro‐gram accepts only long options, then optstring should be specified as an empty string (""), not NULL.)  Long option  names  maybe abbreviated if the abbreviation is unique or is an exact match for some defined option.  A long option may take a parameter,of the form --arg=param or --arg param.longopts is a pointer to the first element of an array of struct option declared in <getopt.h> asstruct option {const char *name;int         has_arg;int        *flag;int         val;};The meanings of the different fields are:name   is the name of the long option.has_argis: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an argu‐ment; or optional_argument (or 2) if the option takes an optional argument.flag   specifies  how  results are returned for a long option.  If flag is NULL, then getopt_long() returns val.  (For example,the calling program may set val to the equivalent short option character.)  Otherwise, getopt_long() returns 0, and flagpoints to a variable which is set to val if the option is found, but left unchanged if the option is not found.val    is the value to return, or to load into the variable pointed to by flag.The last element of the array has to be filled with zeros.If longindex is not NULL, it points to a variable which is set to the index of the long option relative to longopts.getopt_long_only()  is  like  getopt_long(), but '-' as well as "--" can indicate a long option.  If an option that starts with'-' (not "--") doesn't match a long option, but does match a short option, it is parsed as a short option instead.RETURN VALUEIf an option was successfully found, then getopt() returns the option character.  If all command-line options have been parsed,then  getopt()  returns  -1.   If  getopt() encounters an option character that was not in optstring, then '?' is returned.  Ifgetopt() encounters an option with a missing argument, then the return value depends on the first character in optstring: if itis ':', then ':' is returned; otherwise '?' is returned.getopt_long()  and  getopt_long_only()  also return the option character when a short option is recognized.  For a long option,they return val if flag is NULL, and 0 otherwise.  Error and -1 returns are the same as for getopt(), plus '?' for an ambiguousmatch or an extraneous parameter.ENVIRONMENTPOSIXLY_CORRECTIf this is set, then option processing stops as soon as a nonoption argument is encountered._<PID>_GNU_nonoption_argv_flags_This  variable was used by bash(1) 2.0 to communicate to glibc which arguments are the results of wildcard expansion andso should not be considered as options.  This behavior was removed in bash(1) version 2.01, but the support  remains  inglibc.CONFORMING TOgetopt():POSIX.2  and  POSIX.1-2001,  provided  the environment variable POSIXLY_CORRECT is set.  Otherwise, the elements of argvaren't really const, because we permute them.  We pretend they're const in the prototype to  be  compatible  with  othersystems.The use of '+' and '-' in optstring is a GNU extension.On  some older implementations, getopt() was declared in <stdio.h>.  SUSv1 permitted the declaration to appear in either<unistd.h> or <stdio.h>.  POSIX.1-2001 marked the use of <stdio.h> for this purpose as LEGACY.   POSIX.1-2001  does  notallow the declaration to appear in <stdio.h>.getopt_long() and getopt_long_only():These functions are GNU extensions.

##简解

这个东西会对argv中的参数进行解析.
首先碰到- 然后对-之后的解析.然后返回-之后的东西. 
如果继续调用,则会返回第二个-之后的东西
//注意:不解析不以-开始的参数,直解析以-开始的参数在解析,其实上面说的不全对,返回的不一定是-之后的东西,他会判定-之后的东西是否在getopt函数的第三个参数中,如果有,则返回,如果没有,则会打印出invalid option -- 'p' ,然后返回一个 ?第三个参数可以为"?hvVDarsuwAJSF:f:" ,f 和 F 必须要加参数 ,有参数 返回f,无参数返回?
//即如果后面有:,则表示要加参数
另外optind是已经被解析的参数个个数+1,即包括命令
而且getopt之后会对argv里面的参数重新排序

##实验代码
点我下载

这篇关于getopt解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1008064

相关文章

SQL 外键Foreign Key全解析

《SQL外键ForeignKey全解析》外键是数据库表中的一列(或一组列),用于​​建立两个表之间的关联关系​​,外键的值必须匹配另一个表的主键(PrimaryKey)或唯一约束(UniqueCo... 目录1. 什么是外键?​​ ​​​​2. 外键的语法​​​​3. 外键的约束行为​​​​4. 多列外键​

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

Maven 插件配置分层架构深度解析

《Maven插件配置分层架构深度解析》:本文主要介绍Maven插件配置分层架构深度解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Maven 插件配置分层架构深度解析引言:当构建逻辑遇上复杂配置第一章 Maven插件配置的三重境界1.1 插件配置的拓扑

全解析CSS Grid 的 auto-fill 和 auto-fit 内容自适应

《全解析CSSGrid的auto-fill和auto-fit内容自适应》:本文主要介绍了全解析CSSGrid的auto-fill和auto-fit内容自适应的相关资料,详细内容请阅读本文,希望能对你有所帮助... css  Grid 的 auto-fill 和 auto-fit/* 父元素 */.gri

Maven 依赖发布与仓库治理的过程解析

《Maven依赖发布与仓库治理的过程解析》:本文主要介绍Maven依赖发布与仓库治理的过程解析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录Maven 依赖发布与仓库治理引言第一章:distributionManagement配置的工程化实践1

MySQL复合查询从基础到多表关联与高级技巧全解析

《MySQL复合查询从基础到多表关联与高级技巧全解析》本文主要讲解了在MySQL中的复合查询,下面是关于本文章所需要数据的建表语句,感兴趣的朋友跟随小编一起看看吧... 目录前言:1.基本查询回顾:1.1.查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J1.2.按照部门

Spring三级缓存解决循环依赖的解析过程

《Spring三级缓存解决循环依赖的解析过程》:本文主要介绍Spring三级缓存解决循环依赖的解析过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、循环依赖场景二、三级缓存定义三、解决流程(以ServiceA和ServiceB为例)四、关键机制详解五、设计约

Redis实现分布式锁全解析之从原理到实践过程

《Redis实现分布式锁全解析之从原理到实践过程》:本文主要介绍Redis实现分布式锁全解析之从原理到实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、背景介绍二、解决方案(一)使用 SETNX 命令(二)设置锁的过期时间(三)解决锁的误删问题(四)Re

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义