All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.olivergondza.dumpling.cli.HelpCommand Maven / Gradle / Ivy

Go to download

Dumpling is an object model and domain specific language to represent and query the state of process threads and their lock based dependencies

There is a newer version: 1.0
Show newest version
/*
 * The MIT License
 *
 * Copyright (c) 2014 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.github.olivergondza.dumpling.cli;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.annotation.Nonnull;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

public class HelpCommand implements CliCommand {

    @Argument(required = false, usage = "Print detailed usage", metaVar = "COMMAND")
    private String commandName;

    @Override
    public String getName() {
        return "help";
    }

    @Override
    public String getDescription() {
        return "Print dumpling usage";
    }

    @Override
    public int run(@Nonnull ProcessStream process) throws CmdLineException {
        if (commandName == null) {
            printUsage(process.out());
        } else {
            CliCommand command = CliCommandOptionHandler.getHandler(commandName);
            if (command == null) throw new CmdLineException(null, "No such command " + commandName);

            printUsage(command, process.out());
        }

        return 0;
    }

    /*package*/ static void printUsage(CliCommand handler, PrintStream out) {
        CmdLineParser parser = new CmdLineParser(handler);
        out.print(usage(handler.getName()));
        parser.printSingleLineUsage(out);
        out.println("\n");
        out.println(handler.getDescription());
        parser.printUsage(out);
    }

    /*package*/ static void printUsage(PrintStream out) {
        out.println(usage(" [...]\n"));
        out.println("Available commands:");
        for (CliCommand handler: sortedHandlers()) {
            CmdLineParser parser = new CmdLineParser(handler);
            out.print(handler.getName());
            parser.printSingleLineUsage(out);
            out.print("\n\t");
            out.println(handler.getDescription());
        }
    }

    private static SortedSet sortedHandlers() {
        TreeSet sorted = new TreeSet(new Comparator() {
            @Override
            public int compare(CliCommand lhs, CliCommand rhs) {
                return lhs.getName().compareTo(rhs.getName());
            }
        });
        sorted.addAll(CliCommandOptionHandler.getAllHandlers());
        return sorted;
    }

    private static String usage(String rest) {
        return "Usage: java -jar dumpling.jar " + rest;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy