Scarab  v3.5.0
Project 8 C++ Utility Library
nonoption_parser.cc
Go to the documentation of this file.
1 
2 #define SCARAB_API_EXPORTS
3 
4 #include "nonoption_parser.hh"
5 
6 #include "logger.hh"
7 #include "param_helpers.hh"
8 
9 #include <cctype>
10 #include <sstream>
11 
12 LOGGER( parselog, "nonoption_parser" );
13 
14 namespace scarab
15 {
16  nonoption_parser::nonoption_parser( std::vector< std::string > an_args ) :
17  f_ord_args(),
18  f_kw_args()
19  {
20  for( const std::string& arg : an_args )
21  {
22  parse( arg ); // can throw scarab::error
23  }
24  }
25 
27  {
28  }
29 
30  void nonoption_parser::parse( const std::string& an_arg )
31  {
32  // this method can throw scarab::error
33 
34  // if an_arg starts with '-', then it's either an option or it's a negative number.
35  if( an_arg[0] == f_option_starter )
36  {
37  param_ptr_t t_parsed( simple_parser::parse_value( an_arg ) );
38  param_value& t_value = (*t_parsed)();
39  if( t_value.is_int() || t_value.is_double() )
40  {
41  // if it parses as a number, then it's a negative number (no need to check uint, because presumably it's negative)
42  f_ord_args.push_back( std::move(t_parsed) );
43  return;
44  }
45  // if it's not a negtive number, then it's an option and shouldn't be considered here
46  throw error() << "Cannot parse an option with the nonoption_parser: " << an_arg;
47  }
48 
49  size_t t_val_pos = an_arg.find_first_of( f_value_separator );
50  if( t_val_pos != std::string::npos )
51  {
52  // this argument has an '=' (or whatever f_value_separator is), so parse as a keyword argument
54  an_arg.substr(0, t_val_pos),
55  simple_parser::parse_value( an_arg.substr(t_val_pos+1) ) ); // can throw scarab::error
56  f_kw_args.merge( t_new_param->as_node() );
57  }
58  else
59  {
60  // this argument has no '=', so parse it as an ordered argument
61  f_ord_args.push_back( simple_parser::parse_value( an_arg ) );
62  }
63 
64  return;
65  }
66 
67 }
static const char f_option_starter
static const char f_value_separator
static param_ptr_t parse_address(const std::string &an_addr, param_ptr_t a_value=param_ptr_t())
Converts an address into a nested param structure, and optionally attaches a value.
nonoption_parser(std::vector< std::string > an_args)
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
LOGGER(parselog, "nonoption_parser")
bool is_double() const
Definition: param_value.hh:504
std::unique_ptr< param > param_ptr_t
Definition: param_base.hh:23
void parse(const std::string &an_arg)
static param_ptr_t parse_value(const std::string &a_value)
Attempts to determine whether a value encoded as a string is an int, and unsigned int...
bool is_int() const
Definition: param_value.hh:499