У меня есть простая программа командной строки, созданная с использованием "pla c", анализатора командной строки fantasti c Python, и я пытаюсь программно распечатать использование по умолчанию (также известное как "help" output) для конкретной команды.
Я не смог найти ничего в документации "pla c". Любая помощь приветствуется.
Ниже приведен пример программы:
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)
При ошибке командной строки я хотел бы использовать код («ПЕЧАТЬ ОШИБКИ ЗДЕСЬ» в приведенном выше примере), чтобы показать использование идентично тому, как когда я запускаю команду справки ("python myCLI.py -h") напрямую:
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)