CernVM-FS  2.13.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  Parameter(const std::string &key,
44  char short_key,
45  const std::string &arg_name,
46  const std::string &desc,
47  bool is_switch,
48  bool is_optional)
49  : key(key)
50  , short_key(short_key)
51  , arg_name(arg_name)
52  , description(desc)
53  , is_switch(is_switch)
54  , is_optional(is_optional) {
55  assert(!key.empty());
56  assert(!is_switch || is_optional); // switches are always optional
57  assert(is_optional || !arg_name.empty());
58  }
59 
60  bool operator==(const Parameter &other) const { return key == other.key; }
61  bool operator!=(const Parameter &other) const { return key != other.key; }
62  bool operator<(const Parameter &other) const { return key < other.key; }
63 
64  static Parameter Mandatory(const std::string &key, char short_key,
65  const std::string &arg_name,
66  const std::string &desc) {
67  return Parameter(key, short_key, arg_name, desc, false, false);
68  }
69  static Parameter Optional(const std::string &key, char short_key,
70  const std::string &arg_name,
71  const std::string &desc) {
72  return Parameter(key, short_key, arg_name, desc, false, true);
73  }
74  static Parameter Switch(const std::string &key, char short_key,
75  const std::string &desc) {
76  return Parameter(key, short_key, "", desc, true, true);
77  }
78 
79  std::string key;
80  char short_key;
81  std::string arg_name;
82  std::string description;
83  bool is_switch;
85  };
86  typedef std::vector<Parameter> ParameterList;
87 
93  struct Argument {
94  Argument() : value_int(0) { }
95  explicit Argument(const std::string &v)
96  : value_str(v), value_int(String2Int64(v)) { }
97  std::string value_str;
98  int64_t value_int;
99  };
100 
104  class Options {
105  public:
106  void AppendPlain(const Argument &a) { plain_args_.push_back(a); }
107  void Set(const Parameter &p, const Argument &a) { map_[p] = a; }
108  bool Has(const std::string &key) const {
109  return map_.count(Parameter(key)) > 0;
110  }
111  bool HasNot(const std::string &key) const { return !Has(key); }
112  Argument Get(const std::string &key) const {
113  return map_.find(Parameter(key))->second;
114  }
115  std::string GetString(const std::string &key) const {
116  return map_.find(Parameter(key))->second.value_str;
117  }
118  std::string GetStringDefault(const std::string &key,
119  const std::string &default_value) const {
120  if (Has(key))
121  return map_.find(Parameter(key))->second.value_str;
122  return default_value;
123  }
124  int GetInt(const std::string &key) const {
125  return map_.find(Parameter(key))->second.value_int;
126  }
127  unsigned GetSize() const { return map_.size(); }
128  const std::vector<Argument> &plain_args() const { return plain_args_; }
129 
130  private:
131  std::map<Parameter, Argument> map_;
132  std::vector<Argument> plain_args_;
133  };
134 
135  virtual ~Command() { }
136 
140  virtual std::string GetName() const = 0;
144  virtual std::string GetBrief() const = 0;
148  virtual std::string GetDescription() const { return GetBrief(); }
152  virtual ParameterList GetParams() const = 0;
153  virtual std::string GetUsage() const { return "[options]"; }
154  std::string GetExamples() const;
158  virtual unsigned GetMinPlainArgs() const { return 0; }
163  virtual bool IsHidden() const { return false; }
164 
165  Options ParseOptions(int argc, char **argv);
166 
171  virtual int Main(const Options &options) = 0;
172 
173  std::string progname() const { return progname_; }
174 
175  protected:
179  virtual std::vector<std::string> DoGetExamples() const {
180  return std::vector<std::string>();
181  }
182 
183  private:
184  std::string progname_;
185 };
186 
187 
189  public:
190  ~CommandList();
191  void TakeCommand(Command *command);
192  Command *Find(const std::string &name);
193 
194  const std::vector<Command *> &commands() const { return commands_; }
195 
196  private:
197  std::vector<Command *> commands_;
198 };
199 
200 } // namespace publish
201 
202 #endif // CVMFS_PUBLISH_COMMAND_H_
bool operator!=(const Parameter &other) const
Definition: command.h:61
int GetInt(const std::string &key) const
Definition: command.h:124
std::string GetStringDefault(const std::string &key, const std::string &default_value) const
Definition: command.h:118
bool Has(const std::string &key) const
Definition: command.h:108
std::string value_str
Definition: command.h:97
std::string GetString(const std::string &key) const
Definition: command.h:115
static Parameter Optional(const std::string &key, char short_key, const std::string &arg_name, const std::string &desc)
Definition: command.h:69
virtual bool IsHidden() const
Definition: command.h:163
virtual std::vector< std::string > DoGetExamples() const
Definition: command.h:179
bool operator<(const Parameter &other) const
Definition: command.h:62
void TakeCommand(Command *command)
Definition: command.cc:136
std::string GetExamples() const
Definition: command.cc:23
assert((mem||(size==0))&&"Out Of Memory")
Command * Find(const std::string &name)
Definition: command.cc:125
const std::vector< Argument > & plain_args() const
Definition: command.h:128
std::string progname_
Definition: command.h:184
std::map< Parameter, Argument > map_
Definition: command.h:131
const std::vector< Command * > & commands() const
Definition: command.h:194
bool operator==(const Parameter &other) const
Definition: command.h:60
int64_t String2Int64(const string &value)
Definition: string.cc:234
static Parameter Switch(const std::string &key, char short_key, const std::string &desc)
Definition: command.h:74
Options ParseOptions(int argc, char **argv)
Definition: command.cc:33
std::vector< Command * > commands_
Definition: command.h:197
Argument(const std::string &v)
Definition: command.h:95
static Parameter Mandatory(const std::string &key, char short_key, const std::string &arg_name, const std::string &desc)
Definition: command.h:64
virtual std::string GetDescription() const
Definition: command.h:148
std::vector< Parameter > ParameterList
Definition: command.h:86
void Set(const Parameter &p, const Argument &a)
Definition: command.h:107
virtual int Main(const Options &options)=0
std::string description
Definition: command.h:82
virtual std::string GetUsage() const
Definition: command.h:153
virtual ~Command()
Definition: command.h:135
bool HasNot(const std::string &key) const
Definition: command.h:111
Parameter(const std::string &key)
Definition: command.h:35
virtual ParameterList GetParams() const =0
std::vector< Argument > plain_args_
Definition: command.h:132
std::string progname() const
Definition: command.h:173
virtual std::string GetBrief() const =0
Argument Get(const std::string &key) const
Definition: command.h:112
virtual unsigned GetMinPlainArgs() const
Definition: command.h:158
unsigned GetSize() const
Definition: command.h:127
void AppendPlain(const Argument &a)
Definition: command.h:106
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:43