IODA Bundle
CommandLineParser.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 /// \file CommandLineParser.h
12 ///
13 /// @author Piotr Kuchta, ECMWF, July 2009
14 
15 #ifndef CommandLineParser_H
16 #define CommandLineParser_H
17 
18 #include <map>
19 #include <set>
20 #include <string>
21 #include <vector>
22 
23 
24 namespace odc {
25 namespace tool {
26 
28 public:
29 
30  CommandLineParser(int argc, char **argv);
31 
34 
35  virtual ~CommandLineParser();
36 
37  void registerOptionWithArgument(const std::string&);
38 
39  /// @return command line parameters (without the options starting with '-')
40  const std::vector<std::string> parameters();
41  std::string parameters(size_t i) { return parameters()[i]; }
42 
43  /// @return true if argumentless option is set
44  bool optionIsSet(const std::string&);
45 
46  /// @return value of the command line option passed to the tool converted
47  /// to given type, or value of the second parameter if option not
48  /// present on command line.
49  template <typename T> T optionArgument(const std::string&, T defaultValue);
50 
51  int argc();
52  std::string argv(int i);
53  char **argv() { return argv_; }
54 
55 private:
56 
57  void parseCommandLine();
58 
59 
61  int argc_;
62  char **argv_;
63 
64  std::set<std::string> registeredOptionsWithArguments_;
65 
66  std::map<std::string, std::string> optionsWithArguments_;
67  std::set<std::string> optionsNoArguments_;
68  std::vector<std::string> parameters_;
69 
70  void print(std::ostream& s) const;
71  friend std::ostream& operator<<(std::ostream& s, const CommandLineParser& o) { o.print(s); return s; }
72 };
73 
74 } // namespace tool
75 } // namespace odc
76 
77 #endif
78 
friend std::ostream & operator<<(std::ostream &s, const CommandLineParser &o)
CommandLineParser & operator=(const CommandLineParser &)
std::vector< std::string > parameters_
CommandLineParser(int argc, char **argv)
bool optionIsSet(const std::string &)
T optionArgument(const std::string &, T defaultValue)
std::string parameters(size_t i)
void registerOptionWithArgument(const std::string &)
void print(std::ostream &s) const
std::set< std::string > optionsNoArguments_
std::map< std::string, std::string > optionsWithArguments_
std::set< std::string > registeredOptionsWithArguments_
const std::vector< std::string > parameters()
Definition: ColumnInfo.h:23