File: optparse.h

package info (click to toggle)
simstring 1.0-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 1,376 kB
  • sloc: cpp: 15,653; sh: 3,846; ansic: 488; python: 205; makefile: 50; ruby: 22
file content (273 lines) | stat: -rw-r--r-- 8,293 bytes parent folder | download | duplicates (2)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 *      An event-driven parser for command-line arguments.
 *  
 *      Copyright (c) 2004-2005 by Naoaki Okazaki
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions (known as zlib license):
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 * Naoaki Okazaki <okazaki at chokkan dot org>
 *
 */

/* $Id: optparse.h 2 2009-07-08 05:44:37Z naoaki $ */

/*
 * Class 'optparse' implements a parser for GNU-style command-line arguments.
 * Inherit this class to define your own option variables and to implement an
 * option handler with macros, BEGIN_OPTION_MAP, ON_OPTION(_WITH_ARG), and
 * END_OPTION_MAP. Consult the sample program attached at the bottom of this
 * source code.
 *
 * This code was comfirmed to be compiled with MCVC++ 2003 and gcc 3.3.
 * Define _BUILD_NCL_SAMPLE if you want to build a sample program.
 *  $ g++ -D_BUILD_NCL_SAMPLE -xc++ optparse.h
 */

#ifndef __NCL_OPTPRASE_H__
#define __NCL_OPTPRASE_H__

#include <cstring>
#include <sstream>
#include <stdexcept>
#include <string>


#ifdef  USE_NCL_NAMESPACE
namespace ncl {
#endif/*USE_NCL_NAMESPACE*/


/**
 * An event-driven parser for command-line arguments.
 *  @author Naoaki Okazaki
 */
class optparse {
public:
    /**
     * Exception class for unrecognized options.
     */
    class unrecognized_option : public std::invalid_argument {
    public:
        unrecognized_option(char shortopt)
            : std::invalid_argument(std::string("-") + shortopt) {}
        unrecognized_option(const std::string& longopt)
            : std::invalid_argument(std::string("--") + longopt) {}
    };
    /**
     * Exception class for invalid values.
     */
    class invalid_value : public std::invalid_argument {
    public:
        invalid_value(const std::string& message)
            : std::invalid_argument(message) {}
    };

public:
    /** Construct. */
    optparse() {}
    /** Destruct. */
    virtual ~optparse() {}

    /**
     * Parse options.
     *  @param  argv        array of null-terminated strings to be parsed
     *  @param  num_argv    specifies the number, in strings, of the array
     *  @return             the number of used arguments
     *  @throws             optparse_exception
     */
    int parse(char * const argv[], int num_argv)
    {
        int i;
        for (i = 1;i < num_argv;++i) {
            const char *token = argv[i];
            if (*token++ == '-') {
                const char *next_token = (i+1 < num_argv) ? argv[i+1] : "";
                if (!*token) {
                    break;  // only '-' was found.
                } else if (*token == '-') {
                    const char *arg = std::strchr(++token, '=');
                    if (arg) {
                        arg++;
                    } else {
                        arg = next_token;
                    }
                    int ret = handle_option(0, token, arg);
                    if (ret < 0) {
                        throw unrecognized_option(token);
                    }
                    if (arg == next_token) {
                        i += ret;
                    }
                } else {
                    char c;
                    while ((c = *token++) != '\0') {
                        const char *arg = *token ? token : next_token;
                        int ret = handle_option(c, token, arg);
                        if (ret < 0) {
                            throw unrecognized_option(c);
                        }
                        if (ret > 0) {
                            if (arg == token) {
                                token = "";
                            } else {
                                i++;
                            }
                        }
                    } // while
                } // else (*token == '-') 
            } else {
                break;  // a non-option argument was fonud.
            } 
        } // for (i)

        return i;
    }

protected:
    /**
     * Option handler
     *  This function should be overridden by inheritance class.
     *  @param  c           short option character, 0 for long option
     *  @param  longname    long option name
     *  @param  arg         an argument for the option
     *  @return             0 (success);
                            1 (success with use of an argument);
                            -1 (failed, unrecognized option)
     *  @throws             option_parser_exception
     */
    virtual int handle_option(char c, const char *longname, const char *arg)
    {
        return 0;
    }

    int __optstrcmp(const char *option, const char *longname)
    {
        const char *p = std::strchr(option, '=');
        return p ?
            std::strncmp(option, longname, p-option) :
            std::strcmp(option, longname);
    }
};


/** The begin of inline option map. */
#define BEGIN_OPTION_MAP_INLINE() \
    virtual int handle_option(char __c, const char *__longname, const char *arg) \
    { \
        int used_args = 0; \
        if (0) { \

/** Define of option map. */
#define DEFINE_OPTION_MAP() \
    virtual int handle_option(char __c, const char *__longname, const char *arg);

/** Begin of option map implimentation. */
#define BEGIN_OPTION_MAP(_Class) \
    int _Class::handle_option(char __c, const char *__longname, const char *arg) \
    { \
        int used_args = 0; \
        if (0) { \

/** An entry of option map */
#define ON_OPTION(test) \
            return used_args; \
        } else if (test) { \
            used_args = 0; \

#define ON_OPTION_WITH_ARG(test) \
            return used_args; \
        } else if (test) { \
            used_args = 1; \

/** The end of option map implementation */
#define END_OPTION_MAP() \
            return used_args; \
        } \
        return -1; \
    } \

/** A predicator for short options */
#define SHORTOPT(x)     (__c == x)
/** A predicator for long options */
#define LONGOPT(x)      (!__c && __optstrcmp(__longname, x) == 0)


#ifdef  USE_NCL_NAMESPACE
};
#endif/*USE_NCL_NAMESPACE*/






#ifdef  _BUILD_NCL_SAMPLE

#include <cstdio>
#include <iostream>

/**
 * A class to store parameters specified by command-line arguments
 */
class option : public optparse {
public:
    int bytes;
    int lines;
    bool quiet;

    option() : bytes(0), lines(0), quiet(false) {}

    BEGIN_OPTION_MAP_INLINE()
        ON_OPTION(SHORTOPT('b') || LONGOPT("bytes"))
            bytes = std::atoi(arg);
            used_args = 1;  // Notify the parser of a consumption of argument.

        ON_OPTION_WITH_ARG(SHORTOPT('l') || LONGOPT("lines"))
            lines = std::atoi(arg);
            // no need of the notification: used_args variable will be set to 1.

        ON_OPTION(SHORTOPT('q') || LONGOPT("quiet") || LONGOPT("silent"))
            quiet = true;

    END_OPTION_MAP()
};

int main(int argc, char *argv[])
{
    try {
        option opt;
        int argused = opt.parse(&argv[1], argc-1); // Skip argv[0].

        std::cout << "used argv: " << argused << std::endl;
        std::cout << "bytes: " << opt.bytes << std::endl;
        std::cout << "lines: " << opt.lines << std::endl;
        std::cout << "quiet: " << opt.quiet << std::endl;
    } catch (const optparse::unrecognized_option& e) {
        std::cout << "unrecognized option: " << e.what() << std::endl;
        return 1;
    } catch (const optparse::invalid_value& e) {
        std::cout << "invalid value: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

#endif/*_BUILD_NCL_SAMPLE*/


#endif/*__NCL_OPTPRASE_H__*/