RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
ArgParser.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstddef>
7#include <deque>
8#include <filesystem>
9#include <memory>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <unordered_map>
14#include <utility>
15#include <vector>
16
17namespace RST::Parser {
18
34 class ArgParser {
35 public:
37 explicit ArgParser(std::string programName = "", std::string description = "");
38
39 // ── Declaration ──────────────────────────────────────────────────────
41 Argument& addFlag(std::vector<std::string> flags, std::string description);
43 Argument& addOption(std::vector<std::string> flags, std::string description);
45 Argument& addPositional(std::string name, std::string description);
47 ArgParser& addSubcommand(std::string name, std::string description);
48
50 void setVersion(std::string version) { _version = std::move(version); }
52 void setEpilog(std::string text) { _epilog = std::move(text); }
53
57 bool loadConfig(const std::filesystem::path& file);
58
59 // ── Parsing ──────────────────────────────────────────────────────────
61 [[nodiscard]] ParseResult parse(int argc, const char* const* argv);
63 [[nodiscard]] ParseResult parse(const std::vector<std::string>& args);
64
65 // ── Result ──────────────────────────────────────────────────────────
67 [[nodiscard]] bool isSet(std::string_view name) const { return lookup(name).isSet(); }
69 [[nodiscard]] std::size_t count(std::string_view name) const { return lookup(name).count(); }
70
72 template <typename T = std::string>
73 [[nodiscard]] std::optional<T> get(std::string_view name) const { return lookup(name).get<T>(); }
74
76 template <typename T = std::string>
77 [[nodiscard]] std::vector<T> getAll(std::string_view name) const { return lookup(name).getAll<T>(); }
78
80 [[nodiscard]] const std::string& activeSubcommand() const noexcept { return _activeSubcommand; }
82 [[nodiscard]] const ArgParser* activeSubparser() const;
83
84 // ── Help ─────────────────────────────────────────────────────────────
86 [[nodiscard]] std::string help() const;
88 void printHelp() const;
89
90 private:
91 struct Subcommand {
92 std::string name;
93 std::unique_ptr<ArgParser> parser;
94 };
95
96 Argument& addArgument(Argument::Kind kind, std::vector<std::string> flags, std::string description);
97
98 [[nodiscard]] ParseResult parseLongOption(std::string_view token, const std::vector<std::string>& args, std::size_t& index);
99 [[nodiscard]] ParseResult parseShortOptions(std::string_view token, const std::vector<std::string>& args, std::size_t& index);
100 [[nodiscard]] ParseResult parsePositional(const std::string& token, std::size_t& index);
101 [[nodiscard]] ParseResult parseSubcommand(const Subcommand& command, const std::vector<std::string>& args, std::size_t first);
102 [[nodiscard]] ParseResult takeValues(Argument& argument, std::string_view flag, std::optional<std::string_view> inlineValue,
103 const std::vector<std::string>& args, std::size_t& index);
104 [[nodiscard]] ParseResult builtInOption(std::string_view flag) const;
105 [[nodiscard]] ParseResult applyFallbacks();
106 [[nodiscard]] ParseResult checkRequired() const;
107 [[nodiscard]] ParseResult fail(const std::string& error) const;
108
109 [[nodiscard]] const Argument& lookup(std::string_view name) const;
110 [[nodiscard]] const Argument* findFlag(std::string_view flag) const;
111 [[nodiscard]] Argument* findFlag(std::string_view flag);
112 [[nodiscard]] Argument* findNegatedFlag(std::string_view flag);
113 [[nodiscard]] Argument* positionalAt(std::size_t index);
114 [[nodiscard]] const Subcommand* findSubcommand(std::string_view name) const;
115 [[nodiscard]] bool isOptionToken(std::string_view token) const;
116 [[nodiscard]] std::optional<std::string> configValue(const Argument& argument) const;
117 [[nodiscard]] std::vector<std::string> longFlags() const;
118 [[nodiscard]] std::string programName() const;
119 [[nodiscard]] std::string usage() const;
120
121 std::string _programName;
122 std::string _description;
123 std::string _version;
124 std::string _epilog;
125 std::deque<Argument> _arguments;
126 std::vector<Subcommand> _subcommands;
127 std::unordered_map<std::string, std::string> _config;
128 std::string _activeSubcommand;
129 };
130}
Command-line parser with flags, options, positionals, subcommands and generated help.
Definition ArgParser.hpp:34
ArgParser & addSubcommand(std::string name, std::string description)
Adds a subcommand with its own arguments, and returns its parser.
void setEpilog(std::string text)
Text printed at the end of the help.
Definition ArgParser.hpp:52
std::string help() const
The generated help text.
bool loadConfig(const std::filesystem::path &file)
Loads fallback values from a file of name = value lines, name being a flag without dashes.
void printHelp() const
Prints help() to stdout.
Argument & addOption(std::vector< std::string > flags, std::string description)
Adds an option taking a value: --name value, --name=value, -n value or -n5.
ArgParser(std::string programName="", std::string description="")
Creates a parser; programName defaults to the executable name.
Argument & addFlag(std::vector< std::string > flags, std::string description)
Adds a flag without value; short flags can be bundled, like -abc.
void setVersion(std::string version)
Enables --version, which prints the program name and version.
Definition ArgParser.hpp:50
std::vector< T > getAll(std::string_view name) const
Shortcut for Argument::getAll().
Definition ArgParser.hpp:77
const std::string & activeSubcommand() const noexcept
Name of the subcommand given on the command line, or an empty string.
Definition ArgParser.hpp:80
std::size_t count(std::string_view name) const
Shortcut for Argument::count().
Definition ArgParser.hpp:69
ParseResult parse(int argc, const char *const *argv)
Parses the arguments of main.
bool isSet(std::string_view name) const
Shortcut for Argument::isSet(); name is a flag (-v, --verbose) or a name (verbose).
Definition ArgParser.hpp:67
Argument & addPositional(std::string name, std::string description)
Adds a positional argument; positionals are filled in declaration order.
ParseResult parse(const std::vector< std::string > &args)
Parses args, without the program name.
std::optional< T > get(std::string_view name) const
Shortcut for Argument::get().
Definition ArgParser.hpp:73
const ArgParser * activeSubparser() const
Parser of the active subcommand, or nullptr.
A flag, option or positional argument, configured with chained calls.
Definition Argument.hpp:41
bool isSet() const noexcept
Whether a value came from the command line, the environment or the config file.
Definition Argument.hpp:89
std::size_t count() const noexcept
How many times the argument was given, such as 3 for -vvv.
Definition Argument.hpp:91
std::optional< T > get() const
The last value, or the default value, converted to T; std::nullopt if there is none or it does not co...
Definition Argument.hpp:95
std::vector< T > getAll() const
Every value converted to T, skipping those that do not convert, or the default value.
Definition Argument.hpp:104
Command-line parser with subcommands, typed and validated values and generated help.
Definition ArgParser.hpp:17
Result of ArgParser::parse(), true when the arguments are valid.
Definition ParseResult.hpp:12