ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
cli_app.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 <memory>
19#include <string>
20#include <string_view>
21#include <unordered_map>
22
23#include "cli_config.h"
24#include "commands/command_tree.h"
25#include "commands/docwriting/docwriting.h"
26#include "logging/logger.h"
27#include "parsing/parser.h"
28
29#define RUN_CLI_APP(cliInstance, argc_, argv_) \
30 try \
31 { \
32 return cliInstance.run(argc_, argv_); \
33 } \
34 catch (const std::exception &e) \
35 { \
36 cliInstance.Logger().error() << e.what() << std::endl; \
37 std::abort(); \
38 }
39
40namespace cli
41{
43class CliApp
44{
45public:
46 // Non-copyable
47 CliApp(const CliApp &) = delete;
48 CliApp &operator=(const CliApp &) = delete;
49
50 explicit CliApp(std::string_view executableName);
51 explicit CliApp(CliConfig &&config);
52 explicit CliApp(const CliConfig &config, std::unique_ptr<logging::AbstractLogger> logger);
53 ~CliApp() = default;
54
58 CliApp &withCommand(std::unique_ptr<commands::Command> subCommandPtr);
59
64
69 void init();
70
75 int run(int argc, char *argv[]);
76
79 [[nodiscard]] logging::AbstractLogger &Logger() { return *logger; }
80
83 [[nodiscard]] const commands::CommandTree &getCommandTree() const { return commandsTree; };
84
87 [[nodiscard]] commands::Command *getMainCommand() { return commandsTree.getRootCommand(); };
88
91 [[nodiscard]] CliConfig &getConfig() { return *configuration; };
92
95 [[nodiscard]] commands::docwriting::DocWriter &getDocWriter() { return docWriter; }
96
99 void setLogger(std::unique_ptr<logging::Logger> &&newLogger) { logger = std::move(newLogger); }
100
101private:
102 int internalRun(std::span<char *const> args);
103 bool rootShortCircuits(std::vector<std::string> &args, const cli::commands::Command &cmd) const;
104 bool commandShortCircuits(std::vector<std::string> &args, const cli::commands::Command *cmd) const;
105 bool initialized{false};
106 commands::CommandTree commandsTree;
107
108 std::unique_ptr<CliConfig> configuration;
109 std::unique_ptr<logging::AbstractLogger> logger;
110
111 parsing::Parser parser;
113};
114} // namespace cli
Main class representing a command-line application.
Definition cli_app.h:44
commands::Command * getMainCommand()
Get the command tree used by the CLI application.
Definition cli_app.h:87
CliApp & withCommand(std::unique_ptr< commands::Command > subCommandPtr)
Add a new command to the application.
Definition cli_app.cpp:56
logging::AbstractLogger & Logger()
Get the logger instance used by the CLI application.
Definition cli_app.h:79
void setLogger(std::unique_ptr< logging::Logger > &&newLogger)
Set the logger instance used by the CLI application.
Definition cli_app.h:99
const commands::CommandTree & getCommandTree() const
Get the command tree used by the CLI application.
Definition cli_app.h:83
commands::docwriting::DocWriter & getDocWriter()
Get the documentation writer used by the CLI application.
Definition cli_app.h:95
int run(int argc, char *argv[])
Run the CLI application with the given arguments.
Definition cli_app.cpp:80
void init()
Initialize the CLI application, preparing it for execution This method sets up internal structures an...
Definition cli_app.cpp:62
CliConfig & getConfig()
Get the configuration used by the CLI application.
Definition cli_app.h:91
Tree structure to manage commands and their subcommands.
Definition command_tree.h:56
Command * getRootCommand()
Get the root command of the tree.
Definition command_tree.h:120
Represents a command in the CLI application.
Definition command.h:36
Documentation writer for CLI commands. Consists of formatters for commands and arguments.
Definition docwriting.h:34
Abstract base class for logger implementations.
Definition logger.h:31
Holds the configuration for the CLI application.
Definition cli_config.h:24