aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/getopt/small/last_getopt_support.h
blob: 17bed3e614e6dfe99ce53023244c2ae2c9ab298c (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
#pragma once

#include <util/string/cast.h>
#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/generic/utility.h>
#include <util/generic/yexception.h>

namespace NLastGetopt {
    class TOpt;
    class TOpts;
    class TOptsParser;
    class TOptsParseResult;

    /// base of all getopt exceptions
    class TException: public yexception {
    };

    /// TOpts configuration is incorrect
    class TConfException: public TException {
    };

    /// User passed incorrect arguments, parsing failed
    /// Note: use `throw TUsageException()` instead of `ythrow TUsageException()` to prevent appearence of stacktrace
    /// and location of the `ythrow` statment in error messages.
    class TUsageException: public TException {
    };

    struct IOptHandler {
        virtual void HandleOpt(const TOptsParser* parser) = 0;
        virtual ~IOptHandler() = default;
    };

    namespace NPrivate {
        template <typename TpFunc>
        class THandlerFunctor0
           : public IOptHandler {
            TpFunc Func_;

        public:
            THandlerFunctor0(TpFunc func)
                : Func_(func)
            {
            }

            void HandleOpt(const TOptsParser*) override {
                Func_();
            }
        };

        template <typename TpFunc, typename TpArg = const TOptsParser*>
        class THandlerFunctor1
           : public IOptHandler {
            TpFunc Func_;
            const TpArg Def_;
            const bool HasDef_;

        public:
            THandlerFunctor1(TpFunc func)
                : Func_(func)
                , Def_()
                , HasDef_(false)
            {
            }

            template <typename T>
            THandlerFunctor1(const TpFunc& func, const T& def)
                : Func_(func)
                , Def_(def)
                , HasDef_(true)
            {
            }

            void HandleOpt(const TOptsParser* parser) override;
        };

        template <typename TpFunc>
        class THandlerFunctor1<TpFunc, const TOptsParser*>
           : public IOptHandler {
            TpFunc Func_;

        public:
            THandlerFunctor1(TpFunc func)
                : Func_(func)
            {
            }

            void HandleOpt(const TOptsParser* parser) override {
                Func_(parser);
            }
        };

        template <typename T, typename TpVal = T>
        class TStoreResultFunctor {
        private:
            T* Target_;

        public:
            TStoreResultFunctor(T* target)
                : Target_(target)
            {
            }

            void operator()(const TpVal& val) {
                *Target_ = val;
            }
        };

        template <typename TpTarget, typename TpFunc, typename TpVal = TpTarget>
        class TStoreMappedResultFunctor {
        private:
            TpTarget* Target_;
            const TpFunc Func_;

        public:
            TStoreMappedResultFunctor(TpTarget* target, const TpFunc& func)
                : Target_(target)
                , Func_(func)
            {
            }

            void operator()(const TpVal& val) {
                *Target_ = Func_(val);
            }
        };

        template <typename T, typename TpVal = T>
        class TStoreValueFunctor {
            T* Target;
            const TpVal Value;

        public:
            template <typename TpArg>
            TStoreValueFunctor(T* target, const TpArg& value)
                : Target(target)
                , Value(value)
            {
            }

            void operator()(const TOptsParser*) {
                *Target = Value;
            }
        };

        TString OptToString(char c);
        TString OptToString(const TString& longOption);
        TString OptToString(const TOpt* opt);

        template <typename T>
        inline T OptFromStringImpl(const TStringBuf& value) {
            return FromString<T>(value);
        }

        template <>
        inline TStringBuf OptFromStringImpl<TStringBuf>(const TStringBuf& value) {
            return value;
        }

        template <>
        inline const char* OptFromStringImpl<const char*>(const TStringBuf& value) {
            return value.data();
        }

        template <typename T, typename TSomeOpt>
        T OptFromString(const TStringBuf& value, const TSomeOpt opt) {
            try {
                return OptFromStringImpl<T>(value);
            } catch (...) {
                throw TUsageException() << "failed to parse opt " << OptToString(opt) << " value " << TString(value).Quote() << ": " << CurrentExceptionMessage();
            }
        }

        // wrapper of FromString<T> that prints nice message about option used
        template <typename T, typename TSomeOpt>
        T OptFromString(const TStringBuf& value, const TSomeOpt opt);

    }
}