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