[PR] この広告は3ヶ月以上更新がないため表示されています。
ホームページを更新後24時間以内に表示されなくなります。

getopt_long

"getopt"は一文字だけのオプションでしたが,
"getopt_long"はロングオプションを対応させることができます.

with line numberplain text
  1. #include <unistd.h>
  2. #include <getopt.h>
  3.  
  4. int getopt_long(int argc, char * const argv[],
  5.     const char *optstring,
  6.     const struct option *longopts, int *longindex);
  7.  
  8. int getopt_long_only(int argc, char * const argv[],
  9.     const char *optstring,
  10.     const struct option *longopts, int *longindex);
#include <unistd.h>
#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);


getopt_long Example

with line numberplain text
  1. /* long_option.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <getopt.h>
  6.  
  7. void print_help(void);
  8. void plus(void);
  9.  
  10. struct option long_options[] = {
  11.     {"help",  no_argument, 0, 'h'},
  12.     {"math",  no_argument, 0, 'm'},
  13.     {0, 0, 0, 0},
  14. };
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.     int opt, opt_index = 0;
  19.     int help_flag = 0;
  20.     int math_flag = 0;
  21.  
  22.     if(argc < 2)
  23.         print_help();
  24.  
  25.     while((opt = getopt_long(argc, argv, "hma:", long_options, &opt_index)) != EOF) {
  26.         switch(opt) {
  27.             case 'h':
  28.                 help_flag = 1;
  29.                 break;
  30.             case 'm':
  31.                 math_flag = 1;
  32.                 break;
  33.             case '?':
  34.                 help_flag = 1;
  35.                 break;
  36.             default:
  37.                 help_flag = 1;
  38.                 break;
  39.         }
  40.     }
  41.  
  42.     if(help_flag)
  43.         print_help();
  44.  
  45.     else
  46.         if(math_flag)
  47.             plus();
  48.  
  49.     return 0;
  50. }
  51.  
  52. void print_help(void) {
  53.     printf("Usage: ./long_opt [OPTION]\n");
  54.     printf("-m, --math : 加算\n");
  55.     printf("-h, --help : ヘルプを表示\n");
  56.     exit(EXIT_SUCCESS);
  57. }
  58.  
  59. void plus(void) {
  60.     int na, nb, nc;
  61.     char tmp[32];
  62.  
  63.     printf("Number A : ");
  64.     fgets(tmp, 32, stdin);
  65.     na = atoi(tmp);
  66.  
  67.     printf("Number B : ");
  68.     fgets(tmp, 32, stdin);
  69.     nb = atoi(tmp);
  70.  
  71.     nc = na + nb;
  72.  
  73.     printf("%d + %d = %d\n", na ,nb, nc);
  74. }
/* long_option.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

void print_help(void);
void plus(void);

struct option long_options[] = {
    {"help",  no_argument, 0, 'h'},
    {"math",  no_argument, 0, 'm'},
    {0, 0, 0, 0},
};

int main(int argc, char **argv)
{
    int opt, opt_index = 0;
    int help_flag = 0;
    int math_flag = 0;

    if(argc < 2)
        print_help();

    while((opt = getopt_long(argc, argv, "hma:", long_options, &opt_index)) != EOF) {
        switch(opt) {
            case 'h':
                help_flag = 1;
                break;
            case 'm':
                math_flag = 1;
                break;
            case '?':
                help_flag = 1;
                break;
            default:
                help_flag = 1;
                break;
        }
    }

    if(help_flag)
        print_help();

    else
        if(math_flag)
            plus();

    return 0;
}

void print_help(void) {
    printf("Usage: ./long_opt [OPTION]\n");
    printf("-m, --math : 加算\n");
    printf("-h, --help : ヘルプを表示\n");
    exit(EXIT_SUCCESS);
}

void plus(void) {
    int na, nb, nc;
    char tmp[32];

    printf("Number A : ");
    fgets(tmp, 32, stdin);
    na = atoi(tmp);

    printf("Number B : ");
    fgets(tmp, 32, stdin);
    nb = atoi(tmp);

    nc = na + nb;

    printf("%d + %d = %d\n", na ,nb, nc);
}
下記の部分がプログラムのメイン
加算する関数とかあるけどぶっちゃけなんでもいい
with line numberplain text
  1. struct option long_options[] = {
  2.     {"help",  no_argument, 0, 'h'},
  3.     {"math",  no_argument, 0, 'm'},
  4.     {0, 0, 0, 0},
  5. };
  6.  
  7. ...
  8.  
  9. while((opt = getopt_long(argc, argv, "hma:", long_options, &opt_index)) != EOF) {
  10.     switch(opt) {
  11.     case 'h':
  12.         help_flag = 1;
  13.         break;
  14.         
  15.         ...
  16.     }
  17. }
struct option long_options[] = {
    {"help",  no_argument, 0, 'h'},
    {"math",  no_argument, 0, 'm'},
    {0, 0, 0, 0},
};

...

while((opt = getopt_long(argc, argv, "hma:", long_options, &opt_index)) != EOF) {
    switch(opt) {
    case 'h':
        help_flag = 1;
        break;
        
        ...
    }
}
"getopt_long"関数の第4引数に"long_options"構造体を参照しています.
構造体の第1変数は(char)型の変数, オプションの名前
    第2変数は(int)型の変数, "argment"を必要とするかどうか.
    第3変数は(int)*型の変数, ロングオプションに対する結果の返し方の指定.
    第4変数は(int)型の変数, 返り値, またはflagがポイントする変数へロードされる値

内容はほぼ"getopt"と変わらない. ロングオプションをつけたい場合にのみ使用してください.



実行例

$ ./long --math
Number A : 3
Number B : 4
3 + 4 = 7
$ ./long --help
Usage: ./long_opt [OPTION]
-m, --math : 加算
-h, --help : ヘルプを表示