ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
flag_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 <typeindex>
22
23#include "argument.h"
24#include "parsing/parser_utils.h"
25
26namespace cli::commands
27{
28
31{
32public:
38 explicit FlagArgument(std::string_view name, std::string_view shortName = "",
39 std::string_view optionsComment = "", bool isRequired = false)
40 : ArgumentBase(name, optionsComment, ArgumentKind::Flag, false, isRequired),
41 FlaggedArgumentBase(shortName)
42 {
43 }
44
45 [[nodiscard]] std::string getOptionsDocString(
46 const docwriting::DocWriter &writer) const override;
47 [[nodiscard]] std::string getArgDocString(const docwriting::DocWriter &writer) const override;
48
49#pragma region ChainingMethods
55 FlagArgument &withOptionsComment(std::string_view comment)
56 {
57 optionsComment = comment;
58 return *this;
59 }
60
65 {
66 required = req;
67 return *this;
68 }
69
75 FlagArgument &withShortName(std::string_view name)
76 {
77 this->shortName = name;
78 return *this;
79 }
80#pragma endregion ChainingMethods
81};
82
83} // 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
Represents a flag argument in the CLI.
Definition flag_argument.h:31
std::string getArgDocString(const docwriting::DocWriter &writer) const override
Get the argument documentation string for the argument.
Definition flag_argument.cpp:27
FlagArgument & withShortName(std::string_view name)
Set the short name for the argument.
Definition flag_argument.h:75
FlagArgument & withOptionsComment(std::string_view comment)
Set the options comment for the argument.
Definition flag_argument.h:55
std::string getOptionsDocString(const docwriting::DocWriter &writer) const override
Get the options documentation string for the argument.
Definition flag_argument.cpp:22
FlagArgument & withRequired(bool req)
Set whether the argument is required.
Definition flag_argument.h:64
FlagArgument(std::string_view name, std::string_view shortName="", std::string_view optionsComment="", bool isRequired=false)
Construct a new Flag Argument object.
Definition flag_argument.h:38
Base class for flag-like command-line arguments.
Definition argument.h:129
Documentation writer for CLI commands. Consists of formatters for commands and arguments.
Definition docwriting.h:34