Ejemplo n.º 1
0
  @Test
  public void testExamplesAndDiscussion() throws IOException {
    // @formatter:off
    Cli<?> parser = Cli.builder("git").withCommand(CommandRemove.class).build();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Help.help(parser.getMetadata(), AirlineUtils.singletonList("remove"), out);

    String discussion =
        "DISCUSSION\n"
            + "        More details about how this removes files from the index.\n"
            + "\n";

    String examples =
        "EXAMPLES\n"
            + "        $ git remove -i myfile.java\n\n"
            + "            This is a usage example";

    String usage = new String(out.toByteArray(), utf8);
    System.out.println(usage);
    assertTrue(
        usage.contains(discussion), "Expected the discussion section to be present in the help");
    assertTrue(usage.contains(examples), "Expected the examples section to be present in the help");
    // @formatter:on
  }
Ejemplo n.º 2
0
  @Test
  public void testGlobalOptionsHidden() throws IOException {
    // @formatter:off
    CliBuilder<Object> builder =
        Cli.builder("test")
            .withDescription("Test commandline")
            .withDefaultCommand(Help.class)
            .withCommands(Help.class, GlobalOptionsHidden.class);

    Cli<Object> parser = builder.build();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Help.help(parser.getMetadata(), AirlineUtils.singletonList("GlobalOptionsHidden"), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        test GlobalOptionsHidden -\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        test [ {-op | --optional} ] GlobalOptionsHidden\n"
            + "\n"
            + "OPTIONS\n"
            + "        -op, --optional\n"
            + "\n"
            + "\n");
    // @formatter:on
  }
Ejemplo n.º 3
0
  @Test
  public void testArgsRequired() throws IOException {
    // @formatter:off
    CliBuilder<Object> builder =
        Cli.builder("test")
            .withDescription("Test commandline")
            .withDefaultCommand(Help.class)
            .withCommands(Help.class, ArgsRequired.class);

    Cli<Object> parser = builder.build();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Help.help(parser.getMetadata(), AirlineUtils.singletonList("ArgsRequired"), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        test ArgsRequired -\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        test ArgsRequired [--] <parameters>...\n"
            + "\n"
            + "OPTIONS\n"
            + "        --\n"
            + "            This option can be used to separate command-line options from the\n"
            + "            list of arguments (useful when arguments might be mistaken for\n"
            + "            command-line options)\n"
            + "\n"
            + "        <parameters>\n"
            + "            List of files\n"
            + "\n");
    // @formatter:on
  }
Ejemplo n.º 4
0
  @Test
  public void testOptionsRequired() throws IOException {
    // @formatter:off
    CliBuilder<Object> builder =
        Cli.builder("test")
            .withDescription("Test commandline")
            .withDefaultCommand(Help.class)
            .withCommands(Help.class, OptionsRequired.class);

    Cli<Object> parser = builder.build();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Help.help(parser.getMetadata(), AirlineUtils.singletonList("OptionsRequired"), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        test OptionsRequired -\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        test OptionsRequired [ --optional <optionalOption> ]\n"
            + "                --required <requiredOption>\n"
            + "\n"
            + "OPTIONS\n"
            + "        --optional <optionalOption>\n"
            + "\n"
            + "\n"
            + "        --required <requiredOption>\n"
            + "\n"
            + "\n");
    // @formatter:on
  }
Ejemplo n.º 5
0
  @Test
  public void testArgsBooleanArity() throws IOException {
    // @formatter:off
    CliBuilder<Object> builder =
        Cli.builder("test")
            .withDescription("Test commandline")
            .withDefaultCommand(Help.class)
            .withCommands(Help.class, ArgsBooleanArity.class);

    Cli<Object> parser = builder.build();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Help.help(parser.getMetadata(), AirlineUtils.singletonList("ArgsBooleanArity"), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        test ArgsBooleanArity -\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        test ArgsBooleanArity [ -debug <debug> ]\n"
            + "\n"
            + "OPTIONS\n"
            + "        -debug <debug>\n"
            + "\n"
            + "\n");
    // @formatter:on
  }
