CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_help.cc
Go to the documentation of this file.
1 
5 #include "cvmfs_config.h"
6 #include "cmd_help.h"
7 
8 #include <algorithm>
9 #include <string>
10 #include <vector>
11 
12 #include "publish/except.h"
13 #include "util/logging.h"
14 #include "util/string.h"
15 
16 using namespace std; // NOLINT
17 
18 namespace publish {
19 
20 int CmdHelp::Main(const Options &options) {
21  Command *cmd = commands_->Find(options.plain_args()[0].value_str);
22  if (cmd == NULL) {
23  LogCvmfs(kLogCvmfs, kLogStderr, "No help for '%s'",
24  options.plain_args()[0].value_str.c_str());
25  return 1;
26  }
27 
28  cmd->progname_ = progname();
29  LogCvmfs(kLogCvmfs, kLogStdout, "\nHelp for '%s'", cmd->GetName().c_str());
30  for (unsigned i = 0; i < cmd->GetName().length() + 11; ++i)
33  LogCvmfs(kLogCvmfs, kLogStdout, "%s\n", cmd->GetDescription().c_str());
34 
35  LogCvmfs(kLogCvmfs, kLogStdout, "Usage:");
36  LogCvmfs(kLogCvmfs, kLogStdout, "------");
37  LogCvmfs(kLogCvmfs, kLogStdout, " %s %s %s\n",
38  progname().c_str(), cmd->GetName().c_str(), cmd->GetUsage().c_str());
39 
40  std::string examples = cmd->GetExamples();
41  if (!examples.empty()) {
42  LogCvmfs(kLogCvmfs, kLogStdout, "Examples:");
43  LogCvmfs(kLogCvmfs, kLogStdout, "---------");
44  std::vector<std::string> ex_lines = SplitString(examples, '\n');
45  for (unsigned i = 0; i < ex_lines.size() - 1; ++i) {
46  LogCvmfs(kLogCvmfs, kLogStdout, " [%d] %s", i + 1, ex_lines[i].c_str());
47  }
49  }
50 
51  ParameterList params = cmd->GetParams();
52  if (params.empty()) return 0;
53 
54  LogCvmfs(kLogCvmfs, kLogStdout, "Options:");
55  LogCvmfs(kLogCvmfs, kLogStdout, "--------");
56  string::size_type max_len = 0;
57  for (unsigned i = 0; i < params.size(); ++i) {
58  string::size_type l = params[i].key.length();
59  if (!params[i].is_switch) {
60  l += 3 + params[i].arg_name.length();
61  }
62  max_len = std::max(max_len, l);
63  }
64  for (unsigned i = 0; i < params.size(); ++i) {
65  LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, " -%c, --%s%s",
66  params[i].short_key, params[i].key.c_str(),
67  params[i].is_switch ?
68  "" : (" <" + params[i].arg_name + ">").c_str());
69  unsigned l = params[i].key.length();
70  if (!params[i].is_switch) l += 3 + params[i].arg_name.length();
71  for (unsigned p = l; p < max_len; ++p)
73  LogCvmfs(kLogCvmfs, kLogStdout, " %s%s",
74  params[i].description.c_str(),
75  params[i].is_optional ? "" : " [mandatory]");
76  }
78 
79  return 0;
80 }
81 
82 } // namespace publish
std::string GetExamples() const
Definition: command.cc:23
const std::vector< Argument > & plain_args() const
Definition: command.h:137
std::string progname_
Definition: command.h:193
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:290
virtual std::string GetDescription() const
Definition: command.h:157
std::vector< Parameter > ParameterList
Definition: command.h:92
virtual std::string GetUsage() const
Definition: command.h:162
virtual ParameterList GetParams() const =0
virtual std::string GetName() const =0
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528