aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/getopt/small/last_getopt_parse_result.h
blob: e55a1a1eec0b5901b728765a5bff694e1f32d2d5 (plain) (blame)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#pragma once 
 
#include "last_getopt_opts.h" 
#include "last_getopt_parser.h"
 
namespace NLastGetopt { 
    /**
     * NLastGetopt::TOptParseResult contains all arguments for exactly one TOpt,
     * that have been fetched during parsing
     *
     * The class is a wraper over a vector of nil-terminated strings.
     */
    class TOptParseResult {
    public:
        typedef TVector<const char*> TValues;
 
    public:
        TOptParseResult(const TOpt* opt = nullptr)
            : Opt_(opt)
        {
        }
 
    public:
        const TOpt& Opt() const {
            return *Opt_;
        }
        const TOpt* OptPtr() const {
            return Opt_;
        }
        const TValues& Values() const {
            return Values_;
        }
        bool Empty() const {
            return Values().empty();
        }
        size_t Count() const {
            return Values_.size();
        }
        void AddValue(const char* val) {
            if (nullptr != val)
                Values_.push_back(val);
        }
        const char* DefVal(const char* def = nullptr) const {
            return Opt().HasDefaultValue() ? Opt().GetDefaultValue().c_str() : def;
        }
        const char* Front(const char* def = nullptr) const {
            return Empty() ? DefVal(def) : Values().front();
        }
        const char* Back(const char* def = nullptr) const {
            return Empty() ? DefVal(def) : Values().back();
        }
 
    private:
        const TOpt* Opt_;
        TValues Values_;
    };

    /**
     * NLastGetopt::TOptsParseResult contains result of parsing argc,argv be parser.
     *
     * In most common case constructed by argc,argv pair and rules (TOpts).
     * The instance being constructed validates rules and performs parsing, stores result for futher access.
     *
     * If error during parsing occures, the program aborts with exit code 1.
     * Note, that if PERMUTE mode is on, then data, pointed by argv can be changed.
     */
    class TOptsParseResult {
    private:
        THolder<TOptsParser> Parser_; //The instance of parser.
 
        // XXX: make argc, argv
        typedef TVector<TOptParseResult> TdVec;
 
        TdVec Opts_;    //Parsing result for all options, that have been explicitly defined in argc/argv
        TdVec OptsDef_; //Parsing result for options, that have been defined by default values only
 
    private:
        TOptParseResult& OptParseResult();
 
        /**
         * Searchs for object in given container
         *
         * @param vec     container
         * @param opt     ptr for required object
         *
         * @retunr        ptr on corresponding TOptParseResult
         */
        static const TOptParseResult* FindParseResult(const TdVec& vec, const TOpt* opt);
 
    protected:
        /**
         * Performs parsing of comand line arguments.
         */
        void Init(const TOpts* options, int argc, const char** argv);
 
        TOptsParseResult() = default;
 
    public:
        /**
         * The action in case of parser failure.
         * Allows to asjust behavior in derived classes.
         * By default prints error string and aborts the program
         */
        virtual void HandleError() const;
 
        /**
         * Constructs object by parsing arguments with given rules
         *
         * @param options      ptr on parsing rules
         * @param argc
         * @param argv
         */
        TOptsParseResult(const TOpts* options, int argc, const char* argv[]) {
            Init(options, argc, argv);
        }
 
        /**
         * Constructs object by parsing arguments with given rules
         *
         * @param options      ptr on parsing rules
         * @param argc
         * @param argv
         */
        TOptsParseResult(const TOpts* options, int argc, char* argv[]) {
            Init(options, argc, const_cast<const char**>(argv));
        }
 
        virtual ~TOptsParseResult() = default;
 
        /**
         * Search for TOptParseResult that corresponds to given option (TOpt)
         *
         * @param opt                ptr on required object
         * @param includeDefault     search in results obtained from default values
         *
         * @return                   ptr on result
         */
        const TOptParseResult* FindOptParseResult(const TOpt* opt, bool includeDefault = false) const;
 
        /**
         * Search for TOptParseResult that corresponds to given long name
         *
         * @param name               long name of required object
         * @param includeDefault     search in results obtained from default values
         *
         * @return                   ptr on result
         */
        const TOptParseResult* FindLongOptParseResult(const TString& name, bool includeDefault = false) const;
 
        /**
         * Search for TOptParseResult that corresponds to given short name
         *
         * @param c                  short name of required object
         * @param includeDefault     search in results obtained from default values
         *
         * @return                   ptr on result
         */
        const TOptParseResult* FindCharOptParseResult(char c, bool includeDefault = false) const;
 
