ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
argument.h
1/*
2 * Copyright 2025 Dominik Czekai
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18#include <any>
19#include <string>
20#include <string_view>
21#include <typeindex>
22
23namespace cli::commands
24{
25
27enum class ArgumentKind
28{
29 Positional,
30 Option,
31 Flag,
32};
33
34namespace docwriting
35{
36class DocWriter; // Forward declaration
37}
38
41{
42public:
43 virtual ~ArgumentBase() = default;
44
45#pragma region Accessors
46
50 [[nodiscard]] constexpr std::string_view getName() const noexcept { return name; }
51
56 [[nodiscard]] constexpr std::string_view getOptionComment() const noexcept
57 {
58 return optionsComment;
59 }
60
63 [[nodiscard]] constexpr bool isRequired() const noexcept { return required; }
64
67 [[nodiscard]] constexpr bool isRepeatable() const noexcept { return repeatable; }
68
71 [[nodiscard]] constexpr ArgumentKind getArgType() const { return argType; }
72
78 [[nodiscard]] virtual std::string getOptionsDocString(
79 const docwriting::DocWriter &writer) const = 0;
80
87 [[nodiscard]] virtual std::string getArgDocString(
88 const docwriting::DocWriter &writer) const = 0;
89#pragma endregion Accessors
90
91protected:
92 ArgumentBase(std::string_view name, std::string_view optionsComment, ArgumentKind argType,
93 bool repeatable, bool required)
94 : name(name), optionsComment(optionsComment), argType(argType), repeatable(repeatable),
95 required(required)
96 {
97 }
98
99 const std::string name;
100 std::string optionsComment;
101 const ArgumentKind argType;
102 bool repeatable{false};
103 bool required{true};
104};
105
108{
109public:
110 virtual ~TypedArgumentBase() = default;
111
114 [[nodiscard]] std::type_index getType() const { return type; }
115
119 [[nodiscard]] virtual std::any parseToValue(const std::string &input) const = 0;
120
121protected:
122 explicit TypedArgumentBase(std::type_index t) : type(t) {}
123
124 const std::type_index type;
125};
126
129{
130public:
133 [[nodiscard]] constexpr std::string_view getShortName() const noexcept { return shortName; }
134
135protected:
136 explicit FlaggedArgumentBase(std::string_view shortName) : shortName(shortName) {}
137
138 std::string shortName;
139};
140
141} // namespace cli::commands
Base class for command-line arguments.
Definition argument.h:41
constexpr bool isRequired() const noexcept
Check if the argument is required.
Definition argument.h:63
virtual std::string getOptionsDocString(const docwriting::DocWriter &writer) const =0
Get the options documentation string for the argument.
virtual std::string getArgDocString(const docwriting::DocWriter &writer) const =0
Get the argument documentation string for the argument.
constexpr ArgumentKind getArgType() const
Get the type of the argument.
Definition argument.h:71
constexpr bool isRepeatable() const noexcept
Check if the argument is repeatable.
Definition argument.h:67
constexpr std::string_view getOptionComment() const noexcept
Get the option comment for the argument.
Definition argument.h:56
constexpr std::string_view getName() const noexcept
Get the name of the argument.
Definition argument.h:50
Base class for flag-like command-line arguments.
Definition argument.h:129
constexpr std::string_view getShortName() const noexcept
Get the short name of the argument.
Definition argument.h:133
Base class for typed command-line arguments.
Definition argument.h:108
virtual std::any parseToValue(const std::string &input) const =0
Parse the input string to the argument's value type.
std::type_index getType() const
Get the type of the argument.
Definition argument.h:114
Documentation writer for CLI commands. Consists of formatters for commands and arguments.
Definition docwriting.h:34