Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically show help when user types invalid command? #45

Open
ChrisLMerrill opened this issue Sep 13, 2015 · 2 comments
Open

Automatically show help when user types invalid command? #45

ChrisLMerrill opened this issue Sep 13, 2015 · 2 comments

Comments

@ChrisLMerrill
Copy link

When the user enters a command that doesn't exist (e.g. mis-typed a command), an exception is thrown.

Exception in thread "main" io.airlift.airline.ParseArgumentsUnexpectedException: Found unexpected parameters: [blah, something]
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)

Since the end-user mistyping a command is an obvious use case, I'm assuming I've done something wrong. I've set Help as the default command, but I guess that isn't it.

    Cli.CliBuilder<Runnable> builder = Cli.<Runnable>builder("muse")
        .withDescription("Muse command-line tools")
        .withDefaultCommand(Help.class)
        .withCommands(Help.class)
        .withCommands(implementors);
    Cli<Runnable> muse_parser = builder.build();
    muse_parser.parse(args).run();

My fix thus far is to catch the exception and run the Help command by re-parsing an empty command line. That seems like a hack. new Help().run() threw an exception, too, so that wasn't any better.

I feel like I'm missing an obvious solution, since none of the examples include catching an exception.

TIA!
Chris

@petercoulton
Copy link

Totally agree --help is the first thing I run on a new command so seems like it should be supported. I have a workaround for now though

try {
  parser.parse(args).run();
} catch (ParseArgumentsUnexpectedException e) {

  final List<String> commandNames =
      reportingParser.getMetadata().getCommandGroups().stream()
          .flatMap(cg -> cg.getCommands().stream().map(CommandMetadata::getName))
          .collect(Collectors.toList());

  Help.help(parser.getMetadata(), commandNames);
}

I haven't looked much into the airline code base yet, but I'll have a look and try to put a pull request together with a better solution, unless the maintainers are philosophically against this behaviour for some reason.

@rvesse
Copy link

rvesse commented Sep 10, 2018

Now possible in my fork and documented here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants