GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/publish/command.h
Date: 2024-04-21 02:33:16
Exec Total Coverage
Lines: 0 55 0.0%
Branches: 0 44 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
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:
24 /**
25 * A parameter is information that can be passed by -$short_key or --$key to
26 * a command. Parameters can be boolean switches (they have no arguments).
27 * If they have an argument, parameters can be required for the command or
28 * optional.
29 */
30 struct Parameter {
31 /**
32 * This constructor should only be used for objects that are meant for
33 * comparison with / search of existing parameters.
34 */
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
44 Parameter(
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;
90 bool is_optional;
91 };
92 typedef std::vector<Parameter> ParameterList;
93
94 /**
95 * Encapsulates the value of parameters that are not switches. The conversion
96 * to an int is done unconditionally, which doesn't hurt even for string
97 * arguments.
98 */
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
107 /**
108 * Encapsulates parameter-argument pairs
109 */
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
146 /**
147 * Used to call the command as `cvmfs_server $GetName() ...`
148 */
149 virtual std::string GetName() const = 0;
150 /**
151 * A one-line explanation of what the command does
152 */
153 virtual std::string GetBrief() const = 0;
154 /**
155 * An extended description of the functionality
156 */
157 virtual std::string GetDescription() const { return GetBrief(); }
158 /**
159 * Returns the ordered array of possible parameters to this command
160 */
161 virtual ParameterList GetParams() const = 0;
162 virtual std::string GetUsage() const { return "[options]"; }
163 std::string GetExamples() const;
164 /**
165 * The command needs at least so many non-parameter arguments (e.g. fqrn)
166 */
167 virtual unsigned GetMinPlainArgs() const { return 0; }
168 /**
169 * Internal commands can be added that will be omitted from the printed list
170 * of available commands. By default, commands are visible though.
171 */
172 virtual bool IsHidden() const { return false; }
173
174 Options ParseOptions(int argc, char** argv);
175
176 /**
177 * The dictionary of passed arguments can be obtained by a call to
178 * ParseOptions()
179 */
180 virtual int Main(const Options &options) = 0;
181
182 std::string progname() const { return progname_; }
183
184 protected:
185 /**
186 * Example one-liners which get prepended by the command invocation
187 */
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
197 class CommandList : SingleCopy {
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_
212