您的位置:首页 > 运维架构 > Linux

linux 中解析命令行参数 (getopt_long用法)

2010-01-10 20:52 501 查看
我们在linux下面的程序开发,很多时候都会遇到命令行处理,命令行参数处理这样一种控制台程序,它能够很明了的给用户程序出程序的使用方法,也能够使我们的程序开发的流程变得更加明了。下面我们就一起来讨论一下,我们平时如何处理这种命令的输入参数,其实这个就跟readline的处理差不多,我们如果掌握了这种固有的模式,那你以后每个程序有这方面的需求都可以直接套用了,大大提高了我们的开发效率。
一.模式框架讲解

本想完整的写一篇文章,但发现一是man手册讲解太详细了,还有很详细的实例,再就是根据man翻译过来的哥们文章太多,故这部分内容我也就直接转载了一位网友的部分翻译,本部分内容转载自:http://blog.csdn.net/ast_224/archive/2009/02/04/3861625.aspx
getopt_long支持长选项的命令行解析,使用man getopt_long,得到其声明如下:
#include <getopt.h>

int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);

int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);


说明:函数中的argc和argv通常直接从main()到两个参数传递而来。optsting是选项参数组成的字符串,如

果该字符串里任一字母后有冒号,那么这个选项就要求有参数。下一个参数是指向数组的指针,这个数组是

option结构数组,option结构称为长选项表,其声明如下:

struct option {
const char *name;
int has_arg;
int *flag;
int val;
};

结构中的元素解释如下:
const char *name:选项名,前面没有短横线。譬如"help"、"verbose"之类。
int has_arg:描述长选项是否有选项参数,如果有,是哪种类型的参数,其值见下表:
符号常量 数值 含义
no_argument 0 选项没有参数
required_argument 1 选项需要参数
optional_argument 2 选项参数是可选的
int *flag:
如果该指针为NULL,那么getopt_long返回val字段的值;
如果该指针不为NULL,那么会使得它所指向的结构填入val字段的值,同时getopt_long返回0
int val:
如果flag是NULL,那么val通常是个字符常量,如果短选项和长选项一致,那么该字符就应该与optstring中

出现的这个选项的参数相同;

最后一个参数:longindex参数一般赋为NULL即可;如果没有设置为NULL,那么它就指向一个变量,这个变量

会被赋值为寻找到的长选项在longopts中的索引值,这可以用于错误诊断。

注:GNU提供的getopt-long()和getopt-long-only()函数,其中,后者的长选项字串是以一个短横线开始的

,而非一对短横线。

linux 命令行约定:
几乎所有的GNU/Linux程序都遵循一些命令行参数定义的约定。程序希望出现的参数可以分成两种:选

项(options or flags)、其他类型的的参数。Options修饰了程序运行的方式,其他类型的参数则提供了输

入(例如,输入文件的名称)。

对于options类型参数可以有两种方式:
1)短选项(short options):顾名思义,就是短小参数。它们通常包含一个连字号和一个字母(大写

或小写字母)。例如:-s,-h等。
2)长选项(long options):长选项,包含了两个连字号和一些大小写字母组成的单词。例如,--

size,--help等。
*注:一个程序通常会提供包括short options和long options两种参数形式的参数。

对于其他类型参数的说明:
这种类型的参数,通常跟随在options类型参数之后。例如,ls –s /功能为显示root目录的大小。’/

’这个参数告诉ls要显示目录的路径。

getopt_long()函数使用规则:

(1)使用前准备两种数据结构
字符指针型变量
该数据结构包括了所有要定义的短选项,每一个选项都只用单个字母表示。如果该选项需要参数(如,

需要文件路径等),则其后跟一个冒号。例如,三个短选项分别为‘-h’‘-o’‘-v’,其中-o需要参数,

其他两个不需要参数。那么,我们可以将数据结构定义成如下形式:
const char * const shor_options = “ho:v” ;

struct option 类型数组
该数据结构中的每个元素对应了一个长选项,并且每个元素是由四个域组成。通常情况下,可以按以下

规则使用。第一个元素,描述长选项的名称;第二个选项,代表该选项是否需要跟着参数,需要参数则为1,

反之为0;第三个选项,可以赋为NULL;第四个选项,是该长选项对应的短选项名称。另外,数据结构的最后

