aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/getopt/small/last_getopt_parser.cpp
blob: 911c76f3425bd6381c897207ce92f72a4b002800 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "last_getopt_parser.h"

#include <library/cpp/colorizer/colors.h>

#include <util/string/escape.h>

namespace NLastGetopt {
    void TOptsParser::Init(const TOpts* opts, int argc, const char* argv[]) {
        opts->Validate();

        Opts_ = opts;

        if (argc < 1)
            throw TUsageException() << "argv must have at least one argument";

        Argc_ = argc;
        Argv_ = argv;

        ProgramName_ = argv[0];

        Pos_ = 1;
        Sop_ = 0;
        CurrentOpt_ = nullptr;
        CurrentValue_ = nullptr;
        GotMinusMinus_ = false;
        Stopped_ = false;
        OptsSeen_.clear();
        OptsDefault_.clear();
    }

    void TOptsParser::Init(const TOpts* opts, int argc, char* argv[]) {
        Init(opts, argc, const_cast<const char**>(argv));
    }

    void TOptsParser::Swap(TOptsParser& that) {
        DoSwap(Opts_, that.Opts_);
        DoSwap(Argc_, that.Argc_);
        DoSwap(Argv_, that.Argv_);
        DoSwap(TempCurrentOpt_, that.TempCurrentOpt_);
        DoSwap(ProgramName_, that.ProgramName_);
        DoSwap(Pos_, that.Pos_);
        DoSwap(Sop_, that.Sop_);
        DoSwap(Stopped_, that.Stopped_);
        DoSwap(CurrentOpt_, that.CurrentOpt_);
        DoSwap(CurrentValue_, that.CurrentValue_);
        DoSwap(GotMinusMinus_, that.GotMinusMinus_);
        DoSwap(OptsSeen_, that.OptsSeen_);
    }

    bool TOptsParser::Commit(const TOpt* currentOpt, const TStringBuf& currentValue, size_t pos, size_t sop) {
        Pos_ = pos;
        Sop_ = sop;
        CurrentOpt_ = currentOpt;
        CurrentValue_ = currentValue;
        if (nullptr != currentOpt)
            OptsSeen_.insert(currentOpt);
        return true;
    }

    bool TOptsParser::CommitEndOfOptions(size_t pos) {
        Pos_ = pos;
        Sop_ = 0;
        Y_ASSERT(!CurOpt());
        Y_ASSERT(!CurVal());

        Y_ASSERT(!Stopped_);

        if (Opts_->FreeArgsMin_ == Opts_->FreeArgsMax_ && Argc_ - Pos_ != Opts_->FreeArgsMin_)
            throw TUsageException() << "required exactly " << Opts_->FreeArgsMin_ << " free args";
        else if (Argc_ - Pos_ < Opts_->FreeArgsMin_)
            throw TUsageException() << "required at least " << Opts_->FreeArgsMin_ << " free args";
        else if (Argc_ - Pos_ > Opts_->FreeArgsMax_)
            throw TUsageException() << "required at most " << Opts_->FreeArgsMax_ << " free args";

        return false;
    }

    bool TOptsParser::ParseUnknownShortOptWithinArg(size_t pos, size_t sop) {
        Y_ASSERT(pos < Argc_);
        const TStringBuf arg(Argv_[pos]);
        Y_ASSERT(sop > 0);
        Y_ASSERT(sop < arg.length());
        Y_ASSERT(EIO_NONE != IsOpt(arg));

        if (!Opts_->AllowUnknownCharOptions_)
            throw TUsageException() << "unknown option '" << EscapeC(arg[sop])
                                     << "' in '" << arg << "'";

        TempCurrentOpt_.Reset(new TOpt);
        TempCurrentOpt_->AddShortName(arg[sop]);

        sop += 1;

        // mimic behavior of Opt: unknown option has arg only if char is last within arg
        if (sop < arg.length()) {
            return Commit(TempCurrentOpt_.Get(), nullptr, pos, sop);
        }

        pos += 1;
        sop = 0;
        if (pos == Argc_ || EIO_NONE != IsOpt(Argv_[pos])) {
            return Commit(TempCurrentOpt_.Get(), nullptr, pos, 0);
        }

        return Commit(TempCurrentOpt_.Get(), Argv_[pos], pos + 1, 0);
    }

