命令行參数选项处理:getopt()及getopt_long()函数使用
当 gcc的程序启动代码调用我们的入口函数 main(int argc,char *argv[]) 时,已经对命令行进行了处理。argc 參数包括程序參数的个数,而
argv 包括指向这些參数的指针数组。
程序的參数能够分为三种:选项。选项的关联值,非选项參数。
比如:
getopt_test.c是非选项參数。-o是选项,testopt是-o选项的关联值。
依据Linux的惯例。程序的选项应该以一个短横线开头。后面包括单个字母或数字。选项分为:带关联值的和不带关联值的以及可选的。不带关联值的选项能够在一个短横线后合并使用,比如
ls -al。
此外还有长选项。有两个短横线来指明。比方说 -o filename --output filename 给定输出文件名称等,以下整理了一些国外的资源用来学习。
getopt():短选项处理
int getopt( int argc, char *const argv[], const char *optstring );
假设一个选项须要一个关联值。而程序运行时没有提供,返回一个问号(?),假设将optstring的第一个字符设为冒号(:),这样的情况下,函数会返回冒号而不是问号。
实际上。getopt在运行过程中会重排argv数组,将非选项參数移到数组的尾部。
{
= 0;
= 0;
= ":vV:h:" ; //
= 0; i < argc; i++)
i, argv[i]);
= getopt (argc, argv, optstring)) !=
-1){
%c:the Version is %s\n" , opt, optarg);
option %c is %s...\n" , opt, optarg);
option %c\n" ,optopt);
%d \n" , optind);
= 0; i < argc; i++)
i, argv[i]);
getopt_long():长选项处理
Decode options from the vector argv (whose length is argc). The argument shortopts describes the short options to accept, just as it does in getopt. The argument longopts describes
the long options to accept (see above).
When getopt_long encounters a short option, it does the same thing that getopt would do: it returns the character code for the option, and stores the options argument (if it has
one) inoptarg.
When getopt_long encounters a long option, it takes actions based on the flag and val fields of the definition of that option.
If flag is a null pointer, then getopt_long returns the contents of val to indicate which option it found. You should arrange distinct values in the val field
for options with different meanings, so you can decode these values after getopt_long returns. If the long option is equivalent to a short option, you can use the short option's character code in val.
If flag is not a null pointer, that means this option should just set a flag in the program. The flag is a variable of type int that you define. Put the address of the flag in the flag field.
Put in the val field the value you would like this option to store in the flag. In this case, getopt_long returns 0.
For any long option, getopt_long tells you the index in the array longopts of the options definition, by storing it into *indexptr. You can get the name of the option withlongopts[*indexptr].name.
So you can distinguish among long options either by the values in their val fields or by their indices. You can also distinguish in this way among long options that set flags.
When a long option has an argument, getopt_long puts the argument value in the variable optarg before returning. When the option has no argument, the value in optarg is a null pointer. This is
how you can tell whether an optional argument was supplied.
getopt_long has no more options to handle, it returns -1, and leaves in the variable optind the index in argv of the next remaining argument.struct option {
char *name; //长选项的名字
int has_arg; // 0/1。标志是否有选项
int *flag; //上面有具体说明,通常为NULL
int val;
};
containing all zeros.
and optional_argument.
= "vhVo:" ;
long_options[] = {
NULL, 'v' },
NULL, 'h' },
NULL, 'V' },
NULL, 'o' },
end of array. */
{
argv, short_options, long_options, NULL);//
== -1) {
{
usage of this program...\n" );
the program's log verbose...\n");
version is 0.1 ...\n" );
output file is %s.\n" ,optarg);
' :
option , abort the program.");
unexpected
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdm9uemhvdWZ6/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
应用场景分析
memory exhausted\n" );
= malloc (size ? size : 1);
== NULL) {
= xmalloc(length + 1);
p_, length);
memory.
caller is
options[]){
* 3 + 1];
= short_options;
options-> name; options++) {
*o = options;
NULL && o-> val > 0 && o-> val <=
UCHAR_MAX) {
{
{
UCHAR_MAX + 1,
long_options[] = {
NULL, 'v' },
NULL, 'h' },
NULL, 'V' },
NULL, OPT_TIMESTAMP },
= long_options_to_short_options(long_options);
{
命令行參数选项处理:getopt()及getopt_long()函数使用的更多相关文章
- python 命令行參数解析
本文是从我还有一个博客转载过来的,欢迎大家点击进去看一下,帮我添加点人气^_^ ImPyy 选择模块 依据python參考手冊的提示,optparse 已经废弃,应使用 argparse 教程 概念 ...
- Rust 1.7.0 处理命令行參数
std是 Rust 标准函数库: env 模块提供了处理环境函数. 在使用标准函数库的时候,使用 use 导入对应的 module . 一.直接输出 use std::env; fn main(){ ...
- VS2010中使用命令行參数
在Linux下编程习惯了使用命令行參数,故使用VS2010时也尝试了一下. 新建项目,c++编敲代码例如以下: #include<iostream> #include<fstream ...
- 第8章2节《MonkeyRunner源代码剖析》MonkeyRunner启动执行过程-解析处理命令行參数
MonkeyRunnerStarter是MonkeyRunner启动时的入口类,由于它里面包括了main方法.它的整个启动过程主要做了以下几件事情: 解析用户启动MonkeyRunner时从命令行传输 ...
- Python命令行參数大全
-b : 当转换数组为字符串时提出警告.比方str(bytes_instance), str(bytearray_instance). -B : 当导入.py[co]文 ...
- python命令行參数解析实例
闲言少述,直接上代码 #!/usr/bin/env python # # import json import getopt, sys def usage(): print sys.argv[ ...
- 命令行参数解析函数getopt和getopt_long函数【转】
原文地址:http://blog.csdn.net/cashey1991/article/details/7942809 getopt和getopt_long函数 平时在写程序时常常需要对命令行参 ...
- C语言中getopt()和getopt_long()函数的用法
一.参考文章 1.C语言中getopt()和getopt_long()函数的用法 2.linux 中解析命令行参数 (getopt_long用法) 二.调试经验
- Shell 参数(2) --解析命令行参数工具:getopts/getopt
getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...
随机推荐
- 最长公共子序列python实现
最长公共子序列是动态规划基本题目,以下依照动态规划基本步骤解出来. 1.找出最优解的性质,并刻划其结构特征 序列a共同拥有m个元素,序列b共同拥有n个元素,假设a[m-1]==b[n-1],那么a[: ...
- 关于Smartforms换页的
smartforms中有系统变量SFSY-PAGE是总页码,SFSY-FORMPAGES是当前页,可以最后的窗体中做个判断 1.把窗体设置成最终窗体 2.新增一个命令,当前页等于最后一页才输出改内容, ...
- TCP/IP协议的编写《转载》
基于HHARM9-EDU的TCP/IP(UDP)协议的实现 原文网址:http://blog.csdn.net/lhj0503/article/details/3323788 摘 要:嵌入式技术的发展 ...
- linux系统日志及其rsyslog服务
日志是系统用来记录系统运行时候的一些相关消息的纯文本文件 /var/log下保存着大量的纯文本日志文件 日志的目的是为了保持相关程序的运行状态,错误消息,为了对系统运行进行错误分析使用 1.内核消息 ...
- HDU4712-----Hamming Distance------超级大水题
本文出自:http://blog.csdn.net/dr5459 题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题目意思: 海明距离:任意两个 ...
- HDU 2152 Fruit (母函数)
# include<stdio.h> # include <algorithm> # include <string.h> # include <iostre ...
- flask开发restful api
flask开发restful api 如果有几个原因可以让你爱上flask这个极其灵活的库,我想蓝图绝对应该算上一个,部署蓝图以后,你会发现整个程序结构非常清晰,模块之间相互不影响.蓝图对restfu ...
- XPSP2 PSDK(还有lostspeed)
XPSP2 PSDK Full Download with Local Install Use the full download to copy the entire Windows XP SP2 ...
- html name id, 与服务器交互必须有name
html name id, 与服务器交互必须有name 在HTML中元素的ID和Name的区别和联系. 今天写了个测试,在php脚本里怎么也获取不到$_POST['userName']的值,经检查在h ...
- jQuery遍历函数
jQuery遍历函数包含了用于筛选.查找和串联元素的方法. .add():将元素加入到匹配元素的集合中. .andSelf():把堆栈中之前的元素集加入到当前集合中. .children():获得匹配 ...