/** * Full CliBase constructor. * * <p>If <code>logToFileSwitch</code> is false but <code>configSwitch</code> is true, the * configuration file will be checked for <code>LogFilePathKey</code> to obtain logging * information. The value of this key is used in a common logback.xml configuration file that * supports a rolling file appender which can be used for all applications. * * @param args the given command line arguments. Must not be null. * @param configPathKey environment key to use for obtaining the configuration file. Default is * used if null. * @param configSwitch if true enable the configSwitch, otherwise disable it. * @param logToFileSwitch if true enable the logToFileSwitch, otherwise disable it. * @param maxHelpWidth the maximum help width for commons-cli to use. */ public CliBase( String[] args, String configPathKey, boolean configSwitch, boolean logToFileSwitch, int maxHelpWidth) { this.originalArgs = args; this.executableName = getClass().getSimpleName(); this.configPathKey = configPathKey == null ? ConfigPathKey : configPathKey; this.configSwitch = configSwitch; this.logToFile = logToFileSwitch; this.maxHelpWidth = maxHelpWidth; properties = new Properties(); commands = new HashMap<String, CliCommand>(); version = ClassInfo.getInfo(getClass()).getPackage().getImplementationVersion(); initsb = new StringBuilder("\n" + getClass().getSimpleName() + " init:\n"); StrH.ttl(initsb, 1, "executableName", " = ", executableName); StrH.ttl(initsb, 1, "version", " = ", version); StrH.ttl(initsb, 1, "configSwitch", " = ", configSwitch); StrH.ttl(initsb, 1, "logToFileSwitch", " = ", logToFileSwitch); }
private String handleLogger(CommandLine commandline, boolean gotConfig) { String name = null; String logPath = null; if (!logToFile) { if (gotConfig) { logPath = properties.getProperty(LogFilePathKey); StrH.ttl(initsb, 1, LogFilePathKey, " = ", logPath); if (logPath != null) { System.setProperty(LogFilePathKey, logPath); String base = logPath; int idx = logPath.lastIndexOf('.'); if (idx != -1) base = logPath.substring(0, idx); StrH.ttl(initsb, 1, LogFileBaseKey, " = ", base); System.setProperty(LogFileBaseKey, logPath); name = StrH.getAtomicNameFromPath(base); } } } else { logPath = commandline.getOptionValue(LoggerNameShortCl, DefaultLogPath); StrH.ttl(initsb, 1, "--", LoggerNameLongCl, " = ", logPath); System.setProperty(LogFilePathKey, logPath); String base = logPath; int idx = logPath.lastIndexOf('.'); if (idx != -1) base = logPath.substring(0, idx); StrH.ttl(initsb, 1, LogFileBaseKey, " = ", base); System.setProperty(LogFileBaseKey, logPath); name = StrH.getAtomicNameFromPath(base); } log = LoggerFactory.getLogger(getClass()); if (log.isDebugEnabled()) { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); // TODO: StatusPrinter.setPrintStream StatusPrinter.print(lc); try { ClassInfo classInfo = ClassInfo.getInfo(DOF.class); StrH.ttl(initsb, 1, "OAL JAR", " = ", classInfo.getLocation().toExternalForm()); } catch (Exception e) { StrH.ttl(initsb, 1, "OAL JAR", " = null"); } } return name; }
/** * 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; }