private void printCommandSpec(CommandSpec spec, String label, PrintWriter pw, String initialIndent, String indent) { pw.printf("%s%n", label); pw.printf("%sname: '%s'%n", initialIndent, spec.name()); pw.printf("%saliases: %s%n", indent, Arrays.toString(spec.aliases())); pw.printf("%suserObject: %s%n", indent, spec.userObject()); pw.printf("%shelpCommand: %s%n", indent, spec.helpCommand()); pw.printf("%sdefaultValueProvider: %s%n", indent, spec.defaultValueProvider()); pw.printf("%sversionProvider: %s%n", indent, spec.versionProvider()); pw.printf("%sversion: %s%n", indent, Arrays.toString(spec.version())); List<OptionSpec> options = new ArrayList<OptionSpec>(spec.options()); Collections.sort(options, new Comparator<OptionSpec>() { public int compare(OptionSpec o1, OptionSpec o2) { return o1.shortestName().compareTo(o2.shortestName()); } }); printOptionList(options, pw, indent); printPositionalList(spec.positionalParameters(), pw, indent); printUnmatchedArgsBindingList(spec.unmatchedArgsBindings(), pw, indent); printMixinList(spec.mixins(), pw, indent); printUsageMessage(spec.usageMessage(), pw, indent); printParser(spec.parser(), pw, indent); printResourceBundle(spec.resourceBundle(), pw, indent); printSubcommandList(spec.subcommands(), pw, indent); }
private void initCommandHierarchyWithResourceBundle(String bundleBaseName) { if (resourceBundle() == null) { resourceBundleBaseName(bundleBaseName); } for (CommandLine sub : commands.values()) { // percolate down the hierarchy sub.getCommandSpec().initCommandHierarchyWithResourceBundle(bundleBaseName); } }
/** Sets the ResourceBundle containing usage help message strings. * <p>The specified bundle 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 not be impacted. To ensure a setting is applied to all * subcommands, call the setter last, after adding subcommands.</p> * @param bundle the ResourceBundle containing usage help message strings * @return this {@code CommandLine} object, to allow method chaining * @see Command#resourceBundle() * @see CommandSpec#resourceBundle(ResourceBundle) * @since 3.6 */ public CommandLine setResourceBundle(ResourceBundle bundle) { getCommandSpec().resourceBundle(bundle); for (CommandLine command : getCommandSpec().subcommands().values()) { command.getCommandSpec().resourceBundle(bundle); } return this; }
@Test public void testCommandSpecSetResourceBundle_doesNotOverwriteSubcommandBundle() { Locale original = Locale.getDefault(); Locale.setDefault(Locale.ENGLISH); try { // init CommandLine top = new CommandLine(new I18nCommand()); CommandLine sub = top.getSubcommands().get("i18n-sub"); CommandLine help = top.getSubcommands().get("i18n-sub"); ResourceBundle orig = top.getResourceBundle(); ResourceBundle update = ResourceBundle.getBundle("picocli.I18nSuperclass_Messages"); assertNotSame(update, orig); // exercise SUT top.getCommandSpec().resourceBundle(update); // verify: command was modified assertSame(update, top.getResourceBundle()); assertSame(update, top.getCommandSpec().resourceBundle()); assertArgsHaveBundle(update, top.getCommandSpec().options()); assertArgsHaveBundle(update, top.getCommandSpec().positionalParameters()); // verify: subcommands not modified assertSame(orig, sub.getResourceBundle()); assertArgsHaveBundle(orig, sub.getCommandSpec().options()); assertArgsHaveBundle(orig, sub.getCommandSpec().positionalParameters()); assertSame(orig, help.getResourceBundle()); assertArgsHaveBundle(orig, help.getCommandSpec().options()); assertArgsHaveBundle(orig, help.getCommandSpec().positionalParameters()); } finally { Locale.setDefault(original); } }
/** Sets the ResourceBundle containing usage help message strings. * <p>The specified bundle 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 not be impacted. To ensure a setting is applied to all * subcommands, call the setter last, after adding subcommands.</p> * @param bundle the ResourceBundle containing usage help message strings * @return this {@code CommandLine} object, to allow method chaining * @see Command#resourceBundle() * @see CommandSpec#resourceBundle(ResourceBundle) * @since 3.6 */ public CommandLine setResourceBundle(ResourceBundle bundle) { getCommandSpec().resourceBundle(bundle); for (CommandLine command : getCommandSpec().subcommands().values()) { command.getCommandSpec().resourceBundle(bundle); } return this; }
/** Adds the specified subcommand with the specified name. * If the specified subcommand does not have a ResourceBundle set, it is initialized to the ResourceBundle of this command spec. * @param name subcommand name - when this String is encountered in the command line arguments the subcommand is invoked * @param subCommandLine the subcommand to envoke when the name is encountered on the command line * @return this {@code CommandSpec} object for method chaining */ public CommandSpec addSubcommand(String name, CommandLine subCommandLine) { CommandLine previous = commands.put(name, subCommandLine); if (previous != null && previous != subCommandLine) { throw new InitializationException("Another subcommand named '" + name + "' already exists for command '" + this.name() + "'"); } CommandSpec subSpec = subCommandLine.getCommandSpec(); if (subSpec.name == null) { subSpec.name(name); } subSpec.parent(this); for (String alias : subSpec.aliases()) { previous = commands.put(alias, subCommandLine); if (previous != null && previous != subCommandLine) { throw new InitializationException("Alias '" + alias + "' for subcommand '" + name + "' is already used by another subcommand of '" + this.name() + "'"); } } subSpec.initResourceBundle(resourceBundle()); return this; } private void initResourceBundle(ResourceBundle bundle) {
private void initResourceBundle(ResourceBundle bundle) { if (resourceBundle() == null) { resourceBundle(bundle); } for (CommandLine sub : commands.values()) { // percolate down the hierarchy sub.getCommandSpec().initResourceBundle(resourceBundle()); } }
/** Returns the ResourceBundle of this command or {@code null} if no resource bundle is set. * @see Command#resourceBundle() * @see CommandSpec#resourceBundle() * @since 3.6 */ public ResourceBundle getResourceBundle() { return getCommandSpec().resourceBundle(); }
@Test public void testCommandSpecAddSubcommand_SubcommandInheritsResourceBundle() { ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages"); CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.resourceBundle(rb); assertSame(rb, spec.resourceBundle()); CommandSpec sub = CommandSpec.wrapWithoutInspection(null); spec.addSubcommand("a", new CommandLine(sub)); assertSame(rb, sub.resourceBundle()); }
@Test public void testCommandSpecSetResourceBundle_doesNotOverwriteSubcommandBundle() { Locale original = Locale.getDefault(); Locale.setDefault(Locale.ENGLISH); try { // init CommandLine top = new CommandLine(new I18nCommand()); CommandLine sub = top.getSubcommands().get("i18n-sub"); CommandLine help = top.getSubcommands().get("i18n-sub"); ResourceBundle orig = top.getResourceBundle(); ResourceBundle update = ResourceBundle.getBundle("picocli.I18nSuperclass_Messages"); assertNotSame(update, orig); // exercise SUT top.getCommandSpec().resourceBundle(update); // verify: command was modified assertSame(update, top.getResourceBundle()); assertSame(update, top.getCommandSpec().resourceBundle()); assertArgsHaveBundle(update, top.getCommandSpec().options()); assertArgsHaveBundle(update, top.getCommandSpec().positionalParameters()); // verify: subcommands not modified assertSame(orig, sub.getResourceBundle()); assertArgsHaveBundle(orig, sub.getCommandSpec().options()); assertArgsHaveBundle(orig, sub.getCommandSpec().positionalParameters()); assertSame(orig, help.getResourceBundle()); assertArgsHaveBundle(orig, help.getCommandSpec().options()); assertArgsHaveBundle(orig, help.getCommandSpec().positionalParameters()); } finally { Locale.setDefault(original); } }
/** Returns the ResourceBundle of this command or {@code null} if no resource bundle is set. * @see Command#resourceBundle() * @see CommandSpec#resourceBundle() * @since 3.6 */ public ResourceBundle getResourceBundle() { return getCommandSpec().resourceBundle(); }
@Test public void testCommandSpecAddSubcommand_SubcommandInheritsResourceBundle() { ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages"); CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.resourceBundle(rb); assertSame(rb, spec.resourceBundle()); CommandSpec sub = CommandSpec.wrapWithoutInspection(null); spec.addSubcommand("a", new CommandLine(sub)); assertSame(rb, sub.resourceBundle()); }