ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
positional_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// untemplated base class for storing in same STL
30
34{
35public:
42 PositionalArgumentBase(std::string_view name, std::string_view optionsComment, bool repeatable,
43 bool required, std::type_index t)
45 ArgumentBase(name, optionsComment, ArgumentKind::Positional, repeatable, required)
46 {
47 }
48
49 [[nodiscard]] std::string getOptionsDocString(
50 const docwriting::DocWriter &writer) const override;
51 [[nodiscard]] std::string getArgDocString(const docwriting::DocWriter &writer) const override;
52};
53
56template <typename T> class PositionalArgument : public PositionalArgumentBase
57{
58
59public:
66 explicit PositionalArgument(std::string_view name, std::string_view optionsComment = "",
67 bool required = true, bool repeatable = false)
68 : PositionalArgumentBase(name, optionsComment, repeatable, required, typeid(T))
69 {
70 }
71
72 [[nodiscard]] std::any parseToValue(const std::string &input) const override;
73
74#pragma region ChainingMethods
75
81 PositionalArgument<T> &withOptionsComment(std::string_view comment)
82 {
83 optionsComment = comment;
84 return *this;
85 }
86
91 {
92 required = req;
93 return *this;
94 }
95
100 {
101 repeatable = rep;
102 return *this;
103 }
104
105 PositionalArgument<T> &&create() { return std::move(*this); }
106
107#pragma endregion ChainingMethods
108};
109
110template <typename T>
111inline std::any PositionalArgument<T>::parseToValue(const std::string &input) const
112{
113 return cli::parsing::ParseHelper::parse<T>(input);
114}
115
116template <typename T> PositionalArgument<T> &createPositionalArgument(std::string_view id)
117{
118 static thread_local PositionalArgument<T> instance(id);
119 return instance;
120}
121
122template <typename T>
123PositionalArgument<T> &createPositionalArgument(std::string_view id,
124 std::string_view optionsComment, bool required,
125 bool repeatable)
126{
127 static thread_local PositionalArgument<T> instance(id, optionsComment, required, repeatable);
128 return instance;
129}
130
131} // namespace cli::commands
Base class for command-line arguments.
Definition argument.h:41
Untemplated Base class for positional arguments in the CLI. Used to store all positional arguments in...
Definition positional_argument.h:34
PositionalArgumentBase(std::string_view name, std::string_view optionsComment, bool repeatable, bool required, std::type_index t)
Construct a new Positional Argument object.
Definition positional_argument.h:42
std::string getArgDocString(const docwriting::DocWriter &writer) const override
Get the argument documentation string for the argument.
Definition positional_argument.cpp:27
std::string getOptionsDocString(const docwriting::DocWriter &writer) const override
Get the options documentation string for the argument.
Definition positional_argument.cpp:22
Represents positional arguments in the CLI.
Definition positional_argument.h:57
PositionalArgument(std::string_view name, std::string_view optionsComment="", bool required=true, bool repeatable=false)
Construct a new Positional Argument object.
Definition positional_argument.h:66
PositionalArgument< T > & withRepeatable(bool rep)
Set whether the argument can be specified multiple times.
Definition positional_argument.h:99
std::any parseToValue(const std::string &input) const override
Parse the input string to the argument's value type.
Definition positional_argument.h:111
PositionalArgument< T > & withOptionsComment(std::string_view comment)
Set the options comment for the argument.
Definition positional_argument.h:81
PositionalArgument< T > & withRequired(bool req)
Set whether the argument is required.
Definition positional_argument.h:90
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