private void printUsage(PrintWriter writer) { // fix defaults for options like help and other 0-arg booleans OptionHandlerRegistry.getRegistry().registerHandler(Boolean.class, BooleanNoDefOptionHandler.class); OptionHandlerRegistry.getRegistry().registerHandler(boolean.class, BooleanNoDefOptionHandler.class); ParserProperties prop = ParserProperties.defaults() .withUsageWidth(80) .withOptionSorter(null); ByteArrayOutputStream buf = new ByteArrayOutputStream(); // new args instance to get correct defaults new CmdLineParser(new CliArgs(), prop) .printUsage(new OutputStreamWriter(buf, StandardCharsets.UTF_8), null); writer.println(MessageFormat.format(Messages.UsageHelp.replace("${tab}", "\t"), new String(buf.toByteArray(), StandardCharsets.UTF_8), DangerStatementOptionHandler.getMetaVariable() + '\n' + DbObjTypeOptionHandler.getMetaVariable())); }
private static void registerHandlersIfNeeded() { if (registeredHandlers) { return; } synchronized (CmdLine.class) { if (registeredHandlers) { return; } OptionHandlerRegistry .getRegistry() .registerHandler(Duration.class, DurationOptionHandler.class); OptionHandlerRegistry .getRegistry() .registerHandler(Optional.class, OptionalOptionHandler.class); OptionHandlerRegistry .getRegistry() .registerHandler(OptionalLimit.class, OptionalLimitOptionHandler.class); registeredHandlers = true; } }
/** * Parses and checks the program arguments. If the arguments are invalid or the user requested * the help message, this method returns <code>false</code> and writes the message for the user. * If the parsing is successful, this method does not print anything to the output. * * @param args * the arguments to parse * @return <code>true</code> if the arguments were successfully parsed, <code>false</code> if * arguments are invalid or the user requested the help message (<code>false</code> * generally means that SETTE should stop) */ public boolean parse(@NonNull String... args) { // register handler for tool and set the current configuration for it OptionHandlerRegistry.getRegistry().registerHandler(SetteToolConfiguration.class, ToolOptionHandler.class); ToolOptionHandler.configuration = configuration; // parse args with preset properties ParserProperties parserProps = ParserProperties.defaults() .withShowDefaults(true) .withUsageWidth(80); CmdLineParser parser = new CmdLineParser(this, parserProps); try { parser.parseArgument(args); if (help) { printHelp(parser); return false; } else { return true; } } catch (CmdLineException ex) { errorOutput.println(ex.getMessage()); printHelp(parser); return false; } }