IODA Bundle
CommandLineParser.cc
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 /// \file CommandLineParser.h
11 ///
12 /// @author Piotr Kuchta, ECMWF, July 2009
13 
14 #include "odc/CommandLineParser.h"
15 
16 #include "eckit/exception/Exceptions.h"
17 #include "eckit/utils/Translator.h"
18 
19 namespace odc {
20 namespace tool {
21 
23 
25 : commandLineParsed_(false),
26  argc_(argc),
27  argv_(argv),
28  registeredOptionsWithArguments_(),
29  optionsWithArguments_(),
30  optionsNoArguments_(),
31  parameters_()
32 {}
33 
35 {
36  CommandLineParser& self = *this;
37 
39  self.parameters_ = const_cast<CommandLineParser&>(other).parameters();
40  self.optionsWithArguments_ = other.optionsWithArguments_;
41  self.optionsNoArguments_ = other.optionsNoArguments_;
42  self.commandLineParsed_ = true;
43 
44  ASSERT(self.commandLineParsed_ == other.commandLineParsed_);
45 }
46 
48 {
49  if (this == &other) return *this;
50 
51  CommandLineParser& self = *this;
52 
54  self.parameters_ = const_cast<CommandLineParser&>(other).parameters();
55  self.optionsWithArguments_ = other.optionsWithArguments_;
56  self.optionsNoArguments_ = other.optionsNoArguments_;
57  self.commandLineParsed_ = true;
58 
59  ASSERT(self.commandLineParsed_ == other.commandLineParsed_);
60 
61  return *this;
62 }
63 
64 int CommandLineParser::argc() { return argc_; }
65 
66 std::string CommandLineParser::argv(int i)
67 {
68  if (i >= argc_)
69  {
70  std::stringstream ss;
71  ss << "Expected at least " << i << " command line parameters";
72  throw eckit::UserError(ss.str());
73  }
74  return argv_[i];
75 }
76 
77 void CommandLineParser::registerOptionWithArgument(const std::string& option)
78 {
79  registeredOptionsWithArguments_.insert(std::string(option));
80 }
81 
82 const std::vector<std::string> CommandLineParser::parameters()
83 {
85  return parameters_;
86 }
87 
88 bool CommandLineParser::optionIsSet(const std::string& option)
89 {
91 
92  if (optionsNoArguments_.find(option) != optionsNoArguments_.end())
93  return true;
94 
95  return optionsWithArguments_.find(option) != optionsWithArguments_.end();
96 }
97 
98 template <typename T>
99 T CommandLineParser::optionArgument(const std::string& option, T defaultValue)
100 {
102 
103  std::map<std::string, std::string>::iterator it = optionsWithArguments_.find(option);
104  if (it == optionsWithArguments_.end())
105  return defaultValue;
106 
107  eckit::Translator<std::string, T> translator;
108  return translator(it->second);
109 }
110 
112 {
113  for (int i = 0; i < argc(); ++i)
114  {
115  std::string s = argv(i);
116  if (s[0] != '-' || s.size() == 1)
117  {
118  parameters_.push_back(s);
119  eckit::Log::debug() << "CommandLineParser::parseCommandLine: parameter: " << s << std::endl;
120  }
121  else
122  {
124  {
125  optionsWithArguments_[s] = argv(++i);
126  eckit::Log::debug() << "CommandLineParser::parseCommandLine: option with argument: "
127  << s << " = " << optionsWithArguments_[s] << std::endl;
128  }
129  else
130  {
131  optionsNoArguments_.insert(s);
132  eckit::Log::debug() << "CommandLineParser::parseCommandLine: option with no argument: " << s << std::endl;
133  }
134  }
135  }
136  commandLineParsed_ = true;
137 }
138 
139 void CommandLineParser::print(std::ostream& s) const
140 {
141  for (std::set<std::string>::const_iterator i = optionsNoArguments_.begin(); i != optionsNoArguments_.end(); ++i)
142  s << *i << " ";
143 
144  for (std::map<std::string, std::string>::const_iterator i = optionsWithArguments_.begin(); i != optionsWithArguments_.end(); ++i)
145  s << i->first << " " << i->second << " ";
146 
147  for (size_t i = 0; i < parameters_.size(); ++i)
148  s << parameters_[i] << " ";
149 
150 }
151 
152 // Template function's explicit instantiations.
153 
154 template std::string CommandLineParser::optionArgument(const std::string&, std::string);
155 template int CommandLineParser::optionArgument(const std::string&, int);
156 template long CommandLineParser::optionArgument(const std::string&, long);
157 template double CommandLineParser::optionArgument(const std::string&, double);
158 
159 } // namespace tool
160 } // namespace odc
161 
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)
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