Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the default usage/help be shown from plac (Python command line parser)

I have a simple command line program built using "plac", a fantastic Python command line parser, and I am trying to programmatically print out the default usage (aka. "help" output) for a particular command.

I could not find anything in the "plac" documentation. Any help is appreciated.

Below is a sample program:

import plac

@plac.annotations(
    bootstrap=plac.Annotation("bootstrap", "option", "b", str),
    repo=plac.Annotation("repo to load", "option", "r", str),
    similar=plac.Annotation("term (word|pos) to find similarities, requires 'term'", "flag", "s"),
    mostsimilar=plac.Annotation("similar words to find, requires 'term'", "flag", "m"),
    similarity=plac.Annotation("similarity between two words, requires 'term' and 'term2", "flag", "S"),
    term=plac.Annotation("term (word|pos)", "option", "t", str),
    term2=plac.Annotation("second term (word|pos)", "option", "T", str)
)
def main(bootstrap, repo, similar, mostsimilar, similarity, term, term2):
    :
    if error:
        # PRINT USAGE HERE
    :
if __name__ == '__main__':
    plac.call(main)

Upon a command line error I would like to use code ("PRINT ERROR HERE" in the above sample) to show the usage identically as when I issue the help command ("python myCLI.py -h") directly:

usage: myCLI.py [-h] [-b BOOTSTRAP] [-r REPO] [-s] [-m] [-S] [-t TERM] [-T TERM2]

optional arguments:
  -h, --help            show this help message and exit
  -b BOOTSTRAP, --bootstrap BOOTSTRAP
                        bootstrap
  -r REPO, --repo REPO  repo to load
  -s, --similar         term (word|pos) to find similarities, requires 'term'
  -m, --mostsimilar     similar words to find, requires 'term'
  -S, --similarity      similarity between two words, requires 'term' and
                        'term2
  -t TERM, --term TERM  term (word|pos)
  -T TERM2, --term2 TERM2
                        second term (word|pos)
like image 307
Eric Broda Avatar asked Mar 16 '26 14:03

Eric Broda


1 Answers

After a bunch of reading and examining source code, and a few ideas kick started from @chepner and @hpaulj comments, I have found a solution that works. The following code snippet does print a help text identical to that if the CLI is called with the "-h" (help) flag:

parser = plac.parser_from(main)
parser.print_help()

The full sample code would look something like:

import plac

@plac.annotations(
    bootstrap=plac.Annotation("bootstrap", "option", "b", str),
    repo=plac.Annotation("repo to load", "option", "r", str),
    similar=plac.Annotation("term (word|pos) to find similarities, requires 'term'", "flag", "s"),
    mostsimilar=plac.Annotation("similar words to find, requires 'term'", "flag", "m"),
    similarity=plac.Annotation("similarity between two words, requires 'term' and 'term2", "flag", "S"),
    term=plac.Annotation("term (word|pos)", "option", "t", str),
    term2=plac.Annotation("second term (word|pos)", "option", "T", str)
)
def main(bootstrap, repo, similar, mostsimilar, similarity, term, term2):
    :
    if error:
        ###### Print help message on error
        parser = plac.parser_from(main)
        parser.print_help()
        ######
    :
if __name__ == '__main__':
    plac.call(main)
like image 93
Eric Broda Avatar answered Mar 18 '26 02:03

Eric Broda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!