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()); }
else if (config().posixClusteredShortOptionsAllowed() && arg.length() > 2 && arg.startsWith("-")) { if (tracer.isDebug()) {tracer.debug("Trying to process '%s' as clustered short options%n", arg, args);} processClusteredShortOptions(required, initialized, arg, args);
/** Sets whether short options like {@code -x -v -f SomeFile} can be clustered together like {@code -xvfSomeFile}. The default is {@code true}. * <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 * @return this {@code CommandLine} object, to allow method chaining * @since 3.0 */ public CommandLine setPosixClusteredShortOptionsAllowed(boolean newValue) { getCommandSpec().parser().posixClusteredShortOptionsAllowed(newValue); for (CommandLine command : getCommandSpec().subcommands().values()) { command.setPosixClusteredShortOptionsAllowed(newValue); } return this; }
else if (config().posixClusteredShortOptionsAllowed() && arg.length() > 2 && arg.startsWith("-")) { if (tracer.isDebug()) {tracer.debug("Trying to process '%s' as clustered short options%n", arg, args);} processClusteredShortOptions(required, initialized, arg, args);
@Test public void testParserPosixClustedShortOptions_false_disallowsShortOptionsAttachedToOptionParam() { String[] args = {"-oFILE"}; CompactFields compact = CommandLine.populateCommand(new CompactFields(), args); verifyCompact(compact, false, false, "FILE", null); CompactFields unclustered = new CompactFields(); CommandLine cmd = new CommandLine(unclustered); cmd.getCommandSpec().parser().posixClusteredShortOptionsAllowed(false); try { cmd.parse(args); fail("Expected exception"); } catch (UnmatchedArgumentException ex) { assertEquals("Unknown option: -oFILE", ex.getMessage()); } } @Test
/** Sets whether short options like {@code -x -v -f SomeFile} can be clustered together like {@code -xvfSomeFile}. The default is {@code true}. * <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 * @return this {@code CommandLine} object, to allow method chaining * @since 3.0 */ public CommandLine setPosixClusteredShortOptionsAllowed(boolean newValue) { getCommandSpec().parser().posixClusteredShortOptionsAllowed(newValue); for (CommandLine command : getCommandSpec().subcommands().values()) { command.setPosixClusteredShortOptionsAllowed(newValue); } return this; }
@Test public void testParserPosixClustedShortOptions_false_resultsInShortClusteredOptionsNotRecognized() { CompactFields compact = CommandLine.populateCommand(new CompactFields(), "-rvoFILE"); verifyCompact(compact, true, true, "FILE", null); CommandLine cmd = new CommandLine(new CompactFields()); cmd.getCommandSpec().parser().posixClusteredShortOptionsAllowed(false); try { cmd.parse("-rvoFILE"); fail("Expected exception"); } catch (UnmatchedArgumentException ex) { assertEquals("Unknown option: -rvoFILE", ex.getMessage()); } } @Test
/** Returns whether the parser accepts clustered short options. The default is {@code true}. * @return {@code true} if short options like {@code -x -v -f SomeFile} can be clustered together like {@code -xvfSomeFile}, {@code false} otherwise * @since 3.0 */ public boolean isPosixClusteredShortOptionsAllowed() { return getCommandSpec().parser().posixClusteredShortOptionsAllowed(); }
@Test public void testParserPosixClustedShortOptions_false_allowsUnclusteredShortOptions() { String[] args = "-r -v -o FILE".split(" "); CompactFields compact = CommandLine.populateCommand(new CompactFields(), args); verifyCompact(compact, true, true, "FILE", null); CompactFields unclustered = new CompactFields(); CommandLine cmd = new CommandLine(unclustered); cmd.getCommandSpec().parser().posixClusteredShortOptionsAllowed(false); cmd.parse(args); verifyCompact(unclustered, true, true, "FILE", null); }
@Test public void testParserPosixClustedShortOptions_false_disallowsShortOptionsAttachedToOptionParam() { String[] args = {"-oFILE"}; CompactFields compact = CommandLine.populateCommand(new CompactFields(), args); verifyCompact(compact, false, false, "FILE", null); CompactFields unclustered = new CompactFields(); CommandLine cmd = new CommandLine(unclustered); cmd.getCommandSpec().parser().posixClusteredShortOptionsAllowed(false); try { cmd.parse(args); fail("Expected exception"); } catch (UnmatchedArgumentException ex) { assertEquals("Unknown option: -oFILE", ex.getMessage()); } } @Test
@Test public void testParserPosixClustedShortOptions_false_resultsInShortClusteredOptionsNotRecognized() { CompactFields compact = CommandLine.populateCommand(new CompactFields(), "-rvoFILE"); verifyCompact(compact, true, true, "FILE", null); CommandLine cmd = new CommandLine(new CompactFields()); cmd.getCommandSpec().parser().posixClusteredShortOptionsAllowed(false); try { cmd.parse("-rvoFILE"); fail("Expected exception"); } catch (UnmatchedArgumentException ex) { assertEquals("Unknown option: -rvoFILE", ex.getMessage()); } } @Test
@Test public void testParserPosixClustedShortOptions_false_allowsUnclusteredShortOptions() { String[] args = "-r -v -o FILE".split(" "); CompactFields compact = CommandLine.populateCommand(new CompactFields(), args); verifyCompact(compact, true, true, "FILE", null); CompactFields unclustered = new CompactFields(); CommandLine cmd = new CommandLine(unclustered); cmd.getCommandSpec().parser().posixClusteredShortOptionsAllowed(false); cmd.parse(args); verifyCompact(unclustered, true, true, "FILE", null); }
/** Returns whether the parser accepts clustered short options. The default is {@code true}. * @return {@code true} if short options like {@code -x -v -f SomeFile} can be clustered together like {@code -xvfSomeFile}, {@code false} otherwise * @since 3.0 */ public boolean isPosixClusteredShortOptionsAllowed() { return getCommandSpec().parser().posixClusteredShortOptionsAllowed(); }