Linuxオプション解釈-getoptとgetopt_long関数


Linuxオプション解釈-getoptとgetopt_long関数
一、コマンドラインの概要
解釈解析コマンドラインは通常、argcおよびargvパラメータによってそのコマンドラインパラメータにアクセスするC言語の最初のタスクである.
最も簡単なコマンドライン処理技術はif判断で表すことができ、以下の例である.
if(argc>1&&argv[1][0]="-'&&argv[1][1]=="h")//コマンドラインパラメータが-nであるか否かを判断
{
     do _ some thing();
}
このように単純で秩序あるコマンドラインを処理することも可能であり、複雑なコマンドライン処理には無力であるため、GNUはコマンドラインパラメータを処理するために2つの関数を提供する:getoptとgetopt_long.
二、getopt関数
getopt()関数は次のように宣言されます.
#include <unistd.h>

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

extern char *optarg;

extern int optind, opterr, optopt;
 
 
説明:関数のargcとargvは通常main()から2つのパラメータに直接伝達されます.optstingはオプションパラメータからなる文字列であり、その文字列のいずれかのアルファベットの後にコロンがある場合、このオプションにはパラメータが必要であり、optargはオプションパラメータである.optindは現在のインデックスであり、optoptは無効なオプション文字が発見されたときにgetopt関数または「?」を返すために使用されます.または「:」文字を返し、optoptには検出された無効なオプション文字が含まれています.
optstringパラメータの最初の文字がコロンである場合、getoptはエラーに応じて異なる文字を返し、エラーが無効なオプションである場合、getoptは「?」を返します.エラーがオプションパラメータが欠落している場合、getoptは「:」を返します.
注意:GNU getopt()の3つ目の特徴は、optstringのオプション文字の後ろに2つのコロンが付いていることです.このオプションにはオプションのオプションパラメータがあります.オプションパラメータが存在しない場合、GNU getopt()はオプション文字を返し、optargをNULLに設定します.
例:
#include <stdio.h>

#include <unistd.h>

#include <getopt.h>

char *para = ":ab:c";

int main(int argc, char *argv[])

{

     int oc = -1;

     char *b_input = NULL;

     while((oc = getopt(argc, argv, para)) != -1)

     {

         switch(oc)

         {

          case 'a':

              printf("input para is a
"); break; case 'b': b_input = optarg; printf("input para is b,and optarg is %s
", b_input); break; case 'c': printf("input para is c
"); break; case ':': printf("option %c requires an argument
",optopt); break; case '?': default: printf("option %c is invalid:ignored
",optopt); break; } } return 0; }
 
 
コンパイル:
[root@heguangwu projects]# gcc -o getopt_ex getopt_ex.c
実行:
[root@heguangwu projects]# ./getopt_ex -a
input para is a
[root@heguangwu projects]# ./getopt_ex -a -b
input para is a
option b requires an argument
[root@heguangwu projects]# ./getopt_ex -d
option d is invalid:ignored
 
三、getopt_long関数
getopt_longは長いオプションを処理するために使用され、man 3 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);
 
 
最初の3つのパラメータはgetoptと同じで、次のパラメータは配列を指すポインタで、この配列はoption構造配列であり、option構造は長オプションテーブルと呼ばれ、以下のように宣言されています.
struct option
 {

    const char *name;

    int  has_arg;

    int  *flag;

    int  val;

};
 
 
構造内の要素は次のように解釈されます.
const char*name:オプション名、前に短い横線はありません
int has_Arg:長オプションにパラメータがあるかどうかを説明します.その値は次の表を参照してください.
シンボル定数

意味
no_argument required_argument optional_argument
0 1 2
オプションパラメータなしオプションパラメータが必要オプション
int *flag:
ポインタがNULLの場合、getopt_longはvalフィールドの値を返します.
ポインタがNULLでない場合、そのポインタが指す構造はvalフィールドの値を入力し、getopt_longは0を返す
int val:
flagがNULLの場合、valは通常文字定数であり、短いオプションと長いオプションが一致する場合、この文字はoptstringに現れるこのオプションのパラメータと同じであるべきである.
 
 
#include <stdio.h>

#include <unistd.h>

#include <getopt.h>

char *para = ":ab:cf:v";

int do_all = 0;

int do_help = 0; 

int do_version = 0;

char *file = NULL;

struct option longopt[] = 

{

     {"all", no_argument, &do_all, 1},

     {"file", required_argument, NULL, 'f'},

     {"help", no_argument, &do_help, 1},

     {"version", no_argument, &do_version, 1},

     {"bob", required_argument, NULL, 'b'},

     {0, 0, 0, 0},

};

int main(int argc, char *argv[])

{

    int oc = -1;

    char *b_input = NULL;

    while((oc = getopt_long(argc, argv, para, longopt, NULL)) != -1)

    {

         switch(oc)

         {

         case 'a':

               printf("input para is a
"); break; case 'b': b_input = optarg; printf("input para is b,and optarg is %s
", b_input); break; case 'c': printf("input para is c
"); break; case 'v': printf("input para is v
"); break; case 'f': printf("input para is f
"); file = "hello world"; break; case 0: break; case ':': printf("option %c requires an argument
",optopt); break; case '?': default: printf("option %c is invalid:ignored
",optopt); break; } } printf("do_all is %d
",do_all); printf("do_help is %d
",do_help); printf("do_version is %d
",do_version); printf("do_file is %s
",file); printf("bob is %s
", b_input); return 0; }
 
 
 
実行結果:キー結果のみ表示
[root@heguangwu projects]# ./opt_ex2 -a
input para is a
 
[root@heguangwu projects]# ./opt_ex2 --all
do_all is 1
 
[root@heguangwu projects]# ./opt_ex2 -f h
input para is f
do_file is hello world
 
[root@heguangwu projects]# ./opt_ex2 --bob aa
input para is b,and optarg is aa
bob is aa
 
[root@heguangwu projects]# ./opt_ex2 -b aa
       input para is b,and optarg is aa