一个元素,要求所有域的内容均为0,即{NULL,0,NULL,0}。下面举例说明,还是按照短选项为‘-h’‘-o’

‘-v’的例子,该数据结构可以定义成如下形式:
const struct option long_options = {
{ “help”, 0, NULL, ‘h’ },
{ “output”, 1, NULL, ‘o’ },
{ “verbose”, 0, NULL, ‘v’ },
{ NULL, 0, NULL, 0 }
};

(2)调用方法
参照(1)准备的两个数据结构,则调用方式可为:
getopt_long( argc, argv, short_options, long_options, NULL);

(3)几种常见返回值
(a)每次调用该函数,它都会分析一个选项,并且返回它的短选项,如果分析完毕,即已经没有选项了,

则会返回-1。
(b)如果getopt_long()在分析选项时,遇到一个没有定义过的选项,则返回值为‘?’,此时,程序员可

以打印出所定义命令行的使用信息给用户。
(c)当处理一个带参数的选项时,全局变量optarg会指向它的参数
(d)当函数分析完所有参数时,全局变量optind(into argv)会指向第一个‘非选项’的位置

实践小例子:

#include <stdio.h>
#include <getopt.h>
char *l_opt_arg;
char* const short_options = "nbl:";
struct option long_options[] = {
{ "name",     0,   NULL,    'n'     },
{ "bf_name",  0,   NULL,    'b'     },
{ "love",     1,   NULL,    'l'     },
{      0,     0,     0,     0},
};
int main(int argc, char *argv[])
{
int c;
while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)
{
switch (c)
{
case 'n':
printf("My name is XL./n");
break;
case 'b':
printf("His name is ST./n");
break;
case 'l':
l_opt_arg = optarg;
printf("Our love is %s!/n", l_opt_arg);
break;
}
}
return 0;
}


编译并运行:

[root@localhost liuxltest]# gcc -o getopt getopt.c

[root@localhost liuxltest]# ./getopt -n -b -l forever
My name is XL.
His name is ST.
Our love is forever!
[root@localhost liuxltest]#

[root@localhost liuxltest]# ./getopt -nb -l forever
My name is XL.
His name is ST.
Our love is forever!
[root@localhost liuxltest]# ./getopt -nbl forever
My name is XL.
His name is ST.
Our love is forever!

二.实例一:usb_modeswitch中的命令行处理

/*
*以下部分的结果处理和各种变更的定义都省略了,我们只关心与命令行处理相关的东西。
*/
.............

static struct option long_options[] = {
{"help",				no_argument, 0, 'h'},
{"version",				no_argument, 0, 'e'},
{"default-vendor",		required_argument, 0, 'v'},
{"default-product",		required_argument, 0, 'p'},
{"target-vendor",		required_argument, 0, 'V'},
{"target-product",		required_argument, 0, 'P'},
{"target-class",		required_argument, 0, 'C'},
{"message-endpoint",	required_argument, 0, 'm'},
{"message-content",		required_argument, 0, 'M'},
{"response-endpoint",	required_argument, 0, 'r'},
{"detach-only",			no_argument, 0, 'd'},
{"huawei-mode",			no_argument, 0, 'H'},
{"sierra-mode",			no_argument, 0, 'S'},
{"sony-mode",			no_argument, 0, 'O'},
{"gct-mode",			no_argument, 0, 'G'},
{"need-response",		no_argument, 0, 'n'},
{"reset-usb",			no_argument, 0, 'R'},
{"config",				required_argument, 0, 'c'},
{"verbose",				no_argument, 0, 'W'},
{"quiet",				no_argument, 0, 'Q'},
{"no-inquire",			no_argument, 0, 'I'},
{"check-success",		required_argument, 0, 's'},
{"interface",			required_argument, 0, 'i'},
{"configuration",		required_argument, 0, 'u'},
{"altsetting",			required_argument, 0, 'a'},
{0, 0, 0, 0}
};

...................

