CernVM-FS  2.13.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_help.cc
Go to the documentation of this file.
1 
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", progname().c_str(),
38  cmd->GetName().c_str(), cmd->GetUsage().c_str());
39 
40  const 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())
53  return 0;
54 
55  LogCvmfs(kLogCvmfs, kLogStdout, "Options:");
56  LogCvmfs(kLogCvmfs, kLogStdout, "--------");
57  string::size_type max_len = 0;
58  for (unsigned i = 0; i < params.size(); ++i) {
59  string::size_type l = params[i].key.length();
60  if (!params[i].is_switch) {
61  l += 3 + params[i].arg_name.length();
62  }
63  max_len = std::max(max_len, l);
64  }
65  for (unsigned i = 0; i < params.size(); ++i) {
66  LogCvmfs(
67  kLogCvmfs, kLogStdout | kLogNoLinebreak, " -%c, --%s%s",
68  params[i].short_key, params[i].key.c_str(),
69  params[i].is_switch ? "" : (" <" + params[i].arg_name + ">").c_str());
70  unsigned l = params[i].key.length();
71  if (!params[i].is_switch)
72  l += 3 + params[i].arg_name.length();
73  for (unsigned p = l; p < max_len; ++p)
75  LogCvmfs(kLogCvmfs, kLogStdout, " %s%s", params[i].description.c_str(),
76  params[i].is_optional ? "" : " [mandatory]");
77  }
79 
80  return 0;
81 }
82 
83 } // namespace publish
std::string GetExamples() const
Definition: command.cc:23
const std::vector< Argument > & plain_args() const
Definition: command.h:128
std::string progname_
Definition: command.h:184
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:306
virtual std::string GetDescription() const
Definition: command.h:148
std::vector< Parameter > ParameterList
Definition: command.h:86
virtual std::string GetUsage() const
Definition: command.h:153
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:545