linux获取命令行参数struct option

时间:2022-04-18 11:09:58

写得很好的一篇博文,适合对于命令行参数不甚了解的linux初学者。

在原文上摘取一些自己需要的片段,后半段关于非GNU的部分没有细看,以目前的水平也看得云里雾里的。待以后再细细拜读。

原文地址:http://blog.csdn.net/yui/article/details/5669922


可以使用getopt()或者getopt_long()来获得输入的参数。

两者的一个区别在于getopt()只支持短格式参数,而getopt_long()既支持短格式参数,又支持长格式参数。

短格式:./destroy -f cdr -o cdr.txt -c cdr.desc -k 123456

长格式:./destroy --file cdr --output cdr.txt --config cdr.desc --keyword 123456


引入了getopt()getopt_long()的项目,设计者可以按需要,方便地增加参数,或者随意地放置参数的先后次序,只需要在程序中判断,哪些参数是必须的就可以了

附件destroy_linux.c给出了在Linux下使用getopt_long()的实例。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

void print_usage(const char *program_name) {
printf("%s 1.0.0 (2010-06-13)/n", program_name);
printf("This is a program decoding a BER encoded CDR file/n");
printf("Usage: %s -f <file_name> -o <output_name> [-c <config_name>] [-k <keyword>]/n", program_name);
printf(" -f --file the CDR file to be decoded/n");
printf(" -o --output the output file in plain text format/n");
printf(" -c --config the description file of the CDR file, if not given, use default configuration/n");
printf(" -k --keyword the keyword to search, if not given, all records will be written into output file/n");
}

int main(int argc, char *argv[]) {
char *file_name = NULL;
char *output_name = NULL;
char *config_name = NULL;
char *keyword = NULL;

const char *short_opts = "hf:o:c:k:";
const struct option long_opts[] = {
{"help", no_argument, NULL, 'h'},
{"file", required_argument, NULL, 'f'},
{"output", required_argument, NULL, 'o'},
{"config", required_argument, NULL, 'c'},
{"keyword", required_argument, NULL, 'k'},
{0, 0, 0, 0}
};
int hflag = 0;

int c;
opterr = 0;

while ( (c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1 ) {
switch ( c ) {
case 'h' :
hflag = 1;
break;
case 'f' :
file_name = optarg;
break;
case 'o' :
output_name = optarg;
break;
case 'c' :
config_name = optarg;
break;
case 'k' :
keyword = optarg;
break;
case '?' :
if ( optopt == 'f' || optopt == 'o' || optopt == 'c' || optopt == 'k' )
printf("Error: option -%c requires an argument/n", optopt);
else if ( isprint(optopt) )
printf("Error: unknown option '-%c'/n", optopt);
else
printf("Error: unknown option character '//x%x'/n", optopt);
return 1;
default :
abort();
}
}

if ( hflag || argc == 1 ) {
print_usage(argv[0]);
return 0;
}
if ( !file_name ) {
printf("Error: file name must be specified/n");
return 1;
}
if ( !output_name ) {
printf("Error: output name must be specified/n");
return 1;
}

// if not setting default, Linux OK, but SunOS core dump
if ( !config_name ) config_name = "(null)";
if ( !keyword ) keyword = "(null)";
printf("Parameters got: file_name = %s, output_name = %s, config_name = %s, keyword = %s/n", file_name, output_name, config_name, keyword);
return 0;
}
另外一个区别是,getopt()几乎通用于所有类Unix系统,而getopt_long()只有在GNUUnix/Linux下才能用。