jatoben/CommandLine

Provide a Default Help Option

emorydunn opened this issue · 4 comments

I'm starting a new project that was prototyped in Python, and one of the differences I noticed between Python's ArgParse and CommandLine is the lack of a default help flag.

To me, at least, having a a help flag included by default would be useful as most applications would benefit from having a simple -h, --help to provide usage and, for the most part, the what the flag needs to do doesn't change (borrowing from ArgParse's help message): "show this help message and exit".

I made made some modifications to my local repo:

  • Added CommandLine.addHelp(option: BoolOption? = nil) to add a default help option, or override with a custom one. This sets ._helpOption.
  • Updated .parse() to throw a new .HelpOption error. This follows my expected behavior where usage info is printed and then exits.
  • Added a CommandLine.description that is used as the error description

I'm still messing around with the updates, but am planning on making a pull request if people think it's a good idea.

My own todo list on this is possibly integrate the description and/ or overriding the default help option into the CommandLine init.

@emorydunn That sounds like a nice improvement! I'm not quite sure about throwing an error from parse() though; what benefit does that bring over just having parse() call printUsage() and exiting?

There are a few reasons for having parse() throw an error:

  1. Use the exit mechanism already in place (catching an error on parse, printing usage, and exiting)
  2. To provide the CommandLine.description in place of an error description (e.g. missing required arguments) in the output.
  3. It required minimal changes to get the desired result

Of course, that may not be the best way to do this.

Edit: I've properly forked and uploaded my changes, so you can take a look.

Another good reason to throw the error would be that it enables use cases where the app is to continue following the help output.

This repository is being archived. See #111.