Chinaunix首页 | 论坛 | 博客
  • 博客访问: 928647
  • 博文数量: 403
  • 博客积分: 27
  • 博客等级: 民兵
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-25 22:20
文章分类

全部博文(403)

文章存档

2016年(3)

2015年(16)

2014年(163)

2013年(222)

分类: LINUX

2013-05-12 13:21:24

Linux命令行约定)

 

几乎所有的GNU/Linux程序都遵循一些命令行参数定义的约定。程序希望出现的参数可以分成两种:选项(options or flags)、其他类型的的参数。Options修饰了程序运行的方式,其他类型的参数则提供了输入(例如,输入文件的名称)。

 

对于options类型参数可以有两种方式:

1)短选项(short options:顾名思义,就是短小参数。它们通常包含一个连字号和一个字母(大写或小写字母)。例如:-s-h等。

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

*注:一个程序通常会提供包括short optionslong options两种参数形式的参数。

 

对于其他类型参数的说明:

这种类型的参数,通常跟随在options类型参数之后。例如,ls –s /功能为显示root目录的大小。’/’这个参数告诉ls要显示目录的路径。

 

2.Using getopt_long

 

1)         getopt_long()函数说明

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)使用getopt_long()的几条建议

²        调用方法

(a)我们先看一下在函数库里,getopt_long()函数是如何被声明的:

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

(b)说明一下这五个变量吧。

前两个就不必多说了,main函数的参数。第三个和第四个变量,分别是上面讲到的准备的两个数据结构。最后一个参数:longindex参数一般赋为NULL即可;如果没有设置为NULL,那么它就指向一个变量,这个变量会被赋值为寻找到的长选项在longopts中的索引值,这可以用于错误诊断。

(c)举例说明一下:

按以上所讲,参照(1)准备的两个数据结构,则调用方式可为:

getopt_long( argc, argv, short_options, long_options, NULL);

 

²        几种常见返回值

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

(b)如果getopt_long()在分析选项时,遇到一个没有定义过的选项,则返回值为‘?’,此时,程序员可以打印出所定义命令行的使用信息给用户。

(c)当处理一个带参数的选项时,全局变量optarg会指向它的参数

(d)当函数分析完所有参数时,全局变量optindinto argv)会指向第一个‘非选项’的位置

 

3)更多内容可以参照

http://www.cublog.cn/u/18537/showart.php?id=125055

 

2)       应用举例

 

/*===============================================================

* Code listing from "Advanced Linux Programming," by CodeSourcery LLC  *
===============================================================*/

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

/* The name of this program. */
const char* program_name;

/* Prints usage information for this program to STREAM (typically
   stdout or stderr), and exit the program with EXIT_CODE. Does not
   return. */

void print_usage (FILE* stream, int exit_code)
{
  fprintf (stream, "Usage: %s options [ inputfile ... ]\n", program_name);
  fprintf (stream,
           " -h --help Display this usage information.\n"
           " -o --output filename Write output to file.\n"
           " -v --verbose Print verbose messages.\n");
  exit (exit_code);
}

/* Main program entry point. ARGC conains number of argument list
   elements; ARGV is an array of pointers to them. */

int main (int argc, char* argv[])
{
  int next_option;

  /* A string listing valid short options letters. */
  const char* const short_options = "ho:v";
  /* An array describing valid long options. */
  const struct option long_options[] = {
    { "help", 0, NULL, 'h' },
    { "output", 1, NULL, 'o' },
    { "verbose", 0, NULL, 'v' },
    { NULL, 0, NULL, 0 } /* Required at end of array. */
  };

  /* The name of the file to receive program output, or NULL for
     standard output. */
  const char* output_filename = NULL;
  /* Whether to display verbose messages. */
  int verbose = 0;

  /* Remember the name of the program, to incorporate in messages.
     The name is stored in argv[0]. */
  program_name = argv[0];

  do {
    next_option = getopt_long (argc, argv, short_options,
                               long_options, NULL);
    switch (next_option)
    {
    case 'h': /* -h or --help */
      /* User has requested usage information. Print it to standard
         output, and exit with exit code zero (normal termination). */
      print_usage (stdout, 0);

    case 'o': /* -o or --output */
      /* This option takes an argument, the name of the output file. */
      output_filename = optarg;
      break;

    case 'v': /* -v or --verbose */
      verbose = 1;
      break;

    case '?': /* The user specified an invalid option. */
      /* Print usage information to standard error, and exit with exit
         code one (indicating abonormal termination). */
      print_usage (stderr, 1);

    case -1: /* Done with options. */
      break;

    default: /* Something else: unexpected. */
      abort ();
    }
  }
  while (next_option != -1);

  /* Done with options. OPTIND points to first non-option argument.
     For demonstration purposes, print them if the verbose option was
     specified. */
  if (verbose) {
    int i;
    for (i = optind; i < argc; ++i)
      printf ("Argument: %s\n", argv[i]);
  }
  /* The main program goes here. */
  return 0;
}

阅读(591) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~