Scarab  v2.4.4
Project 8 C++ Utility Library
test_app_with_options.cc
Go to the documentation of this file.
1 /*
2  * test_app_with_options.cc
3  *
4  * Created on: Oct 28, 2018
5  * Author: N.S. Oblath
6  *
7  * Output examples:
8  *
9  > bin/test_app_with_options -f 5 -s "hello" -t "world"
10  2018-10-29 11:15:20 [ PROG] (tid 0x7fffb0945380) i/application.cc(97): Final configuration:
11 
12  {
13  third-value : world
14  }
15 
16  2018-10-29 11:15:20 [ PROG] (tid 0x7fffb0945380) i/application.cc(98): Ordered args:
17 
18  [
19  ]
20 
21  2018-10-29 11:15:20 [ PROG] (tid 0x7fffb0945380) _with_options.cc(69): First value: 5
22  2018-10-29 11:15:20 [ PROG] (tid 0x7fffb0945380) _with_options.cc(70): Second value: hello
23  2018-10-29 11:15:20 [ PROG] (tid 0x7fffb0945380) _with_options.cc(71): Third value: world
24  *
25  */
26 
27 #include "application.hh"
28 
29 #include "logger.hh"
30 
31 using namespace scarab;
32 
33 LOGGER( testlog, "test_app_with_options" );
34 
35 class test_app : public main_app
36 {
37  public:
39  main_app(),
40  f_first_value(),
41  f_second_value()
42  {
43  // Add an option that sets the value directly
44  add_option("-f,--first-value", f_first_value, "Set the first value" );
45 
46  // Add an option that sets the value via callback
47  add_option( "-s,--second-value", [this](std::vector< std::string > args) { f_second_value = args[0]; return true; }, "Set the second value" );
48 
49  // Add an option that sets the value into the configuration via callback
50  add_option( "-t,--third-value", [this](std::vector< std::string > args) { f_master_config.add( "third-value", args[0] ); return true; }, "Set the third value" );
51  }
52  virtual ~test_app() {}
53 
54  void set_first_value( int a_value ) { f_first_value = a_value; }
55  int get_first_value() { return f_first_value; }
56 
57  const std::string& second_value() const { return f_second_value; }
58  std::string& second_value() { return f_second_value; }
59 
60  private:
62  std::string f_second_value;
63 };
64 
65 int main( int argc, char **argv )
66 {
67  test_app the_main;
68 
69  CLI11_PARSE( the_main, argc, argv );
70 
71  LPROG( testlog, "First value: " << the_main.get_first_value() );
72  LPROG( testlog, "Second value: " << the_main.second_value() );
73  LPROG( testlog, "Third value: " << the_main.master_config()["third-value"]() );
74 
75  return 0;
76 }
std::string f_second_value
const std::string & second_value() const
std::string & second_value()
LOGGER(mtlog, "authentication")
#define LPROG(...)
Definition: logger.hh:363
int main(int argc, char **argv)
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
virtual ~test_app()
void set_first_value(int a_value)
Primary application class.
Definition: application.hh:247