HelpCommand.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  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. #include "HelpCommand.h"
  17. #include "Lshal.h"
  18. namespace android {
  19. namespace lshal {
  20. std::string HelpCommand::GetName() {
  21. return "help";
  22. }
  23. std::string HelpCommand::getSimpleDescription() const {
  24. return "Print help message.";
  25. }
  26. Status HelpCommand::main(const Arg &arg) {
  27. if (optind >= arg.argc) {
  28. // `lshal help` prints global usage.
  29. mLshal.usage();
  30. return OK;
  31. }
  32. (void)usageOfCommand(arg.argv[optind]);
  33. return OK;
  34. }
  35. Status HelpCommand::usageOfCommand(const std::string& c) const {
  36. if (c.empty()) {
  37. mLshal.usage();
  38. return USAGE;
  39. }
  40. auto command = mLshal.selectCommand(c);
  41. if (command == nullptr) {
  42. // from HelpCommand::main, `lshal help unknown`
  43. mLshal.usage();
  44. return USAGE;
  45. }
  46. command->usage();
  47. return USAGE;
  48. }
  49. void HelpCommand::usage() const {
  50. mLshal.err()
  51. << "help:" << std::endl
  52. << " lshal -h" << std::endl
  53. << " lshal --help" << std::endl
  54. << " lshal help" << std::endl
  55. << " Print this help message" << std::endl;
  56. mLshal.forEachCommand([&](const Command* e) {
  57. mLshal.err() << " lshal help " << e->getName() << std::endl
  58. << " Print help message for " << e->getName() << std::endl;
  59. });
  60. }
  61. } // namespace lshal
  62. } // namespace android