int readArguments(int argc, char **argv)
{
int c, option_index = 0, count=0;
if(argc==1) return 0;

while (1)
{
c = getopt_long (argc, argv, "heWQndHSOGRIv:p:V:P:C:m:M:r:c:i:u:a:s:",
long_options, &option_index);

/* Detect the end of the options. */
if (c == -1)
break;
count++;
switch (c)
{
case 'R': ResetUSB = 1; break;
case 'v': DefaultVendor = strtol(optarg, NULL, 16); break;
case 'p': DefaultProduct = strtol(optarg, NULL, 16); break;
case 'V': TargetVendor = strtol(optarg, NULL, 16); break;
case 'P': TargetProduct = strtol(optarg, NULL, 16); break;
case 'C': TargetClass = strtol(optarg, NULL, 16); break;
case 'm': MessageEndpoint = strtol(optarg, NULL, 16); break;
case 'M': strcpy(MessageContent, optarg); break;
case 'n': NeedResponse = 1; break;
case 'r': ResponseEndpoint = strtol(optarg, NULL, 16); break;
case 'd': DetachStorageOnly = 1; break;
case 'H': HuaweiMode = 1; break;
case 'S': SierraMode = 1; break;
case 'O': SonyMode = 1; break;
case 'G': GCTMode = 1; break;
case 'c': readConfigFile(optarg); break;
case 'W': verbose = 1; show_progress = 1; count--; break;
case 'Q': show_progress = 0; verbose = 0; count--; break;
case 's': CheckSuccess = strtol(optarg, NULL, 16); count--; break;
case 'I': InquireDevice = 0; break;

case 'i': Interface = strtol(optarg, NULL, 16); break;
case 'u': Configuration = strtol(optarg, NULL, 16); break;
case 'a': AltSetting = strtol(optarg, NULL, 16); break;

case 'e':
printVersion();
exit(0);
break;
case 'h':
printVersion();
printf ("Usage: usb_modeswitch [-hvpVPmMrdHn] [-c filename]/n/n");
printf (" -h, --help                    this help/n");
printf (" -e, --version                 print version number and exit/n");
printf (" -v, --default-vendor [nr]     vendor ID to look for (mandatory)/n");
printf (" -p, --default-product [nr]    product ID to look for (mandatory)/n");
printf (" -V, --target-vendor [nr]      target vendor (optional, for success check)/n");
printf (" -P, --target-product [nr]     target model (optional, for success check)/n");
printf (" -C, --target-class [nr]       target device class/n");
printf (" -m, --message-endpoint [nr]   where to direct the message (optional)/n");
printf (" -M, --message-content [str]   command to send (hex number as string)/n");
printf (" -n, --need-response           read a response to the message transfer/n");
printf (" -r, --response-endpoint [nr]  where from read the response (optional)/n");
printf (" -d, --detach-only             just detach the storage driver/n");
printf (" -H, --huawei-mode             apply a special procedure/n");
printf (" -S, --sierra-mode             apply a special procedure/n");
printf (" -O, --sony-mode               apply a special procedure/n");
printf (" -G, --gct-mode                apply a special procedure/n");
printf (" -R, --reset-usb               reset the device in the end/n");
printf (" -c, --config [filename]       load different config file/n");
printf (" -Q, --quiet                   don't show progress or error messages/n");
printf (" -W, --verbose                 print all settings before running/n");
printf (" -s, --success [nr]            check switching result after [nr] secs/n");
printf (" -I, --no-inquire              do not get device details (default on)/n/n");
printf (" -i, --interface [nr]          select initial USB interface (default 0)/n");
printf (" -u, --configuration [nr]      select USB configuration/n");
printf (" -a, --altsetting [nr]         select alternative USB interface setting/n/n");
exit(0);
break;

default: //Unsupported - error message has already been printed
printf ("/n");
exit(1);
}
}

return count;
}

int main(int argc, char **argv)
{

.......................

// Check command arguments, use params instead of config file when given
switch (readArguments(argc, argv)) {
case 0:						// no argument or -W, -q or -s
readConfigFile("/etc/usb_modeswitch.conf");//这里的读取配置文件,将是下一篇文章讲的
break;
default:					// one or more arguments except -W, -q or -s
if (!config_read)		// if arguments contain -c, the config file was already processed
if (verbose) printf("Taking all parameters from the command line/n/n");
}
.......................
processing...
/*
*	根据readArguments函数中变局变量的结果,进行相应的处理。
*/
.......................
}


[b]
[b]说明:其实简单的命令行处理使用这种getopt/getopt_long的方式完全足够,而且简易方便。当然如果自己还想在此基础上加入历史记录,自动补全等等都需要自己加上这种辅助显示了。


[/b]

[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: