Search in sources :

Example 1 with Example

use of org.jcryptool.commands.core.ExtendedHelpCommand.Example in project core by jcryptool.

the class HelpCommand method execute.

public void execute(CommandLine commandLine) {
    StringBuilder result = new StringBuilder();
    try {
        if (commandLine.hasOption(EXAMPLES_SHORT_OPT)) {
            if (commandLine.hasOption(COMMANDLIST_SHORT_OPT)) {
                String mask = Messages.HelpCommand_chooseOnlyOneOptionHint;
                throw new ParseException(String.format(mask, EXAMPLES_SHORT_OPT, COMMANDLIST_SHORT_OPT));
            }
            if (commandLine.getArgs().length == 1) {
                String commandName = commandLine.getArgs()[0];
                Command command = getCommands().get(commandName);
                List<Example> examples = getExamples(commandName, command);
                if (examples.size() != 0) {
                    result.append(getExampleString(examples, commandName));
                } else {
                    throw new ParseException(Messages.HelpCommand_noExampleSupport);
                }
            } else {
                if (commandLine.getArgs().length > 1)
                    throw new ParseException(Messages.HelpCommand_tooManyArgs);
                if (commandLine.getArgs().length < 1)
                    throw new ParseException(Messages.HelpCommand_tooFewArgs);
            }
        } else if (commandLine.hasOption(COMMANDLIST_SHORT_OPT)) {
            if (commandLine.getArgs().length == 0) {
                for (Command command : CommandFactory.loadUniqueExtensions()) {
                    printCommand(result, command);
                }
            } else {
                throw new ParseException(Messages.HelpCommand_tooManyArgs);
            }
        } else if (commandLine.getArgs().length == 1) {
            String commandName = commandLine.getArgs()[0];
            Command command = getCommands().get(commandName);
            result.append(getShortHelpFor(commandName, command, reverseCommandline(this.getCommandName(), commandLine)));
        } else if (commandLine.getArgs().length == 0) {
            result.append(getGeneralHelptext());
        } else {
            throw new ParseException(Messages.HelpCommand_tooManyArgsSyntaxhelpRef);
        }
    } catch (ParseException e) {
        result.append(e.getMessage());
    }
    this.result = result.toString();
}
Also used : AbstractCommand(org.jcryptool.commands.core.api.AbstractCommand) Example(org.jcryptool.commands.core.ExtendedHelpCommand.Example) ParseException(org.apache.commons.cli.ParseException)

Example 2 with Example

use of org.jcryptool.commands.core.ExtendedHelpCommand.Example in project core by jcryptool.

the class HelpCommand method getExampleString.

/**
 * creates a String for output which contains the formatted examples.
 *
 * @param examples the examples
 * @param nameOfCommand the name of the command the examples are for (specify the name that was specified in the
 *        command line)
 */
static String getExampleString(Collection<Example> examples, String nameOfCommand) {
    int maxlength = 0;
    for (Example e : examples) maxlength = Math.max(maxlength, e.exampleCmdLine.length());
    StringBuilder builder = new StringBuilder();
    Iterator<Example> it = examples.iterator();
    if (it.hasNext()) {
        Example example = it.next();
        int lengthDiff = maxlength - example.exampleCmdLine.length();
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        builder.append("'" + makeConcreteExampleString(example.exampleCmdLine, nameOfCommand) + "'" + makeLeerString(lengthDiff) + " -> " + example.explanation);
    }
    while (it.hasNext()) {
        Example example = it.next();
        int lengthDiff = maxlength - example.exampleCmdLine.length();
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        builder.append("\n" + "'" + makeConcreteExampleString(example.exampleCmdLine, nameOfCommand) + "'" + makeLeerString(lengthDiff) + " -> " + example.explanation);
    }
    return builder.toString();
}
Also used : Example(org.jcryptool.commands.core.ExtendedHelpCommand.Example)

Example 3 with Example

use of org.jcryptool.commands.core.ExtendedHelpCommand.Example in project core by jcryptool.

the class HELP_Command method execute.

public void execute(CommandLine commandLine) {
    StringBuilder result = new StringBuilder();
    try {
        if (commandLine.hasOption(HelpCommand.EXAMPLES_SHORT_OPT)) {
            if (commandLine.hasOption(HelpCommand.COMMANDLIST_SHORT_OPT)) {
                String mask = Messages.HelpCommand_chooseOnlyOneOptionHint;
                throw new ParseException(String.format(mask, HelpCommand.EXAMPLES_SHORT_OPT, HelpCommand.COMMANDLIST_SHORT_OPT));
            }
            if (commandLine.getArgs().length == 1) {
                String commandName = commandLine.getArgs()[0];
                Command command = HelpCommand.getCommands().get(commandName);
                List<Example> examples = HelpCommand.getExamples(commandName, command);
                if (examples.size() != 0) {
                    result.append(HelpCommand.getExampleString(examples, commandName));
                } else {
                    throw new ParseException(Messages.HelpCommand_noExampleSupport);
                }
            } else {
                throw new ParseException(Messages.HelpCommand_tooManyArgs);
            }
        } else if (commandLine.hasOption(HelpCommand.COMMANDLIST_SHORT_OPT)) {
            if (commandLine.getArgs().length == 0) {
                for (Command command : CommandFactory.loadUniqueExtensions()) {
                    HelpCommand.printCommand(result, command);
                }
            } else {
                throw new ParseException(Messages.HelpCommand_tooManyArgs);
            }
        } else if (commandLine.getArgs().length == 1) {
            String commandName = commandLine.getArgs()[0];
            Command command = HelpCommand.getCommands().get(commandName);
            result.append(HelpCommand.getDetailedHelpFor(commandName, command, HelpCommand.reverseCommandline(this.getCommandName(), commandLine)));
        } else if (commandLine.getArgs().length == 0) {
            result.append(HelpCommand.getGeneralHelptext());
        } else {
            throw new ParseException(Messages.HelpCommand_tooManyArgsSyntaxhelpRef);
        }
    } catch (ParseException e) {
        result.append(e.getMessage());
    }
    this.result = result.toString();
}
Also used : AbstractCommand(org.jcryptool.commands.core.api.AbstractCommand) Example(org.jcryptool.commands.core.ExtendedHelpCommand.Example) ParseException(org.apache.commons.cli.ParseException)

Aggregations

Example (org.jcryptool.commands.core.ExtendedHelpCommand.Example)3 ParseException (org.apache.commons.cli.ParseException)2 AbstractCommand (org.jcryptool.commands.core.api.AbstractCommand)2