CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
command.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_PUBLISH_COMMAND_H_
6 #define CVMFS_PUBLISH_COMMAND_H_
7 
8 #include <stdint.h>
9 
10 #include <cassert>
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 #include "util/single_copy.h"
16 #include "util/string.h"
17 
18 namespace publish {
19 
20 class Command {
21  friend class CmdHelp; // to set the progname_
22 
23  public:
30  struct Parameter {
35  explicit Parameter(const std::string &key)
36  : key(key)
37  , short_key('?')
38  , arg_name("")
39  , description("")
40  , is_switch(false)
41  , is_optional(false)
42  { }
43 
45  const std::string &key,
46  char short_key,
47  const std::string &arg_name,
48  const std::string &desc,
49  bool is_switch,
50  bool is_optional)
51  : key(key)
52  , short_key(short_key)
53  , arg_name(arg_name)
54  , description(desc)
55  , is_switch(is_switch)
56  , is_optional(is_optional)
57  {
58  assert(!key.empty());
59  assert(!is_switch || is_optional); // switches are always optional
60  assert(is_optional || !arg_name.empty());
61  }
62 
63  bool operator ==(const Parameter &other) const { return key == other.key; }
64  bool operator !=(const Parameter &other) const { return key != other.key; }
65  bool operator <(const Parameter &other) const { return key < other.key; }
66 
67  static Parameter Mandatory(const std::string &key, char short_key,
68  const std::string &arg_name,
69  const std::string &desc)
70  {
71  return Parameter(key, short_key, arg_name, desc, false, false);
72  }
73  static Parameter Optional(const std::string &key, char short_key,
74  const std::string &arg_name,
75  const std::string &desc)
76  {
77  return Parameter(key, short_key, arg_name, desc, false, true);
78  }
79  static Parameter Switch(const std::string &key, char short_key,
80  const std::string &desc)
81  {
82  return Parameter(key, short_key, "", desc, true, true);
83  }
84 
85  std::string key;
86  char short_key;
87  std::string arg_name;
88  std::string description;
89  bool is_switch;
91  };
92  typedef std::vector<Parameter> ParameterList;
93 
99  struct Argument {
100  Argument() : value_int(0) { }
101  explicit Argument(const std::string &v)
102  : value_str(v), value_int(String2Int64(v)) { }
103  std::string value_str;
104  int64_t value_int;
105  };
106 
110  class Options {
111  public:
112  void AppendPlain(const Argument &a) { plain_args_.push_back(a); }
113  void Set(const Parameter &p, const Argument &a) { map_[p] = a; }
114  bool Has(const std::string &key) const {
115  return map_.count(Parameter(key)) > 0;
116  }
117  bool HasNot(const std::string &key) const {
118  return !Has(key);
119  }
120  Argument Get(const std::string &key) const {
121  return map_.find(Parameter(key))->second;
122  }
123  std::string GetString(const std::string &key) const {
124  return map_.find(Parameter(key))->second.value_str;
125  }
126  std::string GetStringDefault(const std::string &key,
127  const std::string &default_value) const
128  {
129  if (Has(key))
130  return map_.find(Parameter(key))->second.value_str;
131  return default_value;
132  }
133  int GetInt(const std::string &key) const {
134  return map_.find(Parameter(key))->second.value_int;
135  }
136  unsigned GetSize() const { return map_.size(); }
137  const std::vector<Argument>& plain_args() const { return plain_args_; }
138 
139  private:
140  std::map<Parameter, Argument> map_;
141  std::vector<Argument> plain_args_;
142  };
143 
144  virtual ~Command() { }
145 
149  virtual std::string GetName() const = 0;
153  virtual std::string GetBrief() const = 0;
157  virtual std::string GetDescription() const { return GetBrief(); }
161  virtual ParameterList GetParams() const = 0;
162  virtual std::string GetUsage() const { return "[options]"; }
163  std::string GetExamples() const;
167  virtual unsigned GetMinPlainArgs() const { return 0; }
172  virtual bool IsHidden() const { return false; }
173 
174  Options ParseOptions(int argc, char** argv);
175 
180  virtual int Main(const Options &options) = 0;
181 
182  std::string progname() const { return progname_; }
183 
184  protected:
188  virtual std::vector<std::string> DoGetExamples() const {
189  return std::vector<std::string>();
190  }
191 
192  private:
193  std::string progname_;
194 };
195 
196 
198  public:
199  ~CommandList();
200  void TakeCommand(Command *command);
201  Command *Find(const std::string &name);
202 
203  const std::vector<Command *>& commands() const { return commands_; }
204 
205  private:
206  std::vector<Command *> commands_;
207 };
208 
209 } // namespace publish
210 
211 #endif // CVMFS_PUBLISH_COMMAND_H_
bool operator!=(const Parameter &other) const
Definition: command.h:64
int GetInt(const std::string &key) const
Definition: command.h:133
std::string GetStringDefault(const std::string &key, const std::string &default_value) const
Definition: command.h:126
bool Has(const std::string &key) const
Definition: command.h:114
std::string GetString(const std::string &key) const
Definition: command.h:123
static Parameter Optional(const std::string &key, char short_key, const std::string &arg_name, const std::string &desc)
Definition: command.h:73
virtual bool IsHidden() const
Definition: command.h:172
virtual std::vector< std::string > DoGetExamples() const
Definition: command.h:188
bool operator<(const Parameter &other) const
Definition: command.h:65
void TakeCommand(Command *command)
Definition: command.cc:134
std::string GetExamples() const
Definition: command.cc:23
assert((mem||(size==0))&&"Out Of Memory")
Command * Find(const std::string &name)
Definition: command.cc:123
const std::vector< Argument > & plain_args() const
Definition: command.h:137
std::string progname_
Definition: command.h:193
std::map< Parameter, Argument > map_
Definition: command.h:140
const std::vector< Command * > & commands() const
Definition: command.h:203
bool operator==(const Parameter &other) const
Definition: command.h:63
int64_t String2Int64(const string &value)
Definition: string.cc:240
static Parameter Switch(const std::string &key, char short_key, const std::string &desc)
Definition: command.h:79
Options ParseOptions(int argc, char **argv)
Definition: command.cc:33
std::vector< Command * > commands_
Definition: command.h:206
Argument(const std::string &v)
Definition: command.h:101
static Parameter Mandatory(const std::string &key, char short_key, const std::string &arg_name, const std::string &desc)
Definition: command.h:67
virtual std::string GetDescription() const
Definition: command.h:157
std::vector< Parameter > ParameterList
Definition: command.h:92
void Set(const Parameter &p, const Argument &a)
Definition: command.h:113
virtual int Main(const Options &options)=0
std::string description
Definition: command.h:88
virtual std::string GetUsage() const
Definition: command.h:162
virtual ~Command()
Definition: command.h:144
bool HasNot(const std::string &key) const
Definition: command.h:117
Parameter(const std::string &key)
Definition: command.h:35
virtual ParameterList GetParams() const =0
std::vector< Argument > plain_args_
Definition: command.h:141
std::string progname() const
Definition: command.h:182
virtual std::string GetBrief() const =0
Argument Get(const std::string &key) const
Definition: command.h:120
virtual unsigned GetMinPlainArgs() const
Definition: command.h:167
unsigned GetSize() const
Definition: command.h:136
void AppendPlain(const Argument &a)
Definition: command.h:112
virtual std::string GetName() const =0
Parameter(const std::string &key, char short_key, const std::string &arg_name, const std::string &desc, bool is_switch, bool is_optional)
Definition: command.h:44