/** * Validate command line <code>args</code>. * <p>The given command line <code>args</code> are validated against the * custom <code>CliCommand</code>'s added by the application. Help is * displayed and the application exits if there are errors. Otherwise, the * specified <code>activeCommand<code> is set, making its commons-cli * <code>CommandLine</code> available for the application. * @return the selected <code>CliCommand</code> */ protected CliCommand validateCommands() { // check for no arguments help if (originalArgs.length == 0) { if (nonCommandCli.get()) commands.get(null).help(0, null); commandHelp(0, null); } // check for simple no commands command line if (nonCommandCli.get()) { CliCommand command = commands.get(null); command.parseCommandLine(originalArgs); boolean gotConfig = command.handleConfiguration(); handleLogger(command.getCommandLine(), gotConfig); command.customInit(); activeCommand = command; return command; } // check for -v --version at first argument if (originalArgs[0].equals("-" + VersionShortCl) || originalArgs[0].equals("--" + VersionLongCl)) version(); if (originalArgs[0].equals("-" + CliBase.HelpShortCl) || originalArgs[0].equals("--" + CliBase.HelpLongCl)) commandHelp(0, null); // check if first command given is known CliCommand cmd = null; for (Entry<String, CliCommand> entry : commands.entrySet()) { if (entry.getKey().equals(originalArgs[0])) { cmd = entry.getValue(); break; } } if (cmd == null) commandHelp(InvalidCommand, "Unknown command: " + originalArgs[0]); // check for first command given but no arguments help if (originalArgs.length == 1) { if (cmd.hasChildren()) cmd.commandHelp(0, null); if (cmd.hasOptions()) cmd.help(0, null); return cmd; } // must be more than 1 argument given, see if 2nd is another level command CliCommand command = null; for (Entry<String, CliCommand> entry : commands.entrySet()) { command = entry.getValue(); activeCommand = command.validateCommands(0); if (activeCommand != null) break; } boolean gotConfig = command.handleConfiguration(); handleLogger(command.getCommandLine(), gotConfig); if (log.isDebugEnabled()) { StrH.ttl(initsb, 1, "java.class.path:"); String path = System.getProperty("java.class.path"); do { int index = path.indexOf(';'); if (index == -1) break; String element = path.substring(0, index); StrH.ttl(initsb, 2, element); path = path.substring(++index); } while (true); path = path.replace(';', '\n'); } log.debug(initsb.toString()); return activeCommand; }
/** * Return Command Line. * * @return the <code>CommandLine</code>from the active <code>CliCommand</code> specified in the * given command line arguments. Will never be null. * @see CliCommand */ public CommandLine getCommandLine() { return activeCommand.getCommandLine(); }