Line data Source code
1 : /**
2 : * @file CommandLineInterpreter.hpp CommandLineInterpreter helper class
3 : *
4 : * CommandLineInterpreter takes the command-line arguments and produces a
5 : * configuration object for use by DAQProcess
6 : *
7 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
8 : * Licensing/copyright details are in the COPYING file that you should have
9 : * received with this code.
10 : */
11 :
12 : #ifndef APPFWK_SRC_COMMANDLINEINTERPRETER_HPP_
13 : #define APPFWK_SRC_COMMANDLINEINTERPRETER_HPP_
14 :
15 : #include "boost/program_options.hpp"
16 :
17 : #include <iostream>
18 : #include <string>
19 : #include <vector>
20 :
21 : namespace bpo = boost::program_options;
22 :
23 : namespace dunedaq::appfwk {
24 : /**
25 : * @brief CommandLineInterpreter parses the command-line options given to the
26 : * application and stores the results as validated data members
27 : *
28 : * @details Please note that contrary to the rest of the framework this class is not supposed to use ERS
29 : * because the ERS cannot be instantiated until the command line is parsed
30 : */
31 : struct CommandLineInterpreter
32 : {
33 : public:
34 : /**
35 : * @brief Parse the command line and return a CommandLineInterpreter struct
36 : * @param argc Number of arguments
37 : * @param argv Command-line arguments
38 : * @return CommandLineInterpreter structure with parsed arguments
39 : */
40 8 : static CommandLineInterpreter parse(int argc, char** argv)
41 : {
42 8 : CommandLineInterpreter output;
43 :
44 8 : std::ostringstream descstr;
45 8 : descstr << *argv
46 : << " known arguments (additional arguments will be stored and "
47 8 : "passed on)";
48 8 : bpo::options_description desc(descstr.str());
49 16 : desc.add_options()("name,n", bpo::value<std::string>()->required(), "Application name")(
50 16 : "sessionName,s", bpo::value<std::string>()->required(), "Session name")(
51 16 : "configurationId,k", bpo::value<std::string>()->required(), "Configuration id")(
52 16 : "commandFacility,c", bpo::value<std::string>()->required(), "CommandFacility URI")(
53 16 : "configurationService,d", bpo::value<std::string>()->required(), "Configuration Service URI")(
54 : "help,h", "produce help message");
55 :
56 8 : bpo::variables_map vm;
57 8 : auto parsed = bpo::command_line_parser(argc, argv).options(desc).allow_unregistered().run();
58 :
59 8 : output.other_options = bpo::collect_unrecognized(parsed.options, bpo::include_positional);
60 8 : bpo::store(parsed, vm);
61 :
62 8 : if (vm.count("help")) {
63 1 : std::cout << desc << std::endl; // NOLINT
64 1 : output.help_requested = true;
65 1 : return output;
66 : }
67 :
68 7 : bpo::notify(vm);
69 :
70 4 : output.app_name = vm["name"].as<std::string>();
71 4 : output.session_name = vm["sessionName"].as<std::string>();
72 4 : output.configuration_id = vm["configurationId"].as<std::string>();
73 4 : output.command_facility_plugin_name = vm["commandFacility"].as<std::string>();
74 4 : output.conf_service_plugin_name = vm["configurationService"].as<std::string>();
75 4 : return output;
76 20 : }
77 :
78 : bool help_requested{ false };
79 :
80 : std::string app_name{ "" };
81 : std::string session_name{ "" };
82 : std::string configuration_id{ "" };
83 : std::string command_facility_plugin_name{ "" };
84 : std::string conf_service_plugin_name{ "" };
85 :
86 : std::vector<std::string> other_options{}; ///< Any other options which were passed and not recognized
87 : };
88 : } // namespace dunedaq::appfwk
89 :
90 : #endif // APPFWK_SRC_COMMANDLINEINTERPRETER_HPP_
|