        /**
         * @return argv[0]
         */
        TString GetProgramName() const;
 
        /**
         * Print usage string.
         */
        void PrintUsage(IOutputStream& os = Cout) const;

        /**
         * @return position in [premuted argv] of the first free argument
         */
        size_t GetFreeArgsPos() const;
 
        /**
         * @return number of fetched free arguments
         */
        size_t GetFreeArgCount() const;
 
        /**
         * @return all fetched free arguments
         */
        TVector<TString> GetFreeArgs() const;
 
        /**
         * @return true if given option exist in results of parsing
         *
         * @param opt                ptr on required object
         * @param includeDefault     search in results obtained from default values
         *
         */
        bool Has(const TOpt* opt, bool includeDefault = false) const;
 
        /**
         * @return nil terminated string on the last fetched argument of givne option
         *
         * @param opt                ptr on required object
         * @param includeDefault     search in results obtained from default values
         */
        const char* Get(const TOpt* opt, bool includeDefault = true) const;
 
        /**
         * @return nil terminated string on the last fetched argument of givne option
         *    if option haven't been fetched, given defaultValue will be returned
         *
         * @param opt                ptr on required object
         * @param defaultValue
         */
        const char* GetOrElse(const TOpt* opt, const char* defaultValue) const;
 
        /**
         * @return true if given option exist in results of parsing
         *
         * @param name               long name of required object
         * @param includeDefault     search in results obtained from default values
         *
         */
        bool Has(const TString& name, bool includeDefault = false) const;
 
        /**
         * @return nil terminated string on the last fetched argument of givne option
         *
         * @param name               long name of required object
         * @param includeDefault     search in results obtained from default values
         */
        const char* Get(const TString& name, bool includeDefault = true) const;
 
        /**
         * @return nil terminated string on the last fetched argument of givne option
         *    if option haven't been fetched, given defaultValue will be returned
         *
         * @param name               long name of required object
         * @param defaultValue
         */
        const char* GetOrElse(const TString& name, const char* defaultValue) const;
 
        /**
         * @return true if given option exist in results of parsing
         *
         * @param c                  short name of required object
         * @param includeDefault     search in results obtained from default values
         *
         */
        bool Has(char name, bool includeDefault = false) const;
 
        /**
         * @return nil terminated string on the last fetched argument of givne option
         *
         * @param c                  short name of required object
         * @param includeDefault     search in results obtained from default values
         */
        const char* Get(char name, bool includeDefault = true) const;
 
        /**
         * @return nil terminated string on the last fetched argument of givne option
         *    if option haven't been fetched, given defaultValue will be returned
         *
         * @param c                  short name of required object
         * @param defaultValue
         */
        const char* GetOrElse(char name, const char* defaultValue) const;
 
        /**
         * for givne option return parsed value of the last fetched argument
         * if option haven't been fetched, HandleError action is called
         *
         * @param opt       required option (one of: ptr, short name, long name).
         *
         * @return       FromString<T>(last feteched argument)
         */
        template <typename T, typename TKey>
        T Get(const TKey opt) const {
            const char* value = Get(opt);
            try {
                return NPrivate::OptFromString<T>(value, opt);
            } catch (...) {
                HandleError();
                throw;
            }
        } 
 
        /**
         * for givne option return parsed value of the last fetched argument
         * if option haven't been fetched, given defaultValue will be returned
         *
         * @param opt       required option (one of: ptr, short name, long name).
         * @param defaultValue
         *
         * @return       FromString<T>(last feteched argument)
         */
        template <typename T, typename TKey>
        T GetOrElse(const TKey opt, const T& defaultValue) const {
            if (Has(opt))
                return Get<T>(opt);
            else
                return defaultValue;
        }
    };
 
    /**
     * NLastGetopt::TOptsParseResultException contains result of parsing argc,argv be parser.
     *
     * Unlike TOptsParseResult, if error during parsing occures, an exception is thrown.
     *
     */
    class TOptsParseResultException: public TOptsParseResult {
    public:
        TOptsParseResultException(const TOpts* options, int argc, const char* argv[]) {
            Init(options, argc, argv);
        }
        TOptsParseResultException(const TOpts* options, int argc, char* argv[]) {
            Init(options, argc, const_cast<const char**>(argv));
        }
        virtual ~TOptsParseResultException() = default;
        void HandleError() const override;

    protected:
        TOptsParseResultException() = default;
    };
 
}