Example usage for org.apache.commons.cli ParseException ParseException

List of usage examples for org.apache.commons.cli ParseException ParseException

Introduction

In this page you can find the example usage for org.apache.commons.cli ParseException ParseException.

Prototype

public ParseException(String message) 

Source Link

Document

Construct a new ParseException with the specified detail message.

Usage

From source file:org.jboss.jbossset.CommandLineParser.java

private int getValidIssueLimit(String issueLimit) throws ParseException {
    try {/*  w w w.j av  a2  s. c o m*/
        int limit = new Integer(issueLimit);
        if (limit > 0)
            return limit;
    } catch (NumberFormatException e) {
    }
    throw new ParseException("Invalid issueLimit value: " + issueLimit + ". A positive integer is expected");
}

From source file:org.jclouds.demo.tweetstore.integration.StaxSdkAppServer2.java

public static StaxSdkAppServer2 createServer(String[] args, String[] classPaths, ClassLoader cl)
        throws ParseException, ServletException {
    StaxSdkAppServerCLI cli = StaxSdkAppServerCLI.parse(args);
    if (cli.getMissingOptions().length > 0) {
        throw new ParseException("Missing required options: " + cli.formatMissingOptions(", "));
    }/*w  ww  .ja  va 2 s .c  om*/

    String[] environments = cli.getEnvironment();
    File serverConfig = cli.getServerConfigFile();
    File baseDir = new File(cli.getBaseDir());
    File webRoot = new File(cli.getWebdir());
    File workDir = new File(baseDir, "work");

    File staxWebXml = new File(webRoot, "WEB-INF/cloudbees-web.xml");
    if (!(staxWebXml.exists()))
        staxWebXml = new File(webRoot, "WEB-INF/stax-web.xml");
    IAppServerConfiguration config = WarBasedServerConfiguration.load(serverConfig, webRoot, staxWebXml,
            environments);
    // force the RequestMonitorValve to sleep for only a short period
    set("statusInterval", StaxReflect.getAppServerConfig(config), 5);
    StaxSdkAppServer server = new StaxSdkAppServer(baseDir.getAbsolutePath(), workDir.getAbsolutePath(), cl,
            classPaths, cli.getPort(), config, cli.getRepositoryPath());
    return new StaxSdkAppServer2(server);
}

From source file:org.jcryptool.commands.core.api.AbstractCommand.java

protected InputStream handleInputOption(CommandLine commandLine) throws FileNotFoundException, ParseException {
    InputStream inputStream = null;
    if (commandLine.hasOption(TEXTINPUT_OPTIONNAME)) {
        String input = commandLine.getOptionValue(TEXTINPUT_OPTIONNAME);
        inputStream = new BufferedInputStream(new ByteArrayInputStream(input.getBytes()));
        try {//from  w  ww  .  j  a va 2  s. com
            if (inputStream.available() < 1)
                throw new ParseException(Messages.AbstractCommand_zerotextMsg);
        } catch (IOException e) {
            throw new ParseException(Messages.AbstractCommand_manualInputException);
        }
    } else if (commandLine.hasOption(EDITORINPUT_OPTIONNAME)) {
        inputStream = EditorsManager.getInstance().getActiveEditorContentInputStream();

        if (inputStream == null)
            throw new ParseException(Messages.AbstractCommand_noEditorMsg);
    } else if (commandLine.hasOption(FILEINPUT_OPTIONNAME)) {
        String fileName = commandLine.getOptionValue(FILEINPUT_OPTIONNAME);
        inputStream = new BufferedInputStream(new FileInputStream(fileName));
    }

    return inputStream;
}

From source file:org.jcryptool.commands.core.evaluator.CommandEvaluator.java

/**
 * Sprits command Arg String into its parts. Needs to parse text cause of " " text grouping. \" escapes " for text
 * usage./*from  w  w w  . j  av a2 s  .co  m*/
 *
 * @param commandArgs
 * @return
 * @throws ParseException
 */
private String[] splitArgs(String commandArgs) throws ParseException {
    ArrayList<String> args = new ArrayList<String>();
    StringBuilder arg = new StringBuilder();
    boolean stringMode = false;
    boolean escapeMode = false;
    for (int i = 0; i < commandArgs.length(); i++) {
        char actualChar = commandArgs.charAt(i);
        if (Character.isSpaceChar(actualChar)) {
            escapeMode = false;
            if (stringMode) {
                arg.append(actualChar);
            } else {
                args.add(arg.toString());
                arg.delete(0, arg.length());
            }
        } else if (actualChar == '"') {
            if (escapeMode) {
                arg.deleteCharAt(arg.length() - 1);
                arg.append(actualChar);
                escapeMode = false;
            } else {
                stringMode = !stringMode;
            }
        } else if (actualChar == '\\') {
            arg.append(actualChar);
            escapeMode = true;
        } else {
            escapeMode = false;
            arg.append(actualChar);
        }
    }
    if (arg.length() > 0) {
        args.add(arg.toString());
    }

    if (stringMode) {
        throw new ParseException(
                "Illegal use of \". It needs to be escaped with \\\" or used as a pair that encloses text like \"hello world\""); //$NON-NLS-1$
    }

    return args.toArray(new String[args.size()]);
}

From source file:org.jcryptool.commands.core.HelpCommand.java

public void execute(CommandLine commandLine) {
    StringBuilder result = new StringBuilder();
    try {//www  .  ja v a 2  s .com
        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();
}

From source file:org.jcryptool.commands.core.HELP_Command.java

public void execute(CommandLine commandLine) {
    StringBuilder result = new StringBuilder();
    try {/*from www . jav a  2s .c o m*/
        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();
}

From source file:org.jcryptool.crypto.classic.adfgvx.algorithm.AdfgvxCmd.java

@Override
protected char[] handleKeyOption(CommandLine commandLine, StringBuilder parseResultOut) throws ParseException {

    //returns null -- but writes these two global variables.
    substitutionKeyForDataobject = null;
    transpositionKeyForDataobject = null;

    if (commandLine.hasOption("kS")) { //$NON-NLS-1$
        String key = commandLine.getOptionValue("kS"); //$NON-NLS-1$
        //verificate the key

        List<KeyVerificator> verificators = AdfgvxAlgorithm.specification.getKeyVerificatorsSubstitutionKey();
        verifyKey(key, verificators);/*w  w  w .  ja v  a  2s. c  o m*/

        this.substitutionKeyForDataobject = AdfgvxAlgorithm.specification
                .substitutionKeyInputStringToDataobjectFormat(key);
    } else {
        throw new ParseException(Messages.AdfgvxCmd_specifySubstKey);
    }

    if (commandLine.hasOption("kT")) { //$NON-NLS-1$
        String key = commandLine.getOptionValue("kT"); //$NON-NLS-1$
        //verificate the key

        List<KeyVerificator> verificators = AdfgvxAlgorithm.specification.getKeyVerificatorsTranspositionKey();
        verifyKey(key, verificators);

        this.transpositionKeyForDataobject = AdfgvxAlgorithm.specification
                .transpositionKeyInputStringToDataobjectFormat(key);
    } else {
        throw new ParseException(Messages.AdfgvxCmd_specifyTranspKey);
    }

    return null;
}

From source file:org.jcryptool.crypto.classic.doppelkasten.algorithm.DoppelkastenCmd.java

@Override
protected char[] handleKeyOption(CommandLine commandLine, StringBuilder parseResultOut) throws ParseException {
    String key1 = null;//www  .  j  a v  a 2 s  . c  o  m
    String key2 = null;
    if (commandLine.hasOption("k")) { //$NON-NLS-1$
        String key = commandLine.getOptionValue("k"); //$NON-NLS-1$
        //verificate the key

        List<KeyVerificator> verificators = getSpecification().getKeyVerificators();
        verifyKey(key, verificators);

        key1 = key;

    } else {
        throw new ParseException(
                org.jcryptool.crypto.classic.doppelkasten.algorithm.Messages.DoppelkastenCmd_specifyKeys);
    }

    if (commandLine.hasOption("k2")) { //$NON-NLS-1$
        String key = commandLine.getOptionValue("k2"); //$NON-NLS-1$
        //verificate the key

        List<KeyVerificator> verificators = getSpecification().getKeyVerificators();
        verifyKey(key, verificators);

        key2 = key;

    } else {
        throw new ParseException(
                org.jcryptool.crypto.classic.doppelkasten.algorithm.Messages.DoppelkastenCmd_specifyKeys);
    }

    return DoppelkastenAlgorithm.specification.keyInputStringToDataobjectFormat(key1, key2);
}

From source file:org.jcryptool.crypto.classic.model.algorithm.ClassicAlgorithmCmd.java

/**
 * Verifies a String key against a set of verificators, throwing a parse exception
 * with the reason from the verification result as massage.
 * /*from ww  w .j  a  v a 2 s. co  m*/
 * @param key the key
 * @param verificators the collection of verificators
 * @return
 * @throws ParseException when the verification as not successful.
 */
protected InputVerificationResult verifyKey(String key, Collection<KeyVerificator> verificators)
        throws ParseException {
    InputVerificationResult verificationResult = KeyVerificator.verify(key, currentAlphabet, verificators);

    if (!verificationResult.isValid()) {
        if (verificationResult.isStandaloneMessage()) {
            throw new ParseException(verificationResult.getMessage());
        } else {
            String mask = Messages.ClassicAlgorithmCmd_notwellformedMsg;
            throw new ParseException(String.format(mask, verificationResult.getMessage()));
        }
    }

    return verificationResult;
}

From source file:org.nuxeo.launcher.NuxeoLauncher.java

/**
 * @since 5.6//from  w w  w. j a va 2  s . co m
 */
protected static CommandLine parseOptions(String[] args) throws ParseException {
    initParserOptions();
    CommandLineParser parser = new PosixParser();
    CommandLine cmdLine = null;
    Boolean stopAfterParsing = true;
    try {
        cmdLine = parser.parse(launcherOptions, args);
        if (cmdLine.hasOption(OPTION_HELP) || cmdLine.getArgList().contains(OPTION_HELP)) {
            printLongHelp();
        } else if (cmdLine.getArgList().isEmpty()) {
            printShortHelp();
        } else {
            stopAfterParsing = false;
        }
    } catch (UnrecognizedOptionException e) {
        log.error(e.getMessage());
        printShortHelp();
    } catch (MissingArgumentException e) {
        log.error(e.getMessage());
        printShortHelp();
    } catch (ParseException e) {
        log.error("Error while parsing command line: " + e.getMessage());
        printShortHelp();
    } finally {
        if (stopAfterParsing) {
            throw new ParseException("Invalid command line");
        }
    }
    return cmdLine;
}