ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
context_builder.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 <memory>
20#include <string>
21#include <string_view>
22#include <unordered_map>
23
24#include "cli_context.h"
25#include "logging/logger.h"
26
27namespace cli
28{
29
33{
34public:
37
42 ContextBuilder &addPositionalArgument(const std::string &argName, std::any &val);
43
48 ContextBuilder &addPositionalArgument(std::string_view argName, std::any &val);
49
50
55 ContextBuilder &addRepeatablePositionalArgument(const std::string &argName, const std::vector<std::any> &vals);
56
61 ContextBuilder &addRepeatablePositionalArgument(std::string_view argName, const std::vector<std::any> &vals);
62
67 ContextBuilder &addOptionArgument(const std::string &argName, std::any &val);
68
73 ContextBuilder &addOptionArgument(std::string_view argName, std::any &val);
74
79 ContextBuilder &addRepeatableOptionArgument(const std::string &argName, const std::vector<std::any> &vals);
80
85 ContextBuilder &addRepeatableOptionArgument(std::string_view argName, const std::vector<std::any> &vals);
86
90 ContextBuilder &addFlagArgument(const std::string &argName);
91
95 ContextBuilder &addFlagArgument(std::string_view argName);
96
100 bool isArgPresent(const std::string &argName) const;
101
105 std::unique_ptr<CliContext> build(cli::logging::AbstractLogger &logger);
106
107private:
108 std::unique_ptr<std::unordered_map<std::string, std::any>> positionalArgs;
109 std::unique_ptr<std::unordered_map<std::string, std::any>> optionalArgs;
110 std::unique_ptr<std::unordered_set<std::string>> flagArgs;
111};
112
113} // namespace cli
Builder for CliContext objects, allowing to incrementally add arguments before constructing the final...
Definition context_builder.h:33
ContextBuilder & addPositionalArgument(const std::string &argName, std::any &val)
Add a positional argument to the context being built.
Definition context_builder.cpp:32
ContextBuilder & addFlagArgument(const std::string &argName)
Add a flag argument to the context being built.
Definition context_builder.cpp:114
ContextBuilder & addOptionArgument(const std::string &argName, std::any &val)
Add an optional argument to the context being built.
Definition context_builder.cpp:73
std::unique_ptr< CliContext > build(cli::logging::AbstractLogger &logger)
Builds the final CliContext object from the accumulated arguments.
Definition context_builder.cpp:135
ContextBuilder()
Constructs a new ContextBuilder instance.
Definition context_builder.cpp:25
bool isArgPresent(const std::string &argName) const
Checks if an argument with the given name is present in the context being built.
Definition context_builder.cpp:129
ContextBuilder & addRepeatableOptionArgument(const std::string &argName, const std::vector< std::any > &vals)
Add a repeatable optional argument to the context being built.
Definition context_builder.cpp:87
ContextBuilder & addRepeatablePositionalArgument(const std::string &argName, const std::vector< std::any > &vals)
Add a repeatable positional argument to the context being built.
Definition context_builder.cpp:46
Abstract base class for logger implementations.
Definition logger.h:31