24#include <unordered_set>
26#ifdef CHAIN_CLI_VERBOSE
30#include "logging/logger.h"
31#include "context_exception.h"
46 explicit CliContext(std::unique_ptr<std::unordered_map<std::string, std::any>> posArgs,
47 std::unique_ptr<std::unordered_map<std::string, std::any>> optArgs,
48 std::unique_ptr<std::unordered_set<std::string>> flagArgs,
50 : logger(logger), positionalArgs(std::move(posArgs)), optionArgs(std::move(optArgs)),
51 flagArgs(std::move(flagArgs))
86#ifdef CHAIN_CLI_VERBOSE
87 std::cout <<
"Getting positional argument '" << argName <<
"' as type " <<
typeid(T).name() <<
"\n";
89 return getAnyCast<T>(argName, *positionalArgs);
99 out = getAnyCast<T>(argName, *positionalArgs);
109#ifdef CHAIN_CLI_VERBOSE
110 std::cout <<
"Getting option argument '" << argName <<
"' as type " <<
typeid(T).name() <<
"\n";
112 return getAnyCast<T>(argName, *optionArgs);
120 template <
typename T>
void getOptionArg(
const std::string &argName, T &out)
const
122 out = getAnyCast<T>(argName, *optionArgs);
132#ifdef CHAIN_CLI_VERBOSE
133 std::cout <<
"Getting repeatable option argument '" << argName <<
"' as vector of type " <<
typeid(T).name() <<
"\n";
135 auto it = optionArgs->find(argName);
136 if (it == optionArgs->end())
141 auto anyVec = getAnyCast<std::vector<std::any>>(argName, *optionArgs);
142 std::vector<T> result;
143 result.reserve(anyVec.size());
144 for (
const auto &elem : anyVec)
146 result.push_back(std::any_cast<T>(elem));
151 catch (
const std::bad_any_cast &)
163 template <
typename T>
166#ifdef CHAIN_CLI_VERBOSE
167 std::cout <<
"Getting repeatable positional argument '" << argName <<
"' as vector of type " <<
typeid(T).name() <<
"\n";
169 auto it = positionalArgs->find(argName);
170 if (it == positionalArgs->end())
175 auto anyVec = getAnyCast<std::vector<std::any>>(argName, *positionalArgs);
176 std::vector<T> result;
177 result.reserve(anyVec.size());
178 for (
const auto &elem : anyVec)
180 result.push_back(std::any_cast<T>(elem));
185 catch (
const std::bad_any_cast &)
196 template <
typename T> T
getArg(
const std::string &argName)
const
198#ifdef CHAIN_CLI_VERBOSE
199 std::cout <<
"Getting any argument '" << argName <<
"' as type " <<
typeid(T).name() <<
"\n";
203 return getAnyCast<T>(argName, *positionalArgs);
207 return getAnyCast<T>(argName, *optionArgs);
222#ifdef CHAIN_CLI_VERBOSE
223 std::cout <<
"Getting repeatable argument '" << argName <<
"' as vector of type " <<
typeid(T).name() <<
"\n";
227 return getRepeatablePositionalArg<T>(argName);
231 return getRepeatableOptionArg<T>(argName);
246 std::unique_ptr<std::unordered_map<std::string, std::any>> positionalArgs;
247 std::unique_ptr<std::unordered_map<std::string, std::any>> optionArgs;
248 std::unique_ptr<std::unordered_set<std::string>> flagArgs;
250 template <
typename T>
251 static T getAnyCast(
const std::string &name, std::unordered_map<std::string, std::any> &dict)
255 auto it = dict.find(name);
256 if (it == dict.end())
258 throw MissingArgumentException(name, dict);
260 return std::any_cast<T>(it->second);
262 catch (
const std::bad_any_cast &)
264 throw InvalidArgumentTypeException(name,
typeid(T), dict.at(name).type());
Represents the context of a command-line interface (CLI) invocation and as such contains the parsed v...
Definition cli_context.h:39
CliContext(std::unique_ptr< std::unordered_map< std::string, std::any > > posArgs, std::unique_ptr< std::unordered_map< std::string, std::any > > optArgs, std::unique_ptr< std::unordered_set< std::string > > flagArgs, cli::logging::AbstractLogger &logger)
Constructs a new CliContext object from the passed argument maps.
Definition cli_context.h:46
T getArg(const std::string &argName) const
Gets the value of an argument.
Definition cli_context.h:196
bool isFlagPresent(const std::string &argName) const
Checks if a flag with the given name is present in the context.
Definition cli_context.cpp:36
void getOptionArg(const std::string &argName, T &out) const
Gets the value of an optional argument and stores it in the provided output variable.
Definition cli_context.h:120
std::vector< T > getRepeatableOptionArg(const std::string &argName) const
Gets all values of a repeatable option argument.
Definition cli_context.h:130
std::vector< T > getRepeatablePositionalArg(const std::string &argName) const
Gets all values of a repeatable positional argument.
Definition cli_context.h:164
T getPositionalArg(const std::string &argName) const
Gets the value of a positional argument.
Definition cli_context.h:84
bool isArgPresent(const std::string &argName) const
Checks if an argument with the given name is present in the context.
Definition cli_context.cpp:41
void getPositionalArg(const std::string &argName, T &out) const
Gets the value of a positional argument and stores it in the provided output variable.
Definition cli_context.h:97
bool isOptionArgPresent(const std::string &argName) const
Checks if an optional argument with the given name is present in the context.
Definition cli_context.cpp:26
T getOptionArg(const std::string &argName) const
Gets the value of an optional argument.
Definition cli_context.h:107
auto getRepeatableArg(const std::string &argName) const
Gets all values of a repeatable argument.
Definition cli_context.h:220
bool isPositionalArgPresent(const std::string &argName) const
Checks if a positional argument with the given name is present in the context.
Definition cli_context.cpp:31
Thrown when an argument type that was requested is not the one that was parsed.
Definition context_exception.h:42
Thrown when an argument that was requested is missing in the context.
Definition context_exception.h:27
Abstract base class for logger implementations.
Definition logger.h:31