Java Code Examples for picocli.CommandLine.Model.CommandSpec#mixinStandardHelpOptions()

The following examples show how to use picocli.CommandLine.Model.CommandSpec#mixinStandardHelpOptions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testMixinStandardHelpOptions_SettingToTrueAddsHelpOptions() {
    CommandSpec spec = CommandSpec.create();
    assertTrue(spec.mixins().isEmpty());
    assertTrue(spec.optionsMap().isEmpty());
    assertTrue(spec.posixOptionsMap().isEmpty());
    assertTrue(spec.options().isEmpty());

    spec.mixinStandardHelpOptions(true);
    assertFalse(spec.mixins().isEmpty());
    assertFalse(spec.optionsMap().isEmpty());
    assertFalse(spec.posixOptionsMap().isEmpty());
    assertFalse(spec.options().isEmpty());
    assertTrue(spec.mixinStandardHelpOptions());

    OptionSpec usageHelp = spec.posixOptionsMap().get('h');
    assertSame(usageHelp, spec.optionsMap().get("--help"));
    assertTrue(usageHelp.usageHelp());

    OptionSpec versionHelp = spec.posixOptionsMap().get('V');
    assertSame(versionHelp, spec.optionsMap().get("--version"));
    assertTrue(versionHelp.versionHelp());
}
 
Example 2
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testMixinStandardHelpOptions_SettingToFalseRemovesHelpOptions() {
    CommandSpec spec = CommandSpec.create();

    spec.mixinStandardHelpOptions(true);
    assertFalse(spec.mixins().isEmpty());
    assertFalse(spec.optionsMap().isEmpty());
    assertFalse(spec.posixOptionsMap().isEmpty());
    assertFalse(spec.options().isEmpty());
    assertTrue(spec.mixinStandardHelpOptions());

    assertNotNull(spec.posixOptionsMap().get('h'));
    assertNotNull(spec.optionsMap().get("--help"));

    assertNotNull(spec.posixOptionsMap().get('V'));
    assertNotNull(spec.optionsMap().get("--version"));

    spec.mixinStandardHelpOptions(false);
    assertTrue(spec.mixins().isEmpty());
    assertTrue(spec.optionsMap().isEmpty());
    assertTrue(spec.posixOptionsMap().isEmpty());
    assertTrue(spec.options().isEmpty());
    assertFalse(spec.mixinStandardHelpOptions());
}
 
Example 3
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageMessageFromProgrammaticCommandSpec() {
    CommandSpec spec = CommandSpec.create();
    spec.addOption(OptionSpec.builder("-x").splitRegex("").build());
    spec.addPositional(PositionalParamSpec.builder().splitRegex("").build());
    spec.mixinStandardHelpOptions(true);
    String actual = new CommandLine(spec).getUsageMessage(Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [-hVx] PARAM%n" +
            "      PARAM%n" +
            "  -h, --help      Show this help message and exit.%n" +
            "  -V, --version   Print version information and exit.%n" +
            "  -x%n");
    assertEquals(expected, actual);
}
 
Example 4
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageMessageFromProgrammaticCommandSpecWithSplitRegexSynopsisLabel() {
    CommandSpec spec = CommandSpec.create();
    spec.addOption(OptionSpec.builder("-x").type(String[].class).splitRegex("").splitRegexSynopsisLabel(";").build());
    spec.addPositional(PositionalParamSpec.builder().type(String[].class).splitRegex("").splitRegexSynopsisLabel(";").build());
    spec.mixinStandardHelpOptions(true);
    String actual = new CommandLine(spec).getUsageMessage(Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [-hV] [-x=PARAM]... PARAM...%n" +
            "      PARAM...%n" +
            "  -h, --help      Show this help message and exit.%n" +
            "  -V, --version   Print version information and exit.%n" +
            "  -x=PARAM%n");
    assertEquals(expected, actual);
}
 
Example 5
Source File: BasicResultProcessing.java    From picocli with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {

        CommandSpec spec = CommandSpec.create();
        spec.mixinStandardHelpOptions(true);
        spec.addOption(OptionSpec.builder("-c", "--count")
                .paramLabel("COUNT")
                .type(int.class)
                .description("number of times to execute").build());
        spec.addPositional(PositionalParamSpec.builder()
                .paramLabel("FILES")
                .type(List.class)
                .auxiliaryTypes(File.class)
                .description("The files to process").build());
        CommandLine commandLine = new CommandLine(spec);

        try {
            ParseResult pr = commandLine.parseArgs(args);
            if (CommandLine.printHelpIfRequested(pr)) { return; }

            int count = pr.matchedOptionValue('c', 1);
            List<File> files = pr.matchedPositionalValue(0, Collections.<File>emptyList());
            for (File f : files) {
                for (int i = 0; i < count; i++) {
                    System.out.println(i + " " + f.getName());
                }
            }

        } catch (ParameterException ex) {
            System.err.println(ex.getMessage());
            ex.getCommandLine().usage(System.err);
        }
    }
 
Example 6
Source File: ExecutionStrategyWithExecutionResult.java    From picocli with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {

        CommandSpec spec = CommandSpec.create();
        spec.mixinStandardHelpOptions(true);
        spec.addOption(OptionSpec.builder("-c", "--count")
                .paramLabel("COUNT")
                .type(int.class)
                .description("number of times to execute").build());
        spec.addPositional(PositionalParamSpec.builder()
                .paramLabel("FILES")
                .type(List.class)
                .auxiliaryTypes(File.class)
                .description("The files to process").build());
        CommandLine commandLine = new CommandLine(spec);

        class Handler implements IExecutionStrategy {
            @Override
            public int execute(ParseResult pr) {
                int count = pr.matchedOptionValue('c', 1);
                List<File> files = pr.matchedPositionalValue(0, Collections.<File>emptyList());
                for (File f : files) {
                    for (int i = 0; i < count; i++) {
                        System.out.println(i + " " + f.getName());
                    }
                }
                pr.commandSpec().commandLine().setExecutionResult(files.size());
                return ExitCode.OK;
            }
        }

        commandLine.setExecutionStrategy(new Handler());
        commandLine.execute(args);
        int processed = commandLine.getExecutionResult();
    }