I use the excellent Python Click library for handling command line options in my tool. Here's a simplified version of my code (full script here):
@click.command(
context_settings = dict( help_option_names = ['-h', '--help'] )
)
@click.argument('analysis_dir',
type = click.Path(exists=True),
nargs = -1,
required = True,
metavar = "<analysis directory>"
)
def mytool(analysis_dir):
""" Do stuff """
if __name__ == "__main__":
mytool()
If someone runs the command without any flags, they get the default click error message:
$ mytool
Usage: mytool [OPTIONS] <analysis directory>
Error: Missing argument "analysis_dir".
This is nice, but I'd quite like to tell (very) novice users that more help is available by using the help flag. In other words, add a custom sentence to the error message when the command is invalid telling people to try mytool --help
for more information.
Is there an easy way to do this? I know I could remove the required
attribute and handle this logic in the main function, but that feels kind of hacky for such a minor addition.
command.main(['command-name', 'args', 'go', 'here'])
syntax. I couldn't find any examples anywhere and gave up. If you have any pointers as to how to apply this with the syntax in the example above, that would be really helpful! – Giselagiselbert