    bool TOptsParser::ParseShortOptWithinArg(size_t pos, size_t sop) {
        Y_ASSERT(pos < Argc_);
        const TStringBuf arg(Argv_[pos]);
        Y_ASSERT(sop > 0);
        Y_ASSERT(sop < arg.length());
        Y_ASSERT(EIO_NONE != IsOpt(arg));

        size_t p = sop;
        char c = arg[p];
        const TOpt* opt = Opts_->FindCharOption(c);
        if (!opt)
            return ParseUnknownShortOptWithinArg(pos, sop);
        p += 1;
        if (p == arg.length()) {
            return ParseOptParam(opt, pos + 1);
        }
        if (opt->GetHasArg() == NO_ARGUMENT) {
            return Commit(opt, nullptr, pos, p);
        }
        return Commit(opt, arg.SubStr(p), pos + 1, 0);
    }

    bool TOptsParser::ParseShortOptArg(size_t pos) {
        Y_ASSERT(pos < Argc_);
        const TStringBuf arg(Argv_[pos]);
        Y_ASSERT(EIO_NONE != IsOpt(arg));
        Y_ASSERT(!arg.StartsWith("--"));
        return ParseShortOptWithinArg(pos, 1);
    }

    bool TOptsParser::ParseOptArg(size_t pos) {
        Y_ASSERT(pos < Argc_);
        TStringBuf arg(Argv_[pos]);
        const EIsOpt eio = IsOpt(arg);
        Y_ASSERT(EIO_NONE != eio);
        if (EIO_DDASH == eio || EIO_PLUS == eio || (Opts_->AllowSingleDashForLong_ || !Opts_->HasAnyShortOption())) {
            // long option
            bool singleCharPrefix = EIO_DDASH != eio;
            arg.Skip(singleCharPrefix ? 1 : 2);
            TStringBuf optionName = arg.NextTok('=');
            const TOpt* option = Opts_->FindLongOption(optionName);
            if (!option) {
                if (singleCharPrefix && !arg.IsInited()) {
                    return ParseShortOptArg(pos);
                } else if (Opts_->AllowUnknownLongOptions_) {
                    return false;
                } else {
                    throw TUsageException() << "unknown option '" << optionName
                                             << "' in '" << Argv_[pos] << "'";
                }
            }
            if (arg.IsInited()) {
                if (option->GetHasArg() == NO_ARGUMENT)
                    throw TUsageException() << "option " << optionName << " must have no arg";
                return Commit(option, arg, pos + 1, 0);
            }
            ++pos;
            return ParseOptParam(option, pos);
        } else {
            return ParseShortOptArg(pos);
        }
    }

    bool TOptsParser::ParseOptParam(const TOpt* opt, size_t pos) {
        Y_ASSERT(opt);
        if (opt->GetHasArg() == NO_ARGUMENT || opt->IsEqParseOnly()) {
            return Commit(opt, nullptr, pos, 0);
        }
        if (pos == Argc_) {
            if (opt->GetHasArg() == REQUIRED_ARGUMENT)
                throw TUsageException() << "option " << opt->ToShortString() << " must have arg";
            return Commit(opt, nullptr, pos, 0);
        }
        const TStringBuf arg(Argv_[pos]);
        if (!arg.StartsWith('-') || opt->GetHasArg() == REQUIRED_ARGUMENT) {
            return Commit(opt, arg, pos + 1, 0);
        }
        return Commit(opt, nullptr, pos, 0);
    }

    TOptsParser::EIsOpt TOptsParser::IsOpt(const TStringBuf& arg) const {
        EIsOpt eio = EIO_NONE;
        if (1 < arg.length()) {
            switch (arg[0]) {
                default:
                    break;
                case '-':
                    if ('-' != arg[1])
                        eio = EIO_SDASH;
                    else if (2 < arg.length())
                        eio = EIO_DDASH;
                    break;
                case '+':
                    if (Opts_->AllowPlusForLong_)
                        eio = EIO_PLUS;
                    break;
            }
        }
        return eio;
    }

    static void memrotate(void* ptr, size_t size, size_t shift) {
        TTempBuf buf(shift);
        memcpy(buf.Data(), (char*)ptr + size - shift, shift);
        memmove((char*)ptr + shift, ptr, size - shift);
        memcpy(ptr, buf.Data(), shift);
    }

    bool TOptsParser::ParseWithPermutation() {
        Y_ASSERT(Sop_ == 0);
        Y_ASSERT(Opts_->ArgPermutation_ == PERMUTE);

        const size_t p0 = Pos_;

        size_t pc = Pos_;

        for (; pc < Argc_ && EIO_NONE == IsOpt(Argv_[pc]); ++pc) {
            // count non-args
        }

        if (pc == Argc_) {
            return CommitEndOfOptions(Pos_);
        }

        Pos_ = pc;

        bool r = ParseOptArg(Pos_);
        Y_ASSERT(r);
        while (Pos_ == pc) {
            Y_ASSERT(Sop_ > 0);
            r = ParseShortOptWithinArg(Pos_, Sop_);
            Y_ASSERT(r);
        }

        size_t p2 = Pos_;

        Y_ASSERT(p2 - pc >= 1);
        Y_ASSERT(p2 - pc <= 2);

        memrotate(Argv_ + p0, (p2 - p0) * sizeof(void*), (p2 - pc) * sizeof(void*));

        bool r2 = ParseOptArg(p0);
        Y_ASSERT(r2);
        return r2;
    }

