getopt(),getopt_long(),getopt_long_only()
来源:互联网 发布:最近的网络热歌有哪些 编辑:程序博客网 时间:2024/06/06 03:25
man 3 getopt
NAME
getopt, getopt_long, getopt_long_only - Parse command-line options
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#define _GNU_SOURCE
The getopt() function parses the command-line arguments. Its arguments argc and argv are the argument count and array as
passed 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() is called repeatedly, it returns successively each of the option characters from each of the option elements.
********************************************************************
The getopt_long() function works like getopt() except that it also accepts long options, started with two dashes. (If the
program accepts only long options, then optstring should be specified as an empty string (""), not NULL.) Long option
names may be 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> as
struct 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_arg
is: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an
argument; 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 exam-
ple, the calling program may set val to the equivalent short option character.) Otherwise, getopt_long() returns 0,
and flag points 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 zeroes.
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.
getopt_long例子参考
http://blog.csdn.net/ast_224/article/details/3861625
NAME
getopt, getopt_long, getopt_long_only - Parse command-line options
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#define _GNU_SOURCE
The getopt() function parses the command-line arguments. Its arguments argc and argv are the argument count and array as
passed 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() is called repeatedly, it returns successively each of the option characters from each of the option elements.
#include <unistd.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { int opt; char* short_options="nt:"; while ((opt = getopt(argc, argv,short_options)) != -1) { switch (opt) { case 'n': printf("option n\n"); break; case 't': printf("option t,optarg=%s\n",optarg); printf("str to int:%d\n",atoi(optarg)); break; default: fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n",argv[0]); } } exit(EXIT_SUCCESS); }
[root@localhost test]# ./test1 -t 66 -noption t,optarg=66str to int:66option n
********************************************************************
The getopt_long() function works like getopt() except that it also accepts long options, started with two dashes. (If the
program accepts only long options, then optstring should be specified as an empty string (""), not NULL.) Long option
names may be 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> as
struct 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_arg
is: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an
argument; 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 exam-
ple, the calling program may set val to the equivalent short option character.) Otherwise, getopt_long() returns 0,
and flag points 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 zeroes.
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.
getopt_long例子参考
http://blog.csdn.net/ast_224/article/details/3861625
- getopt();getopt_long();getopt_long_only();option
- getopt(),getopt_long(),getopt_long_only()
- getopt、getopt_long和getopt_long_only
- getopt();getopt_long();getopt_long_only();option
- getopt、getopt_long和getopt_long_only
- getopt(),getopt_long(),getopt_long_only()
- getopt、getopt_long和getopt_long_only
- getopt、getopt_long、getopt_long_only详解
- getopt、getopt_long、getopt_long_only
- getopt、getopt_long、getopt_long_only
- getopt()/getopt_long()/getopt_long_only()
- getopt、getopt_long和getopt_long_only
- getopt、getopt_long、getopt_long_only使用实例
- 浅析getopt()与getopt_long()与getopt_long_only()
- getopt、getopt_long和getopt_long_only解析命令行参数
- 使用 getopt() 、getopt_long()、getopt_long_only()进行命令行处理
- linux 输入参数利用getopt、struct option、getopt_long、getopt_long_only解析
- 命令行选项解析函数(C语言):getopt()、getopt_long()和getopt_long_only
- 【Perl】 system/exec/readpipe区别
- 源代码网站集合
- C++基本要点
- my_simple_RAII
- 常用社交网络(SNS、人人网、新浪微博)动态新闻(feed、新鲜事、好友动态)系统浅析
- getopt(),getopt_long(),getopt_long_only()
- C语言指针的运算
- KINECT开源项目:Open KINECT介绍 与 Open KINECT源码下载
- Error: failed PB timebomb check
- C++ 求两数的最大公约数
- regular dll 模块切换
- C语言题目归类分析
- ASCII编码与中文互转
- WAS61安装调整和应用部署.doc