/** Returns the default value provider for the command, or {@code null} if none has been set. * @return the default value provider for this command, or {@code null} * @since 3.6 * @see Command#defaultValueProvider() * @see CommandSpec#defaultValueProvider() * @see ArgSpec#defaultValueString() */ public IDefaultValueProvider getDefaultValueProvider() { return getCommandSpec().defaultValueProvider(); }
/** Returns a map with the subcommands {@linkplain #addSubcommand(String, Object) registered} on this instance. * @return a map with the registered subcommands * @since 0.9.7 */ public Map<String, CommandLine> getSubcommands() { return new LinkedHashMap<String, CommandLine>(getCommandSpec().subcommands()); } /**
/** Returns the command name (also called program name) displayed in the usage help synopsis. * @return the command name (also called program name) displayed in the usage * @see CommandSpec#name() * @since 2.0 */ public String getCommandName() { return getCommandSpec().name(); }
/** Sets the CommandLine constructed with this {@code CommandSpec} model. */ protected CommandSpec commandLine(CommandLine commandLine) { this.commandLine = commandLine; for (CommandSpec mixedInSpec : mixins.values()) { mixedInSpec.commandLine(commandLine); } for (CommandLine sub : commands.values()) { sub.getCommandSpec().parent(this); } return this; }
/** * Returns the command that this is a subcommand of, or {@code null} if this is a top-level command. * @return the command that this is a subcommand of, or {@code null} if this is a top-level command * @see #addSubcommand(String, Object) * @see Command#subcommands() * @since 0.9.8 */ public CommandLine getParent() { CommandSpec parent = getCommandSpec().parent(); return parent == null ? null : parent.commandLine(); }
/** * Returns the map of section keys and renderers used to construct the usage help message. * The usage help message can be customized by adding, replacing and removing section renderers from this map. * Sections can be reordered with {@link #setHelpSectionKeys(List) setSectionKeys}. * Sections that are either not in this map or not in the list returned by {@link #getHelpSectionKeys() getSectionKeys} are omitted. * <p> * NOTE: By modifying the returned {@code Map}, only the usage help message <em>of this command</em> is affected. * Use {@link #setHelpSectionMap(Map)} to customize the usage help message for this command <em>and all subcommands</em>. * </p> * @since 3.9 */ public Map<String, IHelpSectionRenderer> getHelpSectionMap() { return getCommandSpec().usageMessage().sectionMap(); }
/** Returns whether the parser interprets the first positional parameter as "end of options" so the remaining * arguments are all treated as positional parameters. The default is {@code false}. * @return {@code true} if all values following the first positional parameter should be treated as positional parameters, {@code false} otherwise * @since 2.3 */ public boolean isStopAtPositional() { return getCommandSpec().parser().stopAtPositional(); }
/** Sets the command name (also called program name) displayed in the usage help synopsis to the specified value. * Note that this method only modifies the usage help message, it does not impact parsing behaviour. * The command name may also be set declaratively with the {@link CommandLine.Command#name()} annotation attribute. * @param commandName command name (also called program name) displayed in the usage help synopsis * @return this {@code CommandLine} object, to allow method chaining * @see CommandSpec#name(String) * @since 2.0 */ public CommandLine setCommandName(String commandName) { getCommandSpec().name(Assert.notNull(commandName, "commandName")); return this; }
/** Returns the {@code IHelpFactory} that is used to construct the usage help message. * @see #setHelpFactory(IHelpFactory) * @since 3.9 */ public IHelpFactory getHelpFactory() { return getCommandSpec().usageMessage().helpFactory(); }
/** 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(); }
/** Returns whether to use a simplified argument file format that is compatible with JCommander. * In this format, every line (except empty lines and comment lines) * is interpreted as a single argument. Arguments containing whitespace do not need to be quoted. * When system property {@code "picocli.useSimplifiedAtFiles"} is defined, the system property value overrides the programmatically set value. * @return whether to use a simplified argument file format. The default is {@code false}. * @since 3.9 */ public boolean isUseSimplifiedAtFiles() { return getCommandSpec().parser().useSimplifiedAtFiles(); }
private static CommandSpec findCommandFor(PositionalParamSpec positional, CommandSpec commandSpec) { for (PositionalParamSpec defined : commandSpec.positionalParameters()) { if (defined == positional) { return commandSpec; } } for (CommandLine sub : commandSpec.subcommands().values()) { CommandSpec result = findCommandFor(positional, sub.getCommandSpec()); if (result != null) { return result; } } return null; } private static boolean isPicocliModelObject(Object obj) {
private void initCommandHierarchyWithResourceBundle(String bundleBaseName) { if (resourceBundle() == null) { resourceBundleBaseName(bundleBaseName); } for (CommandLine sub : commands.values()) { // percolate down the hierarchy sub.getCommandSpec().initCommandHierarchyWithResourceBundle(bundleBaseName); } }
@Test public void testCompletionCandidatesPriority_forParameters() { class App { @Parameters(completionCandidates = MyAbcdCandidates.class) MyEfgEnum x; } App app = new App(); CommandLine cmd = new CommandLine(app); assertEquals(Arrays.asList("A", "B", "C", "D"), cmd.getCommandSpec().positionalParameters().get(0).completionCandidates()); } @Test
@Test public void testAssertNoMissingParametersOption() { class App { @Option(names = "-x") int x; } CommandLine cmd = new CommandLine(new App()); cmd.getCommandSpec().parser().collectErrors(true); ParseResult parseResult = cmd.parseArgs("-x"); List<Exception> errors = parseResult.errors(); assertEquals(1, errors.size()); assertEquals("Missing required parameter for option '-x' (<x>)", errors.get(0).getMessage()); }
@Test public void testAssertNoMissingParametersPositional() { class App { @Parameters(arity = "1") int x; } CommandLine cmd = new CommandLine(new App()); cmd.getCommandSpec().parser().collectErrors(true); ParseResult parseResult = cmd.parseArgs(); List<Exception> errors = parseResult.errors(); assertEquals(1, errors.size()); assertEquals("Missing required parameter: <x>", errors.get(0).getMessage()); }
@Test public void testInject_AnnotatedFieldInjected() { class Injected { @Spec CommandSpec commandSpec; @Parameters String[] params; } Injected injected = new Injected(); assertNull(injected.commandSpec); CommandLine cmd = new CommandLine(injected); assertSame(cmd.getCommandSpec(), injected.commandSpec); }
@Test public void testCompletionCandidatesValues_forOption() { class App { @Option(names = "-x", completionCandidates = MyAbcdCandidates.class) String x; } CommandLine cmd = new CommandLine(new App()); assertEquals(Arrays.asList("A", "B", "C", "D"), extract(cmd.getCommandSpec().findOption("x").completionCandidates())); } @Test
@Test public void testCommandSpec_IsCommandLineCommandSpec() { class App { @Parameters String[] positional; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("a", "b"); assertSame(cmd.getCommandSpec(), result.commandSpec()); } @Test
@Test public void testDefaultCompletionCandidatesNull() { class App { @Option(names = "-x") int x; } CommandLine cmd = new CommandLine(new App()); Iterable<String> candidates = cmd.getCommandSpec().findOption('x').completionCandidates(); assertNull(candidates); }