    bool TOptsParser::DoNext() {
        Y_ASSERT(Pos_ <= Argc_);

        if (Pos_ == Argc_)
            return CommitEndOfOptions(Pos_);

        if (GotMinusMinus_ && Opts_->ArgPermutation_ == RETURN_IN_ORDER) {
            Y_ASSERT(Sop_ == 0);
            return Commit(nullptr, Argv_[Pos_], Pos_ + 1, 0);
        }

        if (Sop_ > 0)
            return ParseShortOptWithinArg(Pos_, Sop_);

        size_t pos = Pos_;
        const TStringBuf arg(Argv_[pos]);
        if (EIO_NONE != IsOpt(arg)) {
            return ParseOptArg(pos);
        } else if (arg == "--") {
            if (Opts_->ArgPermutation_ == RETURN_IN_ORDER) {
                pos += 1;
                if (pos == Argc_)
                    return CommitEndOfOptions(pos);
                GotMinusMinus_ = true;
                return Commit(nullptr, Argv_[pos], pos + 1, 0);
            } else {
                return CommitEndOfOptions(pos + 1);
            }
        } else if (Opts_->ArgPermutation_ == RETURN_IN_ORDER) {
            return Commit(nullptr, arg, pos + 1, 0);
        } else if (Opts_->ArgPermutation_ == REQUIRE_ORDER) {
            return CommitEndOfOptions(Pos_);
        } else {
            return ParseWithPermutation();
        }
    }

    bool TOptsParser::Next() {
        bool r = false;

        if (OptsDefault_.empty()) {
            CurrentOpt_ = nullptr;
            TempCurrentOpt_.Destroy();

            CurrentValue_ = nullptr;

            if (Stopped_)
                return false;

            TOptsParser copy = *this;

            r = copy.DoNext();

            Swap(copy);

            if (!r) {
                Stopped_ = true;
                // we are done; check for missing options
                Finish();
            }
        }

        if (!r && !OptsDefault_.empty()) {
            CurrentOpt_ = OptsDefault_.front();
            CurrentValue_ = CurrentOpt_->GetDefaultValue();
            OptsDefault_.pop_front();
            r = true;
        }

        if (r) {
            if (CurOpt())
                CurOpt()->FireHandlers(this);
        }

        return r;
    }

    void TOptsParser::Finish() {
        const TOpts::TOptsVector& optvec = Opts_->Opts_;
        if (optvec.size() == OptsSeen_.size())
            return;

        TVector<TString> missingLong;
        TVector<char> missingShort;

        TOpts::TOptsVector::const_iterator it;
        for (it = optvec.begin(); it != optvec.end(); ++it) {
            const TOpt* opt = (*it).Get();
            if (nullptr == opt)
                continue;
            if (OptsSeen_.contains(opt))
                continue;

            if (opt->IsRequired()) {
                const TOpt::TLongNames& optnames = opt->GetLongNames();
                if (!optnames.empty())
                    missingLong.push_back(optnames[0]);
                else {
                    const char ch = opt->GetCharOr0();
                    if (0 != ch)
                        missingShort.push_back(ch);
                }
                continue;
            }

            if (opt->HasDefaultValue())
                OptsDefault_.push_back(opt);
        }

        // also indicates subsequent options, if any, haven't been seen actually
        OptsSeen_.clear();

        const size_t nmissing = missingLong.size() + missingShort.size();
        if (0 == nmissing)
            return;

        TUsageException usage;
        usage << "The following option";
        usage << ((1 == nmissing) ? " is" : "s are");
        usage << " required:";
        for (size_t i = 0; i != missingLong.size(); ++i)
            usage << " --" << missingLong[i];
        for (size_t i = 0; i != missingShort.size(); ++i)
            usage << " -" << missingShort[i];
        throw usage; // don't need lineinfo, just the message
    }

    void TOptsParser::PrintUsage(IOutputStream& os, const NColorizer::TColors& colors) const {
        Opts_->PrintUsage(ProgramName(), os, colors);
    }

    void TOptsParser::PrintUsage(IOutputStream& os) const {
        PrintUsage(os, NColorizer::AutoColors(os));
    }

}