PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cyad/CommandlineParser/CmdlineOpts.cpp

https://gitlab.com/github-cloud-corporation/cynara
C++ | 214 lines | 158 code | 34 blank | 22 comment | 5 complexity | 8b0d783f0da5d2b44151faf80108008e MD5 | raw file
  1. /*
  2. * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file src/cyad/CommandlineParser/CmdlineOpts.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @author Oskar Świtalski <o.switalski@samsung.com>
  20. * @version 1.0
  21. * @brief Command-line structs and helpers
  22. */
  23. #include <cassert>
  24. #include <sstream>
  25. #include "CmdlineOpts.h"
  26. namespace Cynara {
  27. namespace CmdlineOpts {
  28. const OptionsMap commandlineOptions = {
  29. { CmdlineOpt::SetBucket,
  30. { "set-bucket", CmdlineOpt::SetBucket, "set-bucket=<name>",
  31. "name of bucket to add or alter", OptHasArg::RequiredArgument, OptIsReq::NotRequired } },
  32. { CmdlineOpt::DeleteBucket,
  33. { "delete-bucket", CmdlineOpt::DeleteBucket, "delete-bucket=<name>",
  34. "name of bucket to delete", OptHasArg::RequiredArgument, OptIsReq::Required } },
  35. { CmdlineOpt::SetPolicy,
  36. { "set-policy", CmdlineOpt::SetPolicy, "",
  37. "", OptHasArg::NoArgument, OptIsReq::NotRequired } },
  38. { CmdlineOpt::Erase,
  39. { "erase", CmdlineOpt::Erase, "erase=<name>",
  40. "name of bucket to erase policies from", OptHasArg::RequiredArgument,
  41. OptIsReq::Required } },
  42. { CmdlineOpt::Check,
  43. { "check", CmdlineOpt::Check, "check",
  44. "", OptHasArg::NoArgument, OptIsReq::NotRequired } },
  45. { CmdlineOpt::ListPolicies,
  46. { "list-policies", CmdlineOpt::ListPolicies, "list-policies=<bucket>",
  47. "name of bucket to list policies from", OptHasArg::RequiredArgument,
  48. OptIsReq::Required } },
  49. { CmdlineOpt::ListPoliciesDesc,
  50. { "list-policies-descriptions", CmdlineOpt::ListPoliciesDesc, "list-policies-descriptions",
  51. "", OptHasArg::NoArgument, OptIsReq::NotRequired } },
  52. { CmdlineOpt::Type,
  53. { "type", CmdlineOpt::Type, "type=<type>",
  54. "policy type", OptHasArg::RequiredArgument, OptIsReq::Required } },
  55. { CmdlineOpt::Metadata,
  56. { "metadata", CmdlineOpt::Metadata, "metadata=<metadata>",
  57. "metadata for policy", OptHasArg::RequiredArgument, OptIsReq::NotRequired } },
  58. { CmdlineOpt::Client,
  59. { "client", CmdlineOpt::Client, "client=<client>",
  60. "client value", OptHasArg::RequiredArgument, OptIsReq::Required } },
  61. { CmdlineOpt::User,
  62. { "user", CmdlineOpt::User, "user=<user>",
  63. "user value", OptHasArg::RequiredArgument, OptIsReq::Required } },
  64. { CmdlineOpt::Privilege,
  65. { "privilege", CmdlineOpt::Privilege, "privilege=<privilege>",
  66. "privilege value", OptHasArg::RequiredArgument, OptIsReq::Required } },
  67. { CmdlineOpt::All,
  68. { "all", CmdlineOpt::All, "all", "list all - equivalent to -c \"#\" -u \"#\" -p \"#\"",
  69. OptHasArg::NoArgument, OptIsReq::NotRequired } },
  70. { CmdlineOpt::Bulk,
  71. { "bulk", CmdlineOpt::Bulk, "bulk=<filename>",
  72. "path or - for stdin", OptHasArg::RequiredArgument, OptIsReq::NotRequired } },
  73. { CmdlineOpt::Bucket,
  74. { "bucket", CmdlineOpt::Bucket, "bucket=<name>",
  75. "name of bucket; omit for default", OptHasArg::RequiredArgument,
  76. OptIsReq::NotRequired } },
  77. { CmdlineOpt::Recursive,
  78. { "recursive", CmdlineOpt::Recursive, "recursive=<yes|no>",
  79. "if linked buckets should be processed as well", OptHasArg::RequiredArgument,
  80. OptIsReq::Required } },
  81. { CmdlineOpt::Help,
  82. { "help", CmdlineOpt::Help, "help",
  83. "print help message", OptHasArg::NoArgument, OptIsReq::NotRequired } },
  84. { CmdlineOpt::Humanize,
  85. { "human-readable", CmdlineOpt::Humanize, "human-readable",
  86. "print policy types in human readable format", OptHasArg::NoArgument,
  87. OptIsReq::NotRequired } },
  88. };
  89. std::vector<option> makeLongOptions(const std::vector<CmdlineOpt> &opts) {
  90. std::vector<option> options;
  91. for (const auto &opt : opts) {
  92. const auto &o = commandlineOptions.at(opt);
  93. options.push_back({ o.longOption, o.hasArg, nullptr, static_cast<int>(opt) });
  94. }
  95. options.push_back({ nullptr, 0, nullptr, 0 });
  96. return options;
  97. }
  98. std::string makeShortOptions(const std::vector<CmdlineOpt> &opts) {
  99. std::stringstream optstr;
  100. optstr << ":";
  101. for (const auto &opt : opts) {
  102. const auto &o = commandlineOptions.at(opt);
  103. optstr << o.shortOption;
  104. if (o.hasArg == OptHasArg::RequiredArgument)
  105. optstr << ":";
  106. }
  107. return optstr.str();
  108. }
  109. std::string makeHelp(void) {
  110. std::size_t optWidth = 33;
  111. auto head = [] (const std::string &header, CmdlineOpt opt) -> std::string {
  112. return header + " (with -" + commandlineOptions.at(opt).shortOption
  113. + " or --" + commandlineOptions.at(opt).longOption + ")";
  114. };
  115. auto opt = [&optWidth] (CmdlineOpt opt) -> std::string {
  116. auto prefix = std::string(" -") + commandlineOptions.at(opt).shortOption
  117. + ", --" + commandlineOptions.at(opt).helpArgument;
  118. auto spaceLen = optWidth > prefix.length() ? optWidth - prefix.length() : 1;
  119. return prefix + std::string(spaceLen, ' ')
  120. + commandlineOptions.at(opt).helpDescription
  121. + (commandlineOptions.at(opt).isReq == Required ? " - required" : "");
  122. };
  123. std::stringstream helpStr;
  124. helpStr << "Usage: cyad [OPTIONS]" << std::endl << std::endl;
  125. helpStr << head("Bucket set options", CmdlineOpt::SetBucket) << std::endl;
  126. helpStr << opt(CmdlineOpt::SetBucket) << std::endl;
  127. helpStr << opt(CmdlineOpt::Type) << std::endl;
  128. helpStr << opt(CmdlineOpt::Metadata) << std::endl;
  129. helpStr << std::endl;
  130. helpStr << head("Bucket delete options", CmdlineOpt::DeleteBucket) << std::endl;
  131. helpStr << opt(CmdlineOpt::DeleteBucket) << std::endl;
  132. helpStr << std::endl;
  133. helpStr << head("Policy set options", CmdlineOpt::SetPolicy) << std::endl;
  134. helpStr << opt(CmdlineOpt::Bucket) << std::endl;
  135. helpStr << opt(CmdlineOpt::Client) << std::endl;
  136. helpStr << opt(CmdlineOpt::User) << std::endl;
  137. helpStr << opt(CmdlineOpt::Privilege) << std::endl;
  138. helpStr << opt(CmdlineOpt::Type) << std::endl;
  139. helpStr << opt(CmdlineOpt::Metadata) << std::endl;
  140. helpStr << opt(CmdlineOpt::Bulk) << std::endl;
  141. helpStr << std::endl;
  142. helpStr << "Bulk file" << std::endl;
  143. helpStr << " For each policy a single line should be present with the folowing information:";
  144. helpStr << std::endl;
  145. helpStr << " <bucket>;<client>;<user>;<privilege>;<type as decimal>;[metadata]" << std::endl;
  146. helpStr << " In stdin input mode, double empty line ends this mode." << std::endl;
  147. helpStr << std::endl;
  148. helpStr << head("Policy erase options", CmdlineOpt::Erase) << std::endl;
  149. helpStr << opt(CmdlineOpt::Erase) << std::endl;
  150. helpStr << opt(CmdlineOpt::Recursive) << std::endl;
  151. helpStr << opt(CmdlineOpt::Client) << std::endl;
  152. helpStr << opt(CmdlineOpt::User) << std::endl;
  153. helpStr << opt(CmdlineOpt::Privilege) << std::endl;
  154. helpStr << std::endl;
  155. helpStr << head("Administrative policy check options", CmdlineOpt::Check) << std::endl;
  156. helpStr << opt(CmdlineOpt::Bucket) << std::endl;
  157. helpStr << opt(CmdlineOpt::Recursive) << std::endl;
  158. helpStr << opt(CmdlineOpt::Client) << std::endl;
  159. helpStr << opt(CmdlineOpt::User) << std::endl;
  160. helpStr << opt(CmdlineOpt::Privilege) << std::endl;
  161. helpStr << opt(CmdlineOpt::Humanize) << std::endl;
  162. helpStr << std::endl;
  163. helpStr << head("Policies list options", CmdlineOpt::ListPolicies) << std::endl;
  164. helpStr << opt(CmdlineOpt::ListPolicies) << std::endl;
  165. helpStr << opt(CmdlineOpt::Client) << std::endl;
  166. helpStr << opt(CmdlineOpt::User) << std::endl;
  167. helpStr << opt(CmdlineOpt::Privilege) << std::endl;
  168. helpStr << opt(CmdlineOpt::All) << std::endl;
  169. helpStr << opt(CmdlineOpt::Humanize) << std::endl;
  170. helpStr << std::endl;
  171. helpStr << head("Policies descriptions list options", CmdlineOpt::ListPoliciesDesc)
  172. << std::endl;
  173. helpStr << opt(CmdlineOpt::ListPoliciesDesc) << std::endl;
  174. helpStr << std::endl;
  175. helpStr << head("Help options", CmdlineOpt::Help) << std::endl;
  176. helpStr << opt(CmdlineOpt::Help);
  177. return helpStr.str();
  178. }
  179. } /* namespace CmdlineOpts */
  180. } /* namespace Cynara */