CbmRoot
Loading...
Searching...
No Matches
services/tsa_dump/ProgramOptions.cxx
Go to the documentation of this file.
1/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Pierre-Alain Loizeau [committer] */
4
5#include "ProgramOptions.h"
6
7#include <boost/program_options.hpp>
8
9#include <functional>
10#include <iostream>
11
12namespace po = boost::program_options;
13
14ProgramOptions::ProgramOptions(int argc, char** argv)
15{
16 po::options_description required("Required options");
17 // clang-format off
18 required.add_options()
19 ("filename,f", po::value(&sFullFilename)->value_name("<outputDir>")->required(),
20 "Full path to TSA file")
21 ;
22 // clang-format on
23
24 po::options_description optional{"Other options"};
25 // clang-format off
26 optional.add_options()
27 ("timeslices,n", po::value(&uNbTimeslices),
28 "number of timeslices")
29 ("sys,s", po::value<std::string>()->notifier(std::bind(&ProgramOptions::ConvertSysId, this, std::placeholders::_1)),
30 "SysId to select (component level), in hex! 0x00 to 0xFF!")
31 // Hint: cannot use std::underlying_type_t<fles::Subsystem> directly for two reasons (-_-)
32 // 1) this resolves to uint8_t and boost then want a character as program argument, not a number (which is typically
33 // multiple characters)
34 // 2) boost program_options cannot digest hexadecimal numbers (0xYY) as input
35 ("mspercomp,m", po::value(&nbMsPerComp),
36 "number of microslices to dump per component")
37 ("help,h", "Print help message")
38 ;
39 // clang-format on
40
41
42 po::options_description cmdline_options;
43 cmdline_options.add(required).add(optional);
44
45 po::variables_map vm;
46 po::command_line_parser parser{argc, argv};
47 parser.options(cmdline_options);
48 try {
49 auto result = parser.run();
50 po::store(result, vm);
51 }
52 catch (const std::exception& e) {
53 std::cerr << "Error: " << e.what() << std::endl;
54 std::cerr << "Use '-h' to display all valid options." << std::endl;
55 std::exit(EXIT_FAILURE);
56 }
57
58 if (vm.count("help") > 0) {
59 std::cout << cmdline_options << std::endl;
60 std::exit(EXIT_SUCCESS);
61 }
62
63 try {
64 po::notify(vm);
65 }
66 catch (const po::required_option& e) {
67 std::cerr << "Error: " << e.what() << std::endl;
68 std::cerr << "Use '-h' to display all valid options." << std::endl;
69 std::exit(EXIT_FAILURE);
70 }
71 catch (const po::invalid_option_value& e) {
72 std::cerr << "Error: " << e.what() << std::endl;
73 std::cerr << "Use '-h' to display all valid options." << std::endl;
74 std::exit(EXIT_FAILURE);
75 }
76}
77
78void ProgramOptions::ConvertSysId(const std::string& sOption)
79{
80 std::stringstream interpreter;
81 interpreter << std::hex << sOption;
82 interpreter >> selSysId;
83
84 if (0xFF < selSysId) {
85 std::stringstream ss;
86 // Not sure why the option name is not shown at same place as in default error message, probably wrong step in boost
87 // program_options calls sequence or wrong error type.... gave up, not important
88 ss << sOption << ": Value out of range (bigger than 0xFF) for --sys";
89 throw po::invalid_option_value(ss.str());
90 }
91}
void ConvertSysId(const std::string &option)
ProgramOptions(int argc, char *argv[])
Standard constructor with command line arguments.