Comment detail

起動オプションの解析 (Nested Flatten)
とりあえず getopt()を利用したものです。
コンパイラによってはgetopt()が unistd.hにはいっているかもしれません。

僕はいつも起動情報を保持する構造体を作って保持しています。
has~()という関数作ってしまえばあんまり関係ないですけど。。
// gcc -Wall -std=c99 doukaku205.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
#include <getopt.h>

#define OPTION_OUTPUT 'o'
#define OPTION_QUOTE  'q'
#define OPTION_DEBUG  'd'

static char options[] = { ':', 
                     OPTION_OUTPUT
                     , OPTION_QUOTE
                     , OPTION_DEBUG, ':'
                    , '\0' };

struct tagParamInfo
{
    bool output;
    bool quote;
    int  debug;
    int  num;
    char *argv0;
    char **argv;
} info = { false, false, 0, 0, NULL, NULL};

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

    /* デフォル値 設定 */

    /* オプション解析 */
    opterr = 0;
    while( (opt = getopt(argc, argv, options)) != EOF )
    {
        switch( opt )
        {
            case OPTION_OUTPUT:
                info.output = true;
                break;
            case OPTION_QUOTE:
                info.quote = true;
                break;
            case OPTION_DEBUG:
                info.debug = strtol(optarg, NULL,10);
                if( 0 <= info.debug && info.debug <= 2 )
                {
                    break;
                }
            case '?':
            case ':':
            default:
                printf("変なオプション[%c]\n", opt);
                printf("書式:cmdopt -o [-q] [-d{0|1|2}] 文字列 [文字列 ...]\n");
                return 1;
        }
    }

    /* パラメータ設定 */
    info.argv0 = argv[0];
    info.argv  = &argv[optind];
    info.num = argc - optind;
    if( info.num < 1 )
    {
        printf("文字列がない\n");
        printf("書式:cmdopt -o [-q] [-d{0|1|2}] 文字列 [文字列 ...]\n");
        return 1;
    }

    /* 解析結果の出力 */
    printf("[オプション情報]\n");
    printf("%-10s:%3s\n", "o(output)", ((info.output==true)?"ON":"OFF"));
    printf("%-10s:%3s\n", "q(quote)", ((info.quote==true)?"ON":"OFF"));
    printf("%-10s:%3d\n", "d(debug)", info.debug);
    printf("\n");
    printf("[パラメータ情報]\n");
    printf("%-10s:%3d\n", "指定数", info.num);
    for( int n=0; n<info.num; n++)
    {
        printf("%2d:%s\n", n, info.argv[n]);
    }
    return 0;
}
sh版作っていて-oオプションのチェックが抜けていたことに気づくorz
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
--- doukaku205.c        2008-09-10 22:23:39.953125000 +0900
+++ doukaku205.c.new    2008-09-10 22:43:58.656250000 +0900
@@ -61,6 +61,14 @@
     info.argv0 = argv[0];
     info.argv  = &argv[optind];
     info.num = argc - optind;
+
+    /* 必須チェック */
+    if( info.output != true )
+    {
+        printf("必須オプションがたりない\n");
+        printf("書式:cmdopt -o [-q] [-d{0|1|2}] 文字列 [文字列 ...]\n");
+        return 1;
+    }
     if( info.num < 1 )
     {
         printf("文字列がない\n");

Index

Feed

Other

Link

Pathtraq

loading...