private void printParser(ParserSpec parser, PrintWriter pw, String indent) { pw.printf("%sParserSpec:%n", indent); indent += " "; pw.printf("%sseparator: '%s'%n", indent, parser.separator()); pw.printf("%sendOfOptionsDelimiter: '%s'%n", indent, parser.endOfOptionsDelimiter()); pw.printf("%sexpandAtFiles: %s%n", indent, parser.expandAtFiles()); pw.printf("%satFileCommentChar: '%s'%n", indent, parser.atFileCommentChar()); pw.printf("%soverwrittenOptionsAllowed: %s%n", indent, parser.overwrittenOptionsAllowed()); pw.printf("%sunmatchedArgumentsAllowed: %s%n", indent, parser.unmatchedArgumentsAllowed()); pw.printf("%sunmatchedOptionsArePositionalParams: %s%n", indent, parser.unmatchedOptionsArePositionalParams()); pw.printf("%sstopAtUnmatched: %s%n", indent, parser.stopAtUnmatched()); pw.printf("%sstopAtPositional: %s%n", indent, parser.stopAtPositional()); pw.printf("%sposixClusteredShortOptionsAllowed: %s%n", indent, parser.posixClusteredShortOptionsAllowed()); pw.printf("%saritySatisfiedByAttachedOptionParam: %s%n", indent, parser.aritySatisfiedByAttachedOptionParam()); pw.printf("%scaseInsensitiveEnumValuesAllowed: %s%n", indent, parser.caseInsensitiveEnumValuesAllowed()); pw.printf("%scollectErrors: %s%n", indent, parser.collectErrors()); pw.printf("%slimitSplit: %s%n", indent, parser.limitSplit()); pw.printf("%stoggleBooleanFlags: %s%n", indent, parser.toggleBooleanFlags()); }
boolean resemblesOption(String arg, Tracer tracer) { if (parser().unmatchedOptionsArePositionalParams()) { if (tracer != null && tracer.isDebug()) {tracer.debug("Parser is configured to treat all unmatched options as positional parameter%n", arg);} return false; } if (arg.length() == 1) { if (tracer != null && tracer.isDebug()) {tracer.debug("Single-character arguments that don't match known options are considered positional parameters%n", arg);} return false; } if (options().isEmpty()) { boolean result = arg.startsWith("-"); if (tracer != null && tracer.isDebug()) {tracer.debug("'%s' %s an option%n", arg, (result ? "resembles" : "doesn't resemble"));} return result; } int count = 0; for (String optionName : optionsMap().keySet()) { for (int i = 0; i < arg.length(); i++) { if (optionName.length() > i && arg.charAt(i) == optionName.charAt(i)) { count++; } else { break; } } } boolean result = count > 0 && count * 10 >= optionsMap().size() * 9; // at least one prefix char in common with 9 out of 10 options if (tracer != null && tracer.isDebug()) {tracer.debug("'%s' %s an option: %d matching prefix chars out of %d option names%n", arg, (result ? "resembles" : "doesn't resemble"), count, optionsMap().size());} return result; } }
/** Sets whether arguments on the command line that resemble an option should be treated as positional parameters. * The default is {@code false}. * <p>The specified setting will be registered with this {@code CommandLine} and the full hierarchy of its * subcommands and nested sub-subcommands <em>at the moment this method is called</em>. Subcommands added * later will have the default setting. To ensure a setting is applied to all * subcommands, call the setter last, after adding subcommands.</p> * @param newValue the new setting. When {@code true}, arguments on the command line that resemble an option should be treated as positional parameters. * @return this {@code CommandLine} object, to allow method chaining * @since 3.0 * @see #getUnmatchedArguments() * @see #isUnmatchedArgumentsAllowed */ public CommandLine setUnmatchedOptionsArePositionalParams(boolean newValue) { getCommandSpec().parser().unmatchedOptionsArePositionalParams(newValue); for (CommandLine command : getCommandSpec().subcommands().values()) { command.setUnmatchedOptionsArePositionalParams(newValue); } return this; }
boolean resemblesOption(String arg, Tracer tracer) { if (parser().unmatchedOptionsArePositionalParams()) { if (tracer != null && tracer.isDebug()) {tracer.debug("Parser is configured to treat all unmatched options as positional parameter%n", arg);} return false; } if (options().isEmpty()) { boolean result = arg.startsWith("-"); if (tracer != null && tracer.isDebug()) {tracer.debug("%s %s an option%n", arg, (result ? "resembles" : "doesn't resemble"));} return result; } int count = 0; for (String optionName : optionsMap().keySet()) { for (int i = 0; i < arg.length(); i++) { if (optionName.length() > i && arg.charAt(i) == optionName.charAt(i)) { count++; } else { break; } } } boolean result = count > 0 && count * 10 >= optionsMap().size() * 9; // at least one prefix char in common with 9 out of 10 options if (tracer != null && tracer.isDebug()) {tracer.debug("%s %s an option: %d matching prefix chars out of %d option names%n", arg, (result ? "resembles" : "doesn't resemble"), count, optionsMap().size());} return result; } }
/** Sets whether arguments on the command line that resemble an option should be treated as positional parameters. * The default is {@code false}. * <p>The specified setting will be registered with this {@code CommandLine} and the full hierarchy of its * subcommands and nested sub-subcommands <em>at the moment this method is called</em>. Subcommands added * later will have the default setting. To ensure a setting is applied to all * subcommands, call the setter last, after adding subcommands.</p> * @param newValue the new setting. When {@code true}, arguments on the command line that resemble an option should be treated as positional parameters. * @return this {@code CommandLine} object, to allow method chaining * @since 3.0 * @see #getUnmatchedArguments() * @see #isUnmatchedArgumentsAllowed */ public CommandLine setUnmatchedOptionsArePositionalParams(boolean newValue) { getCommandSpec().parser().unmatchedOptionsArePositionalParams(newValue); for (CommandLine command : getCommandSpec().subcommands().values()) { command.setUnmatchedOptionsArePositionalParams(newValue); } return this; }
@Test public void testResemblesOption_WithOptionsNonDash() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.addOption(OptionSpec.builder("/x").build()); spec.parser().unmatchedOptionsArePositionalParams(false); assertFalse(spec.resemblesOption("blah", null)); System.setProperty("picocli.trace", "DEBUG"); Tracer tracer = new Tracer(); System.clearProperty("picocli.trace"); assertFalse(spec.resemblesOption("blah", tracer)); assertFalse(spec.resemblesOption("-a", tracer)); assertTrue(spec.resemblesOption("/a", tracer)); Tracer tracer2 = new Tracer(); assertFalse(spec.resemblesOption("blah", tracer2)); assertFalse(spec.resemblesOption("-a", tracer)); assertTrue(spec.resemblesOption("/a", tracer)); }
@Test public void testResemblesOption_WithOptionsDash() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.addOption(OptionSpec.builder("-x").build()); spec.parser().unmatchedOptionsArePositionalParams(false); assertFalse(spec.resemblesOption("blah", null)); System.setProperty("picocli.trace", "DEBUG"); Tracer tracer = new Tracer(); System.clearProperty("picocli.trace"); assertFalse(spec.resemblesOption("blah", tracer)); assertTrue(spec.resemblesOption("-a", tracer)); assertFalse(spec.resemblesOption("/a", tracer)); Tracer tracer2 = new Tracer(); assertFalse(spec.resemblesOption("blah", tracer2)); assertTrue(spec.resemblesOption("-a", tracer)); assertFalse(spec.resemblesOption("/a", tracer)); }
@Test public void testResemblesOption_WithoutOptions() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.parser().unmatchedOptionsArePositionalParams(false); assertFalse(spec.resemblesOption("blah", null)); System.setProperty("picocli.trace", "DEBUG"); Tracer tracer = new Tracer(); System.clearProperty("picocli.trace"); assertFalse(spec.resemblesOption("blah", tracer)); assertTrue(spec.resemblesOption("-a", tracer)); Tracer tracer2 = new Tracer(); assertFalse(spec.resemblesOption("blah", tracer2)); assertTrue(spec.resemblesOption("-a", tracer)); }
@Test public void testResemblesOption_WhenUnmatchedArePositional() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.parser().unmatchedOptionsArePositionalParams(true); assertFalse(spec.resemblesOption("blah", null)); System.setProperty("picocli.trace", "DEBUG"); Tracer tracer = new Tracer(); System.clearProperty("picocli.trace"); assertFalse(spec.resemblesOption("blah", tracer)); Tracer tracer2 = new Tracer(); assertFalse(spec.resemblesOption("blah", tracer2)); }
/** Returns whether arguments on the command line that resemble an option should be treated as positional parameters. * The default is {@code false} and the parser behaviour depends on {@link #isUnmatchedArgumentsAllowed()}. * @return {@code true} arguments on the command line that resemble an option should be treated as positional parameters, {@code false} otherwise * @see #getUnmatchedArguments() * @since 3.0 */ public boolean isUnmatchedOptionsArePositionalParams() { return getCommandSpec().parser().unmatchedOptionsArePositionalParams(); }
@Test public void testResemblesOption_WithOptionsDash() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.addOption(OptionSpec.builder("-x").build()); spec.parser().unmatchedOptionsArePositionalParams(false); assertFalse(spec.resemblesOption("blah", null)); System.setProperty("picocli.trace", "DEBUG"); Tracer tracer = new Tracer(); System.clearProperty("picocli.trace"); assertFalse(spec.resemblesOption("blah", tracer)); assertTrue(spec.resemblesOption("-a", tracer)); assertFalse(spec.resemblesOption("/a", tracer)); Tracer tracer2 = new Tracer(); assertFalse(spec.resemblesOption("blah", tracer2)); assertTrue(spec.resemblesOption("-a", tracer)); assertFalse(spec.resemblesOption("/a", tracer)); }
@Test public void testResemblesOption_WithOptionsNonDash() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.addOption(OptionSpec.builder("/x").build()); spec.parser().unmatchedOptionsArePositionalParams(false); assertFalse(spec.resemblesOption("blah", null)); System.setProperty("picocli.trace", "DEBUG"); Tracer tracer = new Tracer(); System.clearProperty("picocli.trace"); assertFalse(spec.resemblesOption("blah", tracer)); assertFalse(spec.resemblesOption("-a", tracer)); assertTrue(spec.resemblesOption("/a", tracer)); Tracer tracer2 = new Tracer(); assertFalse(spec.resemblesOption("blah", tracer2)); assertFalse(spec.resemblesOption("-a", tracer)); assertTrue(spec.resemblesOption("/a", tracer)); }
@Test public void testResemblesOption_WithoutOptions() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.parser().unmatchedOptionsArePositionalParams(false); assertFalse(spec.resemblesOption("blah", null)); System.setProperty("picocli.trace", "DEBUG"); Tracer tracer = new Tracer(); System.clearProperty("picocli.trace"); assertFalse(spec.resemblesOption("blah", tracer)); assertTrue(spec.resemblesOption("-a", tracer)); Tracer tracer2 = new Tracer(); assertFalse(spec.resemblesOption("blah", tracer2)); assertTrue(spec.resemblesOption("-a", tracer)); }
/** Returns whether arguments on the command line that resemble an option should be treated as positional parameters. * The default is {@code false} and the parser behaviour depends on {@link #isUnmatchedArgumentsAllowed()}. * @return {@code true} arguments on the command line that resemble an option should be treated as positional parameters, {@code false} otherwise * @see #getUnmatchedArguments() * @since 3.0 */ public boolean isUnmatchedOptionsArePositionalParams() { return getCommandSpec().parser().unmatchedOptionsArePositionalParams(); }
@Test public void testResemblesOption_WhenUnmatchedArePositional() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.parser().unmatchedOptionsArePositionalParams(true); assertFalse(spec.resemblesOption("blah", null)); System.setProperty("picocli.trace", "DEBUG"); Tracer tracer = new Tracer(); System.clearProperty("picocli.trace"); assertFalse(spec.resemblesOption("blah", tracer)); Tracer tracer2 = new Tracer(); assertFalse(spec.resemblesOption("blah", tracer2)); }