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

Support to print usage explicitly or on error. #39

Open
DALDEI opened this issue Dec 28, 2017 · 4 comments
Open

Support to print usage explicitly or on error. #39

DALDEI opened this issue Dec 28, 2017 · 4 comments

Comments

@DALDEI
Copy link

DALDEI commented Dec 28, 2017

On any SystemExitException except for --help there is no obvious way I could find to print the help text.
In general on error I'd like to print the help text.

The best I could do was to recursively call main("--help") then some ugly code to prevent infinte recursion.

snippet:


catch (h: SystemExitException)
  {
    var w = StringWriter()
    w.use {
      h.printUserMessage(it, progName="maildb-cli", columns=120)
    }
    System.out.println( w.toString() )
    if( h is ShowHelpException ) System.exit(0)
    main(arrayOf("--help"))
  }

Other libraries like JCommander, joptsimple etc provide some way to call the usage() method and/or some way to trigger it being displayed on an error.

The above code is embarrassingly ugly.
I attempted something slightly less ugly - to create a ShowHelpMessageException() but the constructor is internal, plus I don't have access to the parsed help values.

@xenomachina
Copy link
Owner

This is a good idea!

Are you just asking for something you can call to display the help, or do you also want an easy way to enable help printing on error (like perhaps a "usageOnError" flag to mainBody)?

@davidgodzsak
Copy link

davidgodzsak commented Mar 6, 2018

I think it would be nice to have something like ArgPareser(args).printHelp()

@davidgodzsak
Copy link

Any progress on this one?

@xenomachina
Copy link
Owner

Sorry, no, I haven't had a chance to work on this one yet.

I don't think it would be hard to implement though. If you're interested I'd be happy to review a pull request. I'm thinking a printHelp() method like you mentioned, and a way to tell mainBody that it should call printHelp() on error (ie: when handling a SystemExitException and returnCode != 0).

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

No branches or pull requests

3 participants