Ejemplo n.º 6
0
  @Test
  public void testArgs1() throws IOException {
    // @formatter:off
    CliBuilder<Object> builder =
        Cli.builder("test")
            .withDescription("Test commandline")
            .withDefaultCommand(Help.class)
            .withCommands(Help.class, Args1.class);

    Cli<Object> parser = builder.build();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Help.help(parser.getMetadata(), AirlineUtils.singletonList("Args1"), out);
    testStringAssert(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        test Args1 - args1 description\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        test Args1 [ -bigdecimal <bigd> ] [ -date <date> ] [ -debug ]\n"
            + "                [ -double <doub> ] [ -float <floa> ] [ -groups <groups> ]\n"
            + "                [ {-log | -verbose} <verbose> ] [ -long <l> ] [--] [ <parameters>... ]\n"
            + "\n"
            + "OPTIONS\n"
            + "        -bigdecimal <bigd>\n"
            + "            A BigDecimal number\n"
            + "\n"
            + "        -date <date>\n"
            + "            An ISO 8601 formatted date.\n"
            + "\n"
            + "        -debug\n"
            + "            Debug mode\n"
            + "\n"
            + "        -double <doub>\n"
            + "            A double number\n"
            + "\n"
            + "        -float <floa>\n"
            + "            A float number\n"
            + "\n"
            + "        -groups <groups>\n"
            + "            Comma-separated list of group names to be run\n"
            + "\n"
            + "        -log <verbose>, -verbose <verbose>\n"
            + "            Level of verbosity\n"
            + "\n"
            + "        -long <l>\n"
            + "            A long number\n"
            + "\n"
            + "        --\n"
            + "            This option can be used to separate command-line options from the\n"
            + "            list of arguments (useful when arguments might be mistaken for\n"
            + "            command-line options)\n"
            + "\n"
            + "        <parameters>\n"
            + "\n"
            + "\n");
    // @formatter:on
  }
Ejemplo n.º 7
0
  /**
   * Tries to find an option with the given name
   *
   * @param state Current parser state
   * @param options Allowed options
   * @param name Name
   * @param defaultValue Default value to return if nothing found
   * @return Option if found, {@code defaultValue} otherwise
   */
  protected final OptionMetadata findOption(
      ParseState<T> state,
      List<OptionMetadata> options,
      final String name,
      OptionMetadata defaultValue) {
    Predicate<OptionMetadata> findOptionPredicate;
    if (state.getParserConfiguration().allowsAbbreviatedOptions()) {
      findOptionPredicate = new AbbreviatedOptionFinder(name, options);
    } else {
      findOptionPredicate = new OptionFinder(name);
    }

    return AirlineUtils.find(options, findOptionPredicate, defaultValue);
  }
Ejemplo n.º 8
0
  @Test
  public void testArgsInherited() throws IOException {
    // @formatter:off
    CliBuilder<Object> builder =
        Cli.builder("test")
            .withDescription("Test commandline")
            .withDefaultCommand(Help.class)
            .withCommands(Help.class, ArgsInherited.class);

    Cli<Object> parser = builder.build();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Help.help(parser.getMetadata(), AirlineUtils.singletonList("ArgsInherited"), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        test ArgsInherited -\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        test ArgsInherited [ -child <child> ] [ -debug ] [ -groups <groups> ]\n"
            + "                [ -level <level> ] [ -log <log> ] [--] [ <parameters>... ]\n"
            + "\n"
            + "OPTIONS\n"
            + "        -child <child>\n"
            + "            Child parameter\n"
            + "\n"
            + "        -debug\n"
            + "            Debug mode\n"
            + "\n"
            + "        -groups <groups>\n"
            + "            Comma-separated list of group names to be run\n"
            + "\n"
            + "        -level <level>\n"
            + "            A long number\n"
            + "\n"
            + "        -log <log>\n"
            + "            Level of verbosity\n"
            + "\n"
            + "        --\n"
            + "            This option can be used to separate command-line options from the\n"
            + "            list of arguments (useful when arguments might be mistaken for\n"
            + "            command-line options)\n"
            + "\n"
            + "        <parameters>\n"
            + "\n"
            + "\n");
    // @formatter:on
  }
Ejemplo n.º 9
0
  @Test
  public void testCommandHidden() throws IOException {
    // @formatter:off
    CliBuilder<Object> builder =
        Cli.builder("test")
            .withDescription("Test commandline")
            .withDefaultCommand(Help.class)
            .withCommands(Help.class, ArgsRequired.class, CommandHidden.class);

    Cli<Object> parser = builder.build();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Help.help(parser.getMetadata(), Collections.<String>emptyList(), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "usage: test <command> [ <args> ]\n"
            + "\n"
            + "Commands are:\n"
            + "    ArgsRequired\n"
            + "    help           Display help information\n"
            + "\n"
            + "See 'test help <command>' for more information on a specific command.\n");

    out = new ByteArrayOutputStream();
    Help.help(parser.getMetadata(), AirlineUtils.singletonList("CommandHidden"), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        test CommandHidden -\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        test CommandHidden [ --optional <optionalOption> ]\n"
            + "\n"
            + "OPTIONS\n"
            + "        --optional <optionalOption>\n"
            + "\n"
            + "\n");
    // @formatter:on
  }
Ejemplo n.º 10
0
  public void testGit() throws IOException {
    // @formatter:off
    CliBuilder<Runnable> builder =
        Cli.<Runnable>builder("git")
            .withDescription("the stupid content tracker")
            .withDefaultCommand(Help.class)
            .withCommand(Help.class)
            .withCommand(Add.class);

    builder
        .withGroup("remote")
        .withDescription("Manage set of tracked repositories")
        .withDefaultCommand(RemoteShow.class)
        .withCommand(RemoteShow.class)
        .withCommand(RemoteAdd.class);

    Cli<Runnable> gitParser = builder.build();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Help.help(gitParser.getMetadata(), Collections.<String>emptyList(), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "usage: git [ -v ] <command> [ <args> ]\n"
            + "\n"
            + "Commands are:\n"
            + "    add      Add file contents to the index\n"
            + "    help     Display help information\n"
            + "    remote   Manage set of tracked repositories\n"
            + "\n"
            + "See 'git help <command>' for more information on a specific command.\n");

    out = new ByteArrayOutputStream();
    Help.help(gitParser.getMetadata(), AirlineUtils.singletonList("add"), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        git add - Add file contents to the index\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        git [ -v ] add [ -i ] [--] [ <patterns>... ]\n"
            + "\n"
            + "OPTIONS\n"
            + "        -i\n"
            + "            Add modified contents interactively.\n"
            + "\n"
            + "        -v\n"
            + "            Verbose mode\n"
            + "\n"
            + "        --\n"
            + "            This option can be used to separate command-line options from the\n"
            + "            list of arguments (useful when arguments might be mistaken for\n"
            + "            command-line options)\n"
            + "\n"
            + "        <patterns>\n"
            + "            Patterns of files to be added\n"
            + "\n");

    out = new ByteArrayOutputStream();
    Help.help(gitParser.getMetadata(), AirlineUtils.singletonList("remote"), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        git remote - Manage set of tracked repositories\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        git [ -v ] remote { add | show* } [--] [cmd-options] <cmd-args>\n"
            + "\n"
            + "        Where command-specific options [cmd-options] are:\n"
            + "            add: [ -t <branch> ]\n"
            + "            show: [ -n ]\n"
            + "\n"
            + "        Where command-specific arguments <cmd-args> are:\n"
            + "            add: [ <name> <url>... ]\n"
            + "            show: [ <remote> ]\n"
            + "\n"
            + "        Where * indicates the default command(s)\n"
            + "        See 'git help remote <command>' for more information on a specific command.\n"
            + "OPTIONS\n"
            + "        -v\n"
            + "            Verbose mode\n"
            + "\n");

    out = new ByteArrayOutputStream();
    Help.help(
        gitParser.getMetadata(), AirlineUtils.arrayToList(new String[] {"remote", "add"}), out);
    assertEquals(
        new String(out.toByteArray(), utf8),
        "NAME\n"
            + "        git remote add - Adds a remote\n"
            + "\n"
            + "SYNOPSIS\n"
            + "        git [ -v ] remote add [ -t <branch> ] [--] [ <name> <url>... ]\n"
            + "\n"
            + "OPTIONS\n"
            + "        -t <branch>\n"
            + "            Track only a specific branch\n"
            + "\n"
            + "        -v\n"
            + "            Verbose mode\n"
            + "\n"
            + "        --\n"
            + "            This option can be used to separate command-line options from the\n"
            + "            list of arguments (useful when arguments might be mistaken for\n"
            + "            command-line options)\n"
            + "\n"
            + "        <name> <url>\n"
            + "            Name and URL of remote repository to add\n"
            + "\n");
    // @formatter:on
  }