Linux command line parameter analysis

In linux, often requires a variety of command, under normal circumstances will bring a variety of parameters, and these parameters is how to resolve it?
Using function getopt GNU C provided, getopt_long, getopt_long_only function to parse the command line parameters.
They need to refer to the use of header files getopt.h.

Original Address: https://www.cnblogs.com/NickQ/p/11368656.html

1. getopt () function

the getopt () is used to parse the command-line option parameter, but only to resolve short options: -d 100, can not resolve long options: - prefix; prototype:

int getopt(int argc, char * const argv[],const char *optstring);

argc and argv same parameters and two main functions, respectively, an array of strings to save the number of parameters and the command line parameters;
the optstring, short statement option rules, such as form "a:b::cd:", respectively, supports command-line options are short - a, -b, -c, -d, colon following meanings:

  1. Only one character, without a colon - only that options such as -c.
  2. A character, a colon followed - after the option represents a parameter band, or as -a 100 -a100.
  3. A character, followed by two colons - indicates that the following option with an optional parameter, the parameter is optional, if the arguments, the options and parameters directly can not have spaces forms, such as -b200 type should be able to explain here. Why sometimes with parameters need space because space is sometimes really necessary. well understood, it does not need the space because of its optional parameters, if there are spaces can not determine whether it carries parameters.
  4. Sometimes appear shaped like :a:b::cd:optstring, the first character is a colon indicate if the parameter resolution process, the emergence of the situation parameter is missing, the return value return ":" instead of "?." The difference is easy to follow program options parameter error or error;

According to the principles of analytic function return value, the function calling the cycle, until the complete parameter parsing function (return value is -1).
Specific Description The return value is:

  1. If the option is successfully found, the return option letters;
  2. If you have not optstring option character, returns the character '?';
  3. If you encounter missing parameters, the return value depends on optstring the first character, if the first character is ':' Returns ':', otherwise it returns an error message and prompt '?'.
  4. If all the command-line options are parsed completely, return -1;

Further, in the getopt.h also it defines several global variables as follows.

char *optarg —— 指向当前选项参数(如果有参数的话)的指针。ps: 当前选项由返回值传递;
int optind —— 再次调用 getopt() 时的下一个 argv 指针的索引。
int optopt —— 最后一个未知选项。一般当出现未知选项,或缺少参数时,它就会保存这个未知选项。
int opterr ­—— 如果不希望getopt()打印出错信息,则只要将该全局变量opterr设为0即可。

2. getopt_long () function

getopt_long () is to expand on the basis of the getopt () on, the size parameter can be acquired, such as acquisition parameters --help. The prototype is as follows:

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

Similarly, the same parameters argc and argv and two main functions, respectively, an array of strings to save the number of parameters and the command line parameters; the optstring, short statement rules options;
except that getopt_long () has two long parsed parameter variables used;

  • longopts parameter variable is used to describe options to resolve the long program, you need to call well defined before; for example,
static struct option longopts[] = {
    {"help", no_argument, NULL, 'h'},
    {"module", required_argument, NULL, 'm'},
    {"set", required_argument, NULL, 's'},
    {"get", no_argument, NULL, 'g'},
    {"wakelock", required_argument, NULL, 'l'},
    {"wakeunlock", required_argument, NULL, 'u'},
    {0, 0, 0, 0}
};

这个option结构体,定义如下:
struct option
{
    const char *name;//长选项名
    int has_arg;//是否需要参数
    int *flag;
    int val;
};
  1. has_arg indicate whether the parameter value with which the value Optional:
    no_argument show long options without parameters, such as: - name, --help
    required_argument parameters indicate that option must be long, such as: - prefix / root or --prefix = / the root
    optional_argument parameter indicating length option is optional, such as: - help or --prefix = / root, other are wrong.
  2. val flag and interdependent, mainly two cases:
    . (. 1) flag is NULL, val value for determining whether the long option, it is necessary to specify a unique value val length option. Here also builds a bridge to long options and short options.
    (2). Flag is not NULL, the value val is stored in the flag memory space pointed, for identifying the long option appeared.
  • longindex parameter variables. If longindex not empty, it points to the record of the current variable parameter fit the description longopts found in the first few elements, that is, the index value longopts.

3. getopt_long_only function

And getopt_long function uses the same parameter table, basically the same function, just as long getopt_long only --name parameter, but getopt_long_only will --name and -name Both options as long parameters to match. If the option -name not match longopts but can match a short option, it is parsed as a short option.

Guess you like

Origin www.cnblogs.com/NickQ/p/11368656.html