002-getopt_long()

2023-11-29 12:15
文章标签 long getopt 002

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

1-代码及注释

        这个例程是man手册里的。

1-1 getopt_long.c

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>
/*   
man 3 getopt_long
函数原型:int getopt_long(int argc, char * const argv[],const char *optstring,const struct option *longopts, int *longindex);
*/int main(int argc, char **argv){int c;//c在下面作为getopt_long()的返回值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 }};printf("\noptind:%d\n", optind);c = getopt_long(argc, argv, "abc:d:012",long_options, &option_index);//getopt_long(argc, argv,  optstring ,longopts, longindex); //1-argc:main()函数传递过来的参数的个数//2-argv:main()函数传递过来的参数的字符串指针数组//3-optstring:选项字符串,告知 getopt()可以处理哪个选项以及哪个选项需要参数// char*optstring = “ab:c::”;// 单个字符 a         表示选项a没有参数            格式:-a即可,不加参数// 单字符加冒号 b:     表示选项b有且必须加参数      格式:-b 100或-b100,但-b=100错// 单字符加2冒号 c::   表示选项c可以有,也可以无     格式:-c200,其它格式错误//4-longopts指明了长参数的名称和属性//   struct option//     {//     const char *name;// 参数名称//     /* Has_arg不能是enum,因为有些编译器会抱怨在所有假定它是int型的代码中存在类型不匹配。  *///     int has_arg;//指明是否带有参数//     int *flag;// flag=NULL时,返回value;不为空时,*flag=val,返回0//     int val;// 用于指定函数找到选项的返回值或flag非空时指定*flag的值//     };//5-longindex 如果 longindex 非空,//它指向的变量将记录当前找到参数符合 longopts 里的第几个元素的描述,//即是 longopts 的下标值if (c == -1)//如果所有命令行选项都解析完毕,getopt_long()返回 -1;break;//退出while(1)循环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);}}//optind 再次调用 getopt() 时的下一个 argv 指针的索引。printf("\nargc:%d ", argc);printf("\noptind:%d\n", optind);if (optind < argc) {printf("non-option ARGV-elements: ");while (optind < argc)printf("%s ", argv[optind++]);printf("\n");}exit(EXIT_SUCCESS);}

1-2 getopt_core.h(linux系统自带)

/* Declarations for getopt (basic, portable features only).Copyright (C) 1989-2020 Free Software Foundation, Inc.This file is part of the GNU C Library and is also part of gnulib.Patches to this file should be submitted to both projects.The GNU C Library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.The GNU C Library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with the GNU C Library; if not, see<https://www.gnu.org/licenses/>.  */#ifndef _GETOPT_CORE_H
#define _GETOPT_CORE_H 1/* This header should not be used directly; include getopt.h orunistd.h instead.  Unlike most bits headers, it does not havea protective #error, because the guard macro for getopt.h ingnulib is not fixed.  */__BEGIN_DECLS/* For communication from 'getopt' to the caller.When 'getopt' finds an option that takes an argument,the argument value is returned here.Also, when 'ordering' is RETURN_IN_ORDER,each non-option ARGV-element is returned here.  */extern char *optarg;/* Index in ARGV of the next element to be scanned.This is used for communication to and from the callerand for communication between successive calls to 'getopt'.On entry to 'getopt', zero means this is the first call; initialize.When 'getopt' returns -1, this is the index of the first of thenon-option elements that the caller should itself scan.Otherwise, 'optind' communicates from one call to the nexthow much of ARGV has been scanned so far.  */extern int optind;/* Callers store zero here to inhibit the error message 'getopt' printsfor unrecognized options.  */extern int opterr;/* Set to an option character which was unrecognized.  */extern int optopt;/* Get definitions and prototypes for functions to process thearguments in ARGV (ARGC of them, minus the program name) foroptions given in OPTS.Return the option character from OPTS just read.  Return -1 whenthere are no more options.  For unrecognized options, or optionsmissing arguments, 'optopt' is set to the option letter, and '?' isreturned.The OPTS string is a list of characters which are recognized optionletters, optionally followed by colons, specifying that that lettertakes an argument, to be placed in 'optarg'.If a letter in OPTS is followed by two colons, its argument isoptional.  This behavior is specific to the GNU 'getopt'.The argument '--' causes premature termination of argumentscanning, explicitly telling 'getopt' that there are no moreoptions.If OPTS begins with '-', then non-option arguments are treated asarguments to the option '\1'.  This behavior is specific to the GNU'getopt'.  If OPTS begins with '+', or POSIXLY_CORRECT is set inthe environment, then do not permute arguments.For standards compliance, the 'argv' argument has the typechar *const *, but this is inaccurate; if argument permutation isenabled, the argv array (not the strings it points to) must bewritable.  */extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)__THROW __nonnull ((2, 3));__END_DECLS#endif /* getopt_core.h */

2 执行结果

        编译1-1中getopt_long.c,执行可执行文件即可。

        根据执行结果分析代码。

这篇关于002-getopt_long()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/432793

相关文章

JavaSE-易错题集-002

1. 下面有关java基本类型的默认值和取值范围,说法错误的是? A 字节型的类型默认值是0,取值范围是-2^7—2^7-1 B boolean类型默认值是false,取值范围是true\false C 字符型类型默认是0,取值范围是-2^15 —2^15-1 D long类型默认是0,取值范围是-2^63—2^63-1 答案:C 题解:注意字符型(char) char 占16位,

long long,_int64使用小结

前言:   在16位环境下,int/unsigned int 占16位,long/unsigned long占32位   在32位环境下,int占32位,unsigned int占16位,long/unsigned long占32位 何时需要使用:   long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647,而unsigned范围是[0,2^32),

《长得太长也是错?——后端 Long 型 ID 精度丢失的“奇妙”修复之旅》

引言 在前后端分离的时代,我们的生活充满了无数的机遇与挑战——包括那些突然冒出来的让人抓狂的 Bug。今天我们要聊的,就是一个让无数开发者哭笑不得的经典问题:后端 Long 类型 ID 过长导致前端精度丢失。说到这个问题,那可真是“万恶之源”啊,谁让 JavaScript 只能安全地处理 Number.MAX_SAFE_INTEGER(也就是 9007199254740991)以内的数值呢?

​​NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南002

继续,执行pom.xml引入依赖以后,发现以下几种报错: 可以看到在下载aws-java-sdk-bundle 1.12.710版本的时候报错了 可以看到日志信息,就是在阿里云上下载的,因为阿里云上缺少这个jar包 aws-java-sdk-bundle-1.12.710.jar 这个jar包,我还特意去阿里云上查询了一下 https://developer.aliyun.com/

踩坑记录(Long[]ids)

主要针对Long[] ids 的判空问题 问题代码 public void delYnjC(Long[] ids) {if (CollectionUtils.isEmpty(Collections.singleton(ids))) {throw new NullPointerException("参数不能为空");}naturalYnjCMapper.delYnjC(ids);} 修正

CodeForces 407B Long Path

题意: 有n+1个格子  起点在1  每个格子有个前进1的门  前n个格子有个返回的门(返回前面某个格子)  奇数次走进一个格子就去走返回门  偶数次走前进门  问  走到n+1要走过几道门 思路: 一看就是DP to[i]表示第i个格子的返回门 go[i]表示离开第i个格子需要的步数 sum[i]表示离开前i个格子需要的步数 明显  go[i]=sum[i-1]-sum[to

Command line is too long. Shorten command line for DisplayApplication (1) or

微服务项目启动类起不来,如下 解决办法:IEDA开发环境下 找到你的项目下面的.idea\workspace.xml 添加一个property : <property name="dynamic.classpath" value="true" /> 帮同事看的问题,自己测试没问题就关闭了。图片借用网上的。 参考:参考

[C++] 将LONG类型的color值转换为RGB值

转换原理: The calculation is: (65536 * Blue) + (256 * Green) + (Red) 'Convert RGB to LONG: LONG = B * 65536 + G * 256 + R       'Convert LONG to RGB:  B = LONG \ 65536  G = (LONG - B * 65536) \ 256  R =

002线性逻辑结构——线性表

目录 1.数据之间的逻辑关系 2.存储结构的实现 2.1顺序存储结构实现线性表: 对该顺序表进行一系列的增删改查: ①增:         Ⅰ)顺序添加:         Ⅱ)插入添加: ②删:       ③改:       ④查:       输出 整体代码示例 1.数据之间的逻辑关系 线性表:具有相同数据类型的有限个(n)数据元素的序列