airlift / airline

Java annotation-based framework for parsing Git like command line structures
Apache License 2.0
846 stars 138 forks source link

ParseArgumentsUnexpectedException although following usage #47

Closed fabienrenaud closed 9 years ago

fabienrenaud commented 9 years ago

java -jar app-jar-with-dependencies.jar help a

NAME
        app a -

SYNOPSIS
        app [(-n <number> | --number <number>)] a

OPTIONS
        -n <number>, --number <number>

java -jar app-jar-with-dependencies.jar a --number 1

Exception in thread "main" io.airlift.airline.ParseArgumentsUnexpectedException: Found unexpected parameters: [--number, 1]
    at io.airlift.airline.Cli.validate(Cli.java:148)
    at io.airlift.airline.Cli.parse(Cli.java:116)
    at io.airlift.airline.Cli.parse(Cli.java:97)
    at my.App.main(App.java:23)```

Text book example:

public class App {

  public static void main(String[] args) {
    CliBuilder<Runnable> builder = new Cli.CliBuilder<Runnable>("app")
      .withDescription("The app cli")
      .withDefaultCommand(Help.class)
      .withCommands(Help.class, A.class);

    Cli<Runnable> parser = builder.build();
    parser.parse(args).run();
  }

  public static abstract class Abstract implements Runnable {

    @Option(name = {"-n", "--number"}, type = OptionType.GLOBAL)
    public int number;
  }

  @Command(name = "a")
  public static final class A extends Abstract {

    @Override
    public void run() {
      System.out.println(number);
    }

  }
}
fabienrenaud commented 9 years ago

Hmm, the order matters. Would be nice to document the difference between OptionType.GLOBAL and COMMAND because i actually wanted to the use the latter... Problem solved.