R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
neuland_cli.cxx
Go to the documentation of this file.
1#define JSON_USE_IMPLICIT_CONVERSIONS 0
2
3#include <CLI/CLI.hpp>
4#include <R3BLogger.h>
6#include <fmt/format.h>
7#ifdef HAS_MPI
8#include <mpi.h>
9#endif
10
11auto main(int argc, char** argv) -> int
12{
13 auto is_failed = false;
14 auto num_proc = 0;
15 auto num_rank = 0;
16#ifdef HAS_MPI
17 MPI_Init(&argc, &argv);
18 MPI_Comm_size(MPI_COMM_WORLD, &num_proc);
19 MPI_Comm_rank(MPI_COMM_WORLD, &num_rank);
20#endif
21
22 auto app = std::unique_ptr<R3B::Neuland::Application>{};
23 try
24 {
25 auto program_options = CLI::App{ "Neuland command line interface" };
26
27 auto instantiate_app = [&app, &program_options](std::size_t)
28 {
29 if (program_options.got_subcommand("sim"))
30 {
31 app = std::make_unique<R3B::Neuland::SimulationApplication>();
32 app->setup_options(*program_options.get_subcommand("sim"));
33 }
34 if (program_options.got_subcommand("ana"))
35 {
36 app = std::make_unique<R3B::Neuland::AnalysisApplication>();
37 app->setup_options(*program_options.get_subcommand("ana"));
38 }
39 };
40
41 program_options.require_subcommand(1, 1);
42 program_options.add_subcommand("sim", "Run simulation application.")->preparse_callback(instantiate_app);
43 program_options.add_subcommand("ana", "Run analysis application.")->preparse_callback(instantiate_app);
44
45 CLI11_PARSE(program_options, argc, argv);
46
47 if (app == nullptr)
48 {
49 throw R3B::runtime_error("Application is not instantiated!");
50 }
51
52 if (app->has_print_default_options())
53 {
54 app->print_options();
55 return EXIT_SUCCESS;
56 }
57 if (app->has_dump())
58 {
59 app->dump_options();
60 return EXIT_SUCCESS;
61 }
62
63 app->set_rank_num(num_rank);
64 app->set_num_of_procs(num_proc);
65 app->init();
66 app->run();
67 }
68 catch (const std::exception& ex)
69 {
70 fmt::println("");
71 R3BLOG(error, fmt::format("Exception ocurred: \n\n{}\n", ex.what()));
72 is_failed = true;
73 app->set_fail(is_failed);
74 }
75
76#ifdef HAS_MPI
77 MPI_Finalize();
78#endif
79
80 if (is_failed)
81 {
82 return EXIT_FAILURE;
83 }
84 return EXIT_SUCCESS;
85}
#define R3BLOG(severity, x)
Definition R3BLogger.h:35
auto main() -> int
Definition ex_test.cxx:109