ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
orchestrator_helper.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
12
14static constexpr const char* DEFAULT_COMMAND = "help";
15
17 {"name", 'n', RequiredArgument, "Assign a name to the cache container; defaults to 'default' if not provided."},
18 {"color", 0, NoArgument, "Enable colored output."},
19 {"no-color", 0, NoArgument, "Disable colored output."},
20 {"json", 0, NoArgument, "Json output."},
21 {"similarity", 'S', RequiredArgument, "Changes the similarity threshold to `SIMILARITY` for the current command only."},
22 {"help", 'h', NoArgument, "Show this help message."},
24};
25
27 std::unique_ptr<ICommand>& command,
28 const std::vector<std::pair<std::vector<std::string>, CommandsRegistry::CommandFactory>>& commands
29) {
30 return [&command, &commands](Context& ctx) mutable {
31 command = CommandsRegistry::get_command(ctx.command_name, commands);
32 if (command.get() != nullptr) {
33 return true;
34 }
35
36 std::string external_cmd = Config::config().program_name + "-" + ctx.command_name;
37 std::vector<char*> new_argv;
38 new_argv.push_back(const_cast<char*>(external_cmd.c_str()));
39 for (int i = 2; i < ctx.argc; ++i) {
40 new_argv.push_back(ctx.argv[i]);
41 }
42 new_argv.push_back(nullptr);
43 UtilsOSDependable::run_process(external_cmd.c_str(), new_argv.data());
44
45 throw CommandNotFoundError(ctx.command_name);
46
47 return true;
48 };
49}
50
51inline bool formatter_step(Context& ctx) {
52 enum Format format_output = Format::TEXT;
53 if (ctx.options.has("json"))
54 format_output = Format::JSON;
55 FormatterManager::set_format(format_output);
56 FormatterManager::set_formatter(std::make_shared<ConsoleFormatter>(true));
57 if (ctx.options.has("no-color"))
58 FormatterManager::set_formatter(std::make_shared<ConsoleFormatter>(false));
59 return true;
60}
61
63 return [&table](Context& ctx) {
64 auto result = table.load();
65
68
70 "Cache generated with Arkanjo v" +
71 (!params.version.empty() ? params.version : "?.?.?") +
72 " may be incompatible with the current version v" +
73 std::string(Config::config().project_version) +
74 ". Results may be inaccurate.");
75 }
76
77 if (auto similarity = ctx.options.get_as<double>("similarity"))
78 table.update_similarity(*similarity);
79
80 return true;
81 };
82}
83
84inline Step command_run_step(std::shared_ptr<ICommand> command, const OptionsCollector& collector) {
85 return [cmd = std::move(command), &collector](Context& ctx) mutable {
86 return cmd->validate(ctx.options) && cmd->do_run(ctx.command_name, ctx.options, &collector);
87 };
88}
89} // namespace OrchestratorHelper
Format
Definition: base.hpp:9
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 set_formatter(std::shared_ptr< IFormatter > f)
static void warn(const std::string &str)
static void set_format(Format f)
static PreprocessRunParams read_current_run_params()
read preprocessing parameters runs
Represents a similarity graph between functions (paths).
void update_similarity(double new_similarity_threshold)
Updates similarity threshold.
Cache_Compatibility load()
static int run_process(const char *cmd, char *const argv[])
Executes an external program, replacing the current process (exec-style).
std::unique_ptr< ICommand > get_command(const std::string &name, const std::vector< std::pair< std::vector< std::string >, CommandFactory > > &commands)
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)
Step similarity_step(Similarity_Table &table)
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.
std::function< bool(Context &)> Step
#define OPTION_END
This marks the end of a long array of options.
@ RequiredArgument
@ NoArgument
Logical path abstraction for source code elements.
Similarity relationships storage and analysis.
ParsedOptions options
bool has(const std::string &name) const
Checks whether an option exists.
std::string project_version
Current project version.
Definition: schema.hpp:19
std::string program_name
Name of the program.
Definition: schema.hpp:15