ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
preprocessor_main.cpp
Go to the documentation of this file.
1// As preprocessor is, in the moment, separated from orchestrator
2// we define a main function to use it.
3
7#include "../help/help.hpp"
9#include <cassert>
10#include <filesystem>
11#include <iomanip>
12#include <iostream>
13#include <string>
14
19
20int main(int argc, char* argv[]) {
21 auto& cfg = Config::config();
22 cfg.setDefaultConfig();
23 cfg.program_name = "arkanjo-preprocessor";
24
25 Orchestrator orchestrator;
26 Context ctx;
27
28 ctx.command_name = (argc > 1) ? argv[1] : OrchestratorHelper::DEFAULT_COMMAND;
29
30 ctx.argc = argc;
31 ctx.argv = argv;
32
33 static const std::vector<std::pair<std::vector<std::string>, CommandsRegistry::CommandFactory>> internal_commands = {
34 {{"build"}, [&]() { return std::make_unique<PreprocessorBuild>(); }},
35 {{"list", "ls"}, [&]() { return std::make_unique<PreprocessorList>(); }},
36 {{"clean"}, [&]() { return std::make_unique<PreprocessorClean>(); }},
37 {{"path"}, [&]() { return std::make_unique<PreprocessorPath>(); }},
38 };
39
40 std::unique_ptr<ICommand> command;
41 orchestrator.add_step(OrchestratorHelper::setup_command_step(command, internal_commands));
42
43 OptionsCollector collector;
44 orchestrator.add_step([&command, &collector](Context&) {
45 if (!command)
46 return false;
47
49 collector.add_options(command->options());
50
51 return true;
52 });
53
54 orchestrator.add_step(collector.make_parse_step(argc, argv));
56
57 orchestrator.add_step([&orchestrator, &command, &collector](Context& ctx) {
58 if (auto name_value = ctx.options.get("name"))
59 Config::config().name_container = *name_value;
60
61 orchestrator.add_step(OrchestratorHelper::command_run_step(std::move(command), collector));
62
63 return true;
64 });
65
66 try {
67 orchestrator.run_pipeline(ctx);
68 } catch (const CommandNotFoundError& e) {
69 if (ctx.command_name != "help" && !ctx.options.has("help"))
70 FormatterManager::warn(ctx.command_name + " is not a " + Config::config().program_name + " command.");
71 std::make_unique<Help>(internal_commands)->do_run(ctx.command_name, ctx.options);
72 return 1;
73 } catch (const CLIError& e) {
74 std::cerr << e.what() << "\n";
75 return 1;
76 } catch (const std::exception& e) {
77 std::cerr << "Unexpected error: " << e.what() << "\n";
78 return 1;
79 }
80 return 0;
81}
Base class for CLI-related errors.
Definition: cli_error.hpp:10
An error is thrown when an unknown command is passed.
Definition: cli_error.hpp:32
static Config & config()
Gets the singleton configuration instance.
Definition: config.cpp:68
static void warn(const std::string &str)
void add_options(const CliOption *long_opts)
Step make_parse_step(int argc, char *argv[])
Main command orchestrator.
void run_pipeline(Context &ctx)
void add_step(Step step)
std::function< std::unique_ptr< ICommand >()> CommandFactory
Step setup_command_step(std::unique_ptr< ICommand > &command, const std::vector< std::pair< std::vector< std::string >, CommandsRegistry::CommandFactory > > &commands)
constexpr CliOption global_long_opts[]
bool formatter_step(Context &ctx)
Step command_run_step(std::shared_ptr< ICommand > command, const OptionsCollector &collector)
Main command orchestration interface.
Codebase preprocessing interface.
Remove cached application data.
Interface for listing preprocessors in the system.
int main(int argc, char *argv[])
Interface for displaying the cache directory path.
char ** argv
ParsedOptions options
std::string command_name
bool has(const std::string &name) const
Checks whether an option exists.
std::optional< std::string_view > get(const std::string &name) const
Returns the value of an option.
std::string program_name
Name of the program.
Definition: schema.hpp:15
std::filesystem::path name_container
Name of the cache container.
Definition: schema.hpp:18