目录
命令行参数
getopt() / getopt_long() 均用来对命令行参数进行解析。getopt()处理参数,getopt_long()用来处理长参数。
图1 命令行参数
命令行参数由Command name,Option,Option argument 以及Operands组成。
Command name:程序的名称。
Option:选项,它是用来用来决定程序的行为。
Option argument:选项参数,是选项Option所需要的信息。
Operands:操作对象,可以为NULL。
举例:
./a.out -a abc
//Command name:a.out
//Option: a
//Option argument: abc
//Operands: null
./a.out -a abc def
//Command name:a.out
//Option: a
//Option argument: abc
//Operands: def
getopt()函数
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
getopt函数的参数
- 参数argc和argv:通常是从main的参数直接传递而来,argc是命令行参数的数量,argv是一个常量字符串数组的地址。
- argc为命令行参数个数,包含了Command name,Option,Option argument 以及Operands。
- argv[0]为程序名;argv[1]到argv[argc -1]为该程序命令行参数;argv[argc]为空。
- 参数optstring:一个包含正确选项字符的字符串。
- 单个字符,表示该选项Option不需要参数。"p"
- 单个字符后接一个冒号":",表示该选项Option需要一个选项参数Option argument。选项参数Option argument可以紧跟在选项Option之后,或者以空格隔开。选项参数Option argument的首地址赋给optarg。"p:"
- 单个字符后接两个冒号"::",表示该选项Option的选项参数Option argument是可选的。当提供了Option argument时,必须紧跟Option之后,不能以空格隔开,否则getopt()会认为该选项Option没有选项参数Option argument,optarg赋值为NULL。相反,提供了选项参数Option argument,则optarg指向Option argument。"p::"
- 首字符为”:”时,当发生无法识别的选项错误时getopt()返回字符"?",当发生丢失选项参数错误时返回字符":"。
全局变量:
为了使用getopt(),我们需要在while循环中不断地调用直到其返回-1为止。每一次调用,当getopt()找到一个有效的选项Option的时候就会返回这个Option字符,并设置几个全局变量。
- char *optarg
当匹配一个选项后,如果该选项带选项参数,则optarg指向选项参数的字符串;若该选项不带选项参数,则optarg为NULL;若该选项的选项参数为可选时,optarg为NULL表明无选项参数,optarg不为NULL时则指向选项参数字符串。
- int optind
下一个待处理元素在argv中的索引值。即下一次调用getopt的时候,从optind存储的位置处开始扫描选项。当getopt()返回-1后,optind是argv中第一个Operands(最后一个选项参数后紧跟的第一个参数,可以是NULL)参数的索引值。optind的初始值为1。
当getopt函数在while循环中使用时,剩下的字符串为操作数,下标从optind到argc-1
- int opterr
opterr的值非0时,在getopt()遇到无法识别的选项,或者某个选项丢失选项参数的时候,getopt()会打印错误信息到标准错误输出。opterr值为0时,则不打印错误信息。
- int optopt
使用getopt()时,会犯的错误无外乎有两个:无法识别的选项(Invalid option) 和丢失选项参数(Missing option argument)
通常情况下,getopt()在发现这两个错误时,会打印相应的错误信息,并且返回字符"?" 。例如,遇见无法识别的选项时会打印"invalid option",发现丢失参数时打印"option requires an argument"。但是当设置opterr为0时,则不会打印这些信息,因此为了便于发现错误,默认情况下,opterr都是非零值。在上述两种错误之一发生时,一般情况下getopt()会返回'?',并且将optopt赋值为发生错误的选项。
如果你想亲自处理这两种错误的话,应该怎么做呢? getopt()允许我们设置optstring的首字符为冒号":",在这种情况下,当发生无法识别的选项错误时getopt()返回字符"?",当发生丢失选项参数错误时返回字符":"。这样我们就可以很轻松地分辨出错误类型了,不过代价是getopt()不会再打印错误信息了。
实例使用:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int flags = 0, opt;
int nsecs = 0, tfnd = 0;
while((opt = getopt(argc, argv, "nt:")) != -1) {
switch (opt) {
case 'n':
flags = 1;
break;
case 't':
nsecs = atoi(optarg);
tfnd = 1;
break;
default:
printf("optopt = %c\n", (char)optopt);
printf("opterr = %d\n", opterr);
fprintf(stderr, "usage: %s [-t nsecs] [-n] name\n", argv[0]);
//exit(EXIT_SUCCESS )等价于exit(0)表示安全退出
//exit(EXIT_FAILURE)等价于exit(1)表示异常退出
exit(EXIT_FAILURE);
}
}
printf("flags = %d; tfnd = %d; nsecs = %d; optind = %d\n", flags, tfnd, nsecs, optind);
printf("optind = %d\n", optind);
printf("argc = %d\n", argc);
#if 1
if(optind >= argc) {
fprintf(stderr, "Expected argument after options\n");
exit(1);
}
#endif
printf("name argument = %s\n", argv[optind]);
/* Other code omitted */
return 0;
}
运行结果
:~$ ./a.out -a name //首先 -a 是一个非法的选项
./a.out: invalid option -- 'a' //输出错误信息,因为非法选项
optind = 2 //此时argv[optind]是我们的操作数,也就是我们传递给主函数的参数
optopt = a //当发现无效项字符时,optopt会包含该字符,正如我们传递的‘a’这个无效项。
opterr = 1 //opterr变量非零,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
usage: ./a.out [-t nsecs] [-n] name
======================================================================================
:~$ ./a.out -t 123 -n //此时,根据字符串”-nt:“应该得知,-t 后应加参数,-n不用加
flags = 1; tfnd = 1; nsecs = 123; optind = 4 //在switch语句中将flags=1,tfnd=1,因为optarg变量保存这当前选项参数的字符串,因此此时optarg保存的是‘-t’参数的参数并用atoi函数转称整数,因此nsecs=123。
optind = 4 //argv[optind]为空,因为没有向主函数传递参数
argc = 4 //一共四个字符串
Expected argument after options //该程序希望我们至少传递一个参数,因此在if语句中退出。
=======================================================================================
:~$ ./a.out -t 123 -n hello world hello C
optind = 3
optind = 4
flags = 1; tfnd = 1; nsecs = 123; optind = 4
optind = 4
argc = 8
name argument = hello
getopt_long()函数
#include <getopt.h>
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
getopt_long函数的参数
- argc和argv:通常是从main的参数直接传递而来,argc是命令行参数的数量,argv是一个常量字符串数组的地址。
- optstring: 表示短选项字符串。
形式如“a:b::cd:“,分别表示程序支持的命令行短选项有-a、-b、-c、-d。
- longopts:表示长选项结构体。
struct option { const char *name; int has_arg; int *flag; int val; }; eg: static struct option longOpts[] = { { "daemon", no_argument, NULL, 'D' }, { "out", required_argument, NULL, 'o' }, { "log", required_argument, NULL, 'l' }, { "http-proxy", required_argument, &lopt, 1 }, { "version", no_argument, NULL, 'v' }, { "help", no_argument, NULL, 'h' }, { 0, 0, 0, 0 } };
(1)name:表示选项的名称,比如daemon,dir,out等。
(2)has_arg:表示选项后面是否携带参数。该参数有三个不同值,如下:
- no_argument(或者是0)时 ——参数后面不跟参数值,eg: --version,--help
- required_argument(或者是1)时 ——参数输入格式为:--参数 值 或者 --参数=值。eg:--dir=/home 或者 --dir /home
- optional_argument(或者是2)时 ——参数输入格式只能为:--参数=值
(3)flag:这个参数有两个意思,空或者非空。
- 如果参数为空NULL,则识别选项后getopt_long返回val。eg,可执行程序 --help,getopt_long的返回值为h.
- 如果参数不为空,那么当选中某个长选项的时候,getopt_long将返回0,并且将flag指针指向val值。eg: 可执行程序 --http-proxy=127.0.0.1:80 那么getopt_long返回值为0,并且lopt值为1。
(4)val:表示指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值val。
ps:
1、如果使用getopt_long想只接受短选项,设置longopts为NULL即可;如果只想接受长选项,相应地设置optstring为NULL即可;
2、长选项名是可以使用缩写方式,比如:选项有--file\--create,那么输入--c/--cr/--cre等均会被正确识别为create选项
- longindex:longindex非空,则是返回识别到struct option数组中元素的位置指针;
- 返回值:
- 如果短选项找到,那么将返回短选项对应的字符。
- 如果长选项找到,如果flag为NULL,返回val。如果flag不为空,返回0
- 如果遇到一个选项没有在短字符、长字符里面。或者在长字符里面存在二义性的,返回“?”
- 如果解析完所有字符没有找到(一般是输入命令参数格式错误,eg: 连斜杠都没有加的选项),返回“-1”
- 如果选项需要参数,忘了添加参数。返回值取决于optstring,如果其第一个字符是“:”,则返回“:”,否则返回“?”。
实例使用
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h>
int main(int argc, char **argv)
{
int c;
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 }
};
c = getopt_long(argc, argv, "abc:d:012",
long_options, &option_index);
if (c == -1)
break;
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);
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
exit(EXIT_SUCCESS);
}
#include<stdio.h>
#include <getopt.h>
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
void showUsage(char* progname) {
printf("Usage: %s [OPTION]...\n", progname);
printf(" Options:\n");
printf(" -d, --dir=DIR The directory to store downloaded file.\n");
printf(" -o, --out=FILE The file name for downloaded file.\n");
printf(" -l, --log=LOG The file path to store log. If '-' is specified\n");
printf(" -D, --daemon Run as daemon.\n");
printf(" --http-proxy=HOST:PORT Use HTTP proxy server. This affects to all.\n");
printf(" --http-passwd=PASSWD Set HTTP password. This affects to all URLs.\n");
printf(" --http-proxy-user=USER Set HTTP proxy user. This affects to all URLs.\n");
//***省略****
printf(" -v, --version Print the version number and exit.\n");
printf(" -h, --help Print this message and exit\n");
return;
}
int main(int argc, char* argv[]) {
while(1) {
int lopt;
static struct option longOpts[] = {
{ "daemon", no_argument, NULL, 'D' },
{ "dir", required_argument, NULL, 'd' },
{ "out", required_argument, NULL, 'o' },
{ "log", required_argument, NULL, 'l' },
{ "split", required_argument, NULL, 's' },
{ "http-proxy", required_argument, &lopt, 1 },
{ "http-user", required_argument, &lopt, 2 },
{ "http-passwd", required_argument, &lopt, 3 },
{ "http-proxy-user", required_argument, &lopt, 4 },
{ "http-proxy-passwd", required_argument, &lopt, 5 },
{ "http-auth-scheme", required_argument, &lopt, 6 },
{ "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "Dd:o:l:s:vh", longOpts, NULL);
printf("返回值: %c\n",c);
if(c == -1) {
break;
}
switch(c) {
case 0:{
switch(lopt) {
case 1: {
printf("1: %s\n",optarg);
break;
}
case 2:
printf("2: %s\n",optarg);
break;
case 3:
printf("3: %s\n",optarg);
break;
case 4:
printf("4: %s\n",optarg);
break;
case 5:
printf("5: %s\n",optarg);
break;
case 6:
printf("6: %s\n",optarg);
break;
}
break;
}
case 'D':
printf("D: %s\n",optarg);
break;
case 'd':
printf("d: %s\n",optarg);
break;
case 'o':
printf("o: %s\n",optarg);
break;
case 'l':
printf("l: %s\n",optarg);
break;
case 's':
printf("s: %s\n",optarg);
break;
case 'v':
printf("s: %s\n",optarg);
//showVersion();
exit(0);
case 'h':
showUsage(argv[0]);
exit(0);
default:
showUsage(argv[0]);
exit(1);
}
}
return 0;
}