Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
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
6#include "../help/help.hpp"
7#include <cassert>
8#include <filesystem>
9#include <iomanip>
10#include <iostream>
11#include <string>
12
17using namespace std;
18
19int main(int argc, char* argv[]) {
20 auto& cfg = Config::config();
21 cfg.setDefaultConfig();
22 cfg.program_name = "arkanjo-preprocessor";
23
24 Orchestrator orchestrator;
25 Context ctx;
26
27 ctx.command_name = (argc > 1) ? argv[1] : OrchestratorHelper::DEFAULT_COMMAND;
28
29 ctx.argc = argc;
30 ctx.argv = argv;
31
32 static const std::vector<std::pair<std::vector<std::string>, CommandsRegistry::CommandFactory>> internal_commands = {
33 {{"build"}, [&]() { return std::make_unique<PreprocessorBuild>(); }},
34 {{"list", "ls"}, [&]() { return std::make_unique<PreprocessorList>(); }}
35 };
36
37 std::unique_ptr<ICommand> command;
38 orchestrator.add_step(OrchestratorHelper::setup_command_step(command, internal_commands));
39
40 OptionsCollector collector;
41 orchestrator.add_step([&command, &collector](Context&) {
42 if (!command)
43 return false;
44
46 collector.add_options(command->options());
47
48 return true;
49 });
50
51 orchestrator.add_step(collector.make_parse_step(argc, argv));
53
54 orchestrator.add_step([&orchestrator, &command, &collector](Context&) {
55 orchestrator.add_step(OrchestratorHelper::command_run_step(std::move(command), collector));
56
57 return true;
58 });
59
60 try {
61 orchestrator.run_pipeline(ctx);
62 } catch (const CommandNotFoundError& e) {
63 if (ctx.command_name != "help" && ctx.options.args.count("help") == 0)
64 FormatterManager::warn(ctx.command_name + " is not a " + Config::config().program_name + " command.");
65 std::make_unique<Help>(internal_commands)->do_run(ctx.command_name, ctx.options);
66 return 1;
67 } catch (const CLIError& e) {
68 std::cerr << e.what() << "\n";
69 return 1;
70 } catch (const std::exception& e) {
71 std::cerr << "Unexpected error: " << e.what() << "\n";
72 return 1;
73 }
74 return 0;
75}
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:37
std::string program_name
Name of the program.
Definition config.hpp:41
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.
Interface for listing preprocessors in the system.
int main(int argc, char *argv[])
char ** argv
ParsedOptions options
std::string command_name
std::map< std::string, std::string > args
Map from option name to value.