Loading...
Searching...
No Matches
cli11_helper.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <algorithm>
5#include <iomanip>
6#include <iostream>
7#include <sstream>
8
9#include "cli11_wrapper.hpp"
10
11namespace terra::util {
12
13/// @brief Just a small wrapper to set the default of the option to the value of the variable where it shall be stored.
14///
15/// Equivalent to
16/// @code
17/// app.add_option( name, field, std::move( desc ) )->default_val( field );
18/// @endcode
19/// but with this you do not have to pass 'field' twice :)
20CLI::Option* add_option_with_default( CLI::App& app, const std::string& name, auto& field, std::string desc = "" )
21{
22 return app.add_option( name, field, std::move( desc ) )->default_val( field );
23}
24
25/// @brief Just a small wrapper to set the default of the flag to the value of the variable where it shall be stored.
26///
27/// Equivalent to
28/// @code
29/// app.add_flag( name, field, std::move( desc ) )->default_val( field );
30/// @endcode
31/// but with this you do not have to pass 'field' twice :)
32inline CLI::Option* add_flag_with_default( CLI::App& app, const std::string& name, bool& field, std::string desc = "" )
33{
34 return app.add_flag( name, field, std::move( desc ) )->default_val( field );
35}
36
37/// @brief Prints an overview of the available flags, the passed arguments, defaults, etc.
38inline void print_cli_summary( const CLI::App& app, std::ostream& os = std::cout )
39{
40 struct Row
41 {
42 std::string name;
43 std::string count;
44 std::string value;
45 std::string def;
46 };
47
48 std::vector< Row > rows;
49 rows.reserve( app.get_options().size() );
50
51 for ( const auto* opt : app.get_options() )
52 {
53 if ( opt->get_name().empty() )
54 continue;
55
56 Row r;
57 r.name = opt->get_name();
58 r.count = std::to_string( opt->count() );
59
60 // Value(s)
61 if ( opt->count() > 0 )
62 {
63 std::ostringstream vals;
64 auto results = opt->results();
65 for ( size_t i = 0; i < results.size(); ++i )
66 {
67 if ( i > 0 )
68 vals << ' ';
69 vals << results[i];
70 }
71 r.value = vals.str();
72 }
73 else
74 {
75 r.value = "(not set)";
76 }
77
78 // Default (if any)
79 r.def = opt->get_default_str();
80
81 rows.push_back( std::move( r ) );
82 }
83
84 // Compute column widths dynamically
85 size_t w_name = std::max< size_t >( 4, std::max_element( rows.begin(), rows.end(), []( auto& a, auto& b ) {
86 return a.name.size() < b.name.size();
87 } )->name.size() );
88 size_t w_count = std::max< size_t >( 5, std::max_element( rows.begin(), rows.end(), []( auto& a, auto& b ) {
89 return a.count.size() < b.count.size();
90 } )->count.size() );
91 size_t w_value = std::max< size_t >( 5, std::max_element( rows.begin(), rows.end(), []( auto& a, auto& b ) {
92 return a.value.size() < b.value.size();
93 } )->value.size() );
94 size_t w_def = std::max< size_t >( 7, std::max_element( rows.begin(), rows.end(), []( auto& a, auto& b ) {
95 return a.def.size() < b.def.size();
96 } )->def.size() );
97
98 // Print header
99 os << std::left << std::setw( w_name + 2 ) << "Name" << std::setw( w_count + 2 ) << "Count"
100 << std::setw( w_value + 2 ) << "Value"
101 << "Default\n";
102
103 os << std::string( w_name + w_count + w_value + w_def + 8, '-' ) << "\n";
104
105 // Print rows
106 for ( const auto& r : rows )
107 {
108 os << std::left << std::setw( w_name + 2 ) << r.name << std::setw( w_count + 2 ) << r.count
109 << std::setw( w_value + 2 ) << r.value << r.def << "\n";
110 }
111}
112
113} // namespace terra::util
Definition solver.hpp:9
void print_cli_summary(const CLI::App &app, std::ostream &os=std::cout)
Prints an overview of the available flags, the passed arguments, defaults, etc.
Definition cli11_helper.hpp:38
CLI::Option * add_flag_with_default(CLI::App &app, const std::string &name, bool &field, std::string desc="")
Just a small wrapper to set the default of the flag to the value of the variable where it shall be st...
Definition cli11_helper.hpp:32
CLI::Option * add_option_with_default(CLI::App &app, const std::string &name, auto &field, std::string desc="")
Just a small wrapper to set the default of the option to the value of the variable where it shall be ...
Definition cli11_helper.hpp:20