Kosim‎ > ‎

ProgramOptions

Kosim provides a simple to use program options parser implemented using Boost.Program_options library.
In the directory kosim/src/program_options there are the common files used by any test, these files do not need to be modified. They are symbolically linked in the directory where the shared library imported by python is built. An example is in main/generic_cpu dir where the script build_program_options_links.py builds the symbolic links to the common program options files from the directory program_options. The only files that need to be modified to match the arguments passed are program_options.h and program_options.cpp. These two files are located in the same directory as the script build_program_options_links.py. The program_options.h provides the accessors of the options to be parsed and the program_options.cpp registers these arguments:
  std::string& get_test_name                       () { return m_test_name           ; }  // part of the program_options.h

     mp_config_options->add_options()                                                                                                   // part of the program_options.cpp
        ("TestName"        , po::value<std::string>(&m_test_name)->default_value("test_x"), "test_name")    // part of the program_options.cpp
           ^^^^^^^^^^                                              ^^^^^^^^^^^^^^                          ^^^^^^^^      ^^^^^^^^
          option name                                 data member associated          default value    explanation when "--help" option is given

The options can be passed on the command line (e.g. --option_cmd_line) or in a configuration file (follows the *.ini format). The file main/generic_cpu/generic_cpu.cfg is an example. The options arguments can be accessed like:
cout << ProgramOptions::GetInstance()->get_test_name() << endl; // part of generic_cpu/gc_main.cpp

For the program options to be parsed, the python script needs to contain the following lines (see main/generic_cpu/generic_cpu.py):
opt_builder = kosim_generic_cpu.OptionsBuilder()
for arg in sys.argv:
     opt_builder.SetArgument(arg)
 opt_builder.BuildArgv()
 opt_builder.InitProgramOptions()