kotlin-argparser icon indicating copy to clipboard operation
kotlin-argparser copied to clipboard

Support to print usage explicitly or on error.

Open DALDEI opened this issue 7 years ago • 4 comments

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.

DALDEI avatar Dec 28 '17 14:12 DALDEI

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)?

xenomachina avatar Jan 18 '18 19:01 xenomachina

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

davidgodzsak avatar Mar 06 '18 09:03 davidgodzsak

Any progress on this one?

davidgodzsak avatar Mar 29 '18 07:03 davidgodzsak

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).

xenomachina avatar Mar 29 '18 16:03 xenomachina