ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
option_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 <ostream>
20#include <string>
21#include <string_view>
22#include <typeindex>
23
24#include "argument.h"
25#include "parsing/parser_utils.h"
26
27namespace cli::commands
28{
29
33{
34public:
43 OptionArgumentBase(std::string_view name, std::string_view optionsComment, bool repeatable,
44 bool required, std::type_index t, std::string_view shortName,
45 std::string_view valueName)
47 ArgumentBase(name, optionsComment, ArgumentKind::Option, repeatable, required),
48 FlaggedArgumentBase(shortName), valueName(valueName)
49 {
50 }
51
52 [[nodiscard]] std::string getOptionsDocString(
53 const docwriting::DocWriter &writer) const override;
54 [[nodiscard]] std::string getArgDocString(const docwriting::DocWriter &writer) const override;
55 [[nodiscard]] constexpr std::string_view getValueName() const noexcept { return valueName; }
56
57protected:
58 const std::string valueName;
59};
60
63template <typename T> class OptionArgument : public OptionArgumentBase
64{
65
66public:
75 explicit OptionArgument(std::string_view name, std::string_view valueName,
76 std::string_view shortName = "", std::string_view optionsComment = "",
77 bool required = false, bool repeatable = false)
78 : OptionArgumentBase(name, optionsComment, repeatable, required, typeid(T), shortName,
79 valueName)
80 {
81 }
82
83 [[nodiscard]] std::any parseToValue(const std::string &input) const override;
84
85#pragma region ChainingMethods
86
92 OptionArgument<T> &withOptionsComment(std::string_view comment)
93 {
94 this->optionsComment = comment;
95 return *this;
96 }
97
102 {
103 required = req;
104 return *this;
105 }
106
112 OptionArgument<T> &withShortName(std::string_view name)
113 {
114 this->shortName = name;
115 return *this;
116 }
117
122 {
123 repeatable = rep;
124 return *this;
125 }
126};
127
128template <typename T>
129inline std::any OptionArgument<T>::parseToValue(const std::string &input) const
130{
131 return cli::parsing::ParseHelper::parse<T>(input);
132}
133} // namespace cli::commands
Base class for command-line arguments.
Definition argument.h:41
Base class for flag-like command-line arguments.
Definition argument.h:129
Untemplated base class for option arguments in the CLI. Used to store all option arguments in a singl...
Definition option_argument.h:33
std::string getOptionsDocString(const docwriting::DocWriter &writer) const override
Get the options documentation string for the argument.
Definition option_argument.cpp:22
std::string getArgDocString(const docwriting::DocWriter &writer) const override
Get the argument documentation string for the argument.
Definition option_argument.cpp:27
OptionArgumentBase(std::string_view name, std::string_view optionsComment, bool repeatable, bool required, std::type_index t, std::string_view shortName, std::string_view valueName)
Construct a new Option Argument object.
Definition option_argument.h:43
Represents option arguments in the CLI.
Definition option_argument.h:64
OptionArgument< T > & withRepeatable(bool rep)
Set whether the argument can be specified multiple times.
Definition option_argument.h:121
OptionArgument(std::string_view name, std::string_view valueName, std::string_view shortName="", std::string_view optionsComment="", bool required=false, bool repeatable=false)
Construct a new Option Argument object.
Definition option_argument.h:75
OptionArgument< T > & withShortName(std::string_view name)
Set the short name for the argument.
Definition option_argument.h:112
std::any parseToValue(const std::string &input) const override
Parse the input string to the argument's value type.
Definition option_argument.h:129
OptionArgument< T > & withOptionsComment(std::string_view comment)
Set the options comment for the argument.
Definition option_argument.h:92
OptionArgument< T > & withRequired(bool req)
Set whether the argument is required.
Definition option_argument.h:101
Base class for typed command-line arguments.
Definition argument.h:108
Documentation writer for CLI commands. Consists of formatters for commands and arguments.
Definition docwriting.h:34