Claw  1.7.3
arguments.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #ifndef __CLAW_ARGUMENTS_HPP__
31 #define __CLAW_ARGUMENTS_HPP__
32 
33 #include <string>
34 #include <map>
35 #include <claw/ordered_set.hpp>
36 
37 namespace claw
38 {
50  class arguments
51  {
52  private:
53  typedef
54  std::map< std::string, std::list<std::string> > valued_arguments_map;
55 
56  public:
57  arguments();
58  explicit arguments( const std::string& prog_name );
59  arguments( int& argc, char** &argv );
60  arguments( int& argc, char** &argv,
61  const claw::math::ordered_set<std::string>& allowed );
62 
63  void parse( int& argc, char** &argv );
64  void parse( int& argc, char** &argv,
65  const claw::math::ordered_set<std::string>& allowed );
66 
67  bool has_value( const std::string& arg_name ) const;
68  bool only_integer_values( const std::string& arg_name ) const;
69  bool only_real_values( const std::string& arg_name ) const;
70 
71  const std::string& get_program_name() const;
72 
73  bool get_bool( const std::string& arg_name ) const;
74  int get_integer( const std::string& arg_name ) const;
75  double get_real( const std::string& arg_name ) const;
76  const std::string& get_string( const std::string& arg_name ) const;
77 
78  std::list<int> get_all_of_integer( const std::string& arg_name ) const;
79  std::list<double> get_all_of_real( const std::string& arg_name ) const;
80  std::list<std::string>
81  get_all_of_string( const std::string& arg_name ) const;
82 
83  void add_argument( const std::string& arg );
84 
85  private:
86  void parse( int& argc, char** &argv, bool always_allowed,
87  const claw::math::ordered_set<std::string>& allowed );
88  bool split_argument
89  ( const std::string& arg, std::string& name, std::string& value ) const;
90 
91  void remove_null_arguments( int& argc, char** &argv ) const;
92 
93  void process_boolean
94  ( char* &arg, bool always_allowed,
95  const claw::math::ordered_set<std::string>& allowed );
96 
97  private:
99  std::string m_program_name;
100 
103 
105  valued_arguments_map m_pairs;
106 
107  }; // class arguments
108 } // namespace claw
109 
110 #endif // __CLAW_ARGUMENTS_HPP__
claw::arguments::get_string
const std::string & get_string(const std::string &arg_name) const
Get the string value of an argument.
Definition: arguments.cpp:234
claw::arguments::get_bool
bool get_bool(const std::string &arg_name) const
Get the boolean state of an argument.
Definition: arguments.cpp:186
claw
This is the main namespace.
Definition: algorithm.hpp:33
claw::arguments::only_integer_values
bool only_integer_values(const std::string &arg_name) const
Tell if only integer values are associated to an argument.
Definition: arguments.cpp:133
claw::arguments::parse
void parse(int &argc, char **&argv)
Parse arguments.
Definition: arguments.cpp:97
claw::arguments::get_integer
int get_integer(const std::string &arg_name) const
Get the integer value of an argument.
Definition: arguments.cpp:197
claw::arguments::get_all_of_string
std::list< std::string > get_all_of_string(const std::string &arg_name) const
Get all string values of an argument.
Definition: arguments.cpp:304
claw::math::ordered_set< std::string >
claw::arguments::arguments
arguments()
Constructor.
Definition: arguments.cpp:41
claw::arguments::get_all_of_integer
std::list< int > get_all_of_integer(const std::string &arg_name) const
Get all integer values of an argument.
Definition: arguments.cpp:248
claw::arguments::add_argument
void add_argument(const std::string &arg)
Add an argument in our list.
Definition: arguments.cpp:325
claw::arguments::has_value
bool has_value(const std::string &arg_name) const
Tell if a value is associated to an argument.
Definition: arguments.cpp:123
claw::arguments
A class to manage the arguments of your program.
Definition: arguments.hpp:50
claw::arguments::only_real_values
bool only_real_values(const std::string &arg_name) const
Tell if only real values are associated to an argument.
Definition: arguments.cpp:155
claw::arguments::get_program_name
const std::string & get_program_name() const
Get the name of the program.
Definition: arguments.cpp:176
claw::arguments::get_all_of_real
std::list< double > get_all_of_real(const std::string &arg_name) const
Get all real values of an argument.
Definition: arguments.cpp:276
claw::arguments::get_real
double get_real(const std::string &arg_name) const
Get the real value of an argument.
Definition: arguments.cpp:215
ordered_set.hpp
A class to manage sets of ordered items.