aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/args/root.hh
blob: 499ee6df4d603d7ad34bc1e9b6c4d303b456e247 (plain)
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
#pragma once
///@file

#include "args.hh"

namespace nix {

/**
 * The concrete implementation of a collection of completions.
 *
 * This is exposed so that the main entry point can print out the
 * collected completions.
 */
struct Completions final : AddCompletions
{
    std::set<Completion> completions;
    Type type = Type::Normal;

    void setType(Type type) override;
    void add(std::string completion, std::string description = "") override;
};

/**
 * The outermost Args object. This is the one we will actually parse a command
 * line with, whereas the inner ones (if they exists) are subcommands (and this
 * is also a MultiCommand or something like it).
 *
 * This Args contains completions state shared between it and all of its
 * descendent Args.
 */
class RootArgs : virtual public Args
{
public:
    /** Parse the command line, throwing a UsageError if something goes
     * wrong.
     */
    void parseCmdline(const Strings & cmdline);

    std::shared_ptr<Completions> completions;

protected:

    friend class Args;

    /**
     * A pointer to the completion and its two arguments; a thunk;
     */
    struct DeferredCompletion {
        const CompleterClosure & completer;
        size_t n;
        std::string prefix;
    };

    /**
     * Completions are run after all args and flags are parsed, so completions
     * of earlier arguments can benefit from later arguments.
     */
    std::vector<DeferredCompletion> deferredCompletions;

    /**
     * Experimental features needed when parsing args. These are checked
     * after flag parsing is completed in order to support enabling
     * experimental features coming after the flag that needs the
     * experimental feature.
     */
    std::set<ExperimentalFeature> flagExperimentalFeatures;

    virtual std::optional<std::reference_wrapper<RootArgs>> asRootArgs() override {
        return *this;
    }

private:

    std::optional<std::string> needsCompletion(std::string_view s);
};

}