Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a8bedc6a authored by Yifan Hong's avatar Yifan Hong
Browse files

lshal: add HelpCommand

Add *Command::usage() function for each Command and let
Lshal class to call them.

Suppress output from getopt_long and write our own
error message to customized error stream (for testing).

Test: lshal_test
Test: lshal --help

Change-Id: I8f5847c84a3e01af29fa85871479cab3baeb5312
parent ded398e5
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ cc_library_shared {
    ],
    srcs: [
        "DebugCommand.cpp",
        "HelpCommand.cpp",
        "Lshal.cpp",
        "ListCommand.cpp",
        "PipeRelay.cpp",
+3 −1
Original line number Diff line number Diff line
@@ -31,7 +31,9 @@ public:
    virtual ~Command() = default;
    // Expect optind to be set by Lshal::main and points to the next argument
    // to process.
    virtual Status main(const std::string &command, const Arg &arg) = 0;
    virtual Status main(const Arg &arg) = 0;

    virtual void usage() const = 0;

protected:
    Lshal& mLshal;
+16 −4
Original line number Diff line number Diff line
@@ -21,9 +21,8 @@
namespace android {
namespace lshal {

Status DebugCommand::parseArgs(const std::string &command, const Arg &arg) {
Status DebugCommand::parseArgs(const Arg &arg) {
    if (optind >= arg.argc) {
        mLshal.usage(command);
        return USAGE;
    }
    mInterfaceName = arg.argv[optind];
@@ -34,8 +33,8 @@ Status DebugCommand::parseArgs(const std::string &command, const Arg &arg) {
    return OK;
}

Status DebugCommand::main(const std::string &command, const Arg &arg) {
    Status status = parseArgs(command, arg);
Status DebugCommand::main(const Arg &arg) {
    Status status = parseArgs(arg);
    if (status != OK) {
        return status;
    }
@@ -46,6 +45,19 @@ Status DebugCommand::main(const std::string &command, const Arg &arg) {
            mLshal.err());
}

void DebugCommand::usage() const {

    static const std::string debug =
            "debug:\n"
            "    lshal debug <interface> [options [options [...]]] \n"
            "        Print debug information of a specified interface.\n"
            "        <inteface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n"
            "            If instance name is missing `default` is used.\n"
            "        options: space separated options to IBase::debug.\n";

    mLshal.err() << debug;
}

}  // namespace lshal
}  // namespace android
+3 −2
Original line number Diff line number Diff line
@@ -33,9 +33,10 @@ class DebugCommand : public Command {
public:
    DebugCommand(Lshal &lshal) : Command(lshal) {}
    ~DebugCommand() = default;
    Status main(const std::string &command, const Arg &arg) override;
    Status main(const Arg &arg) override;
    void usage() const override;
private:
    Status parseArgs(const std::string &command, const Arg &arg);
    Status parseArgs(const Arg &arg);

    std::string mInterfaceName;
    std::vector<std::string> mOptions;
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "HelpCommand.h"

#include "Lshal.h"

namespace android {
namespace lshal {

Status HelpCommand::main(const Arg &arg) {
    if (optind >= arg.argc) {
        // `lshal help` prints global usage.
        mLshal.usage();
        return OK;
    }
    (void)usageOfCommand(arg.argv[optind]);
    return OK;
}

Status HelpCommand::usageOfCommand(const std::string& c) const {
    if (c.empty()) {
        mLshal.usage();
        return USAGE;
    }
    auto command = mLshal.selectCommand(c);
    if (command == nullptr) {
        // from HelpCommand::main, `lshal help unknown`
        mLshal.usage();
        return USAGE;
    }

    command->usage();
    return USAGE;

}

void HelpCommand::usage() const {
    static const std::string help =
            "help:\n"
            "    lshal -h\n"
            "    lshal --help\n"
            "    lshal help\n"
            "        Print this help message\n"
            "    lshal help list\n"
            "        Print help message for list\n"
            "    lshal help debug\n"
            "        Print help message for debug\n";

    mLshal.err() << help;
}

}  // namespace lshal
}  // namespace android
Loading