I have some script on python and argparse, one of optional arguments adds transliteration:
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--text',
action='store_true',
help='display a text')
parser.add_argument('-s', '--search',
dest='string',
action='store',
type=str,
help='search in a text')
parser.add_argument('--translit',
action='store_true',
help='transliterate output; usage: prog [-t | -d STRING] --translit')
results = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
elif results.text and results.translit::
translit(display_text())
elif results.text and results.translit::
display_text()
elif results.string and results.translit:
translit(search(results.string))
elif results.string:
search(results.string)
Output:
usage: prog [-h] [-t] [-s STRING] [--translit]
optional arguments:
-h, --help show this help message and exit
-t, --text display a text
-s STRING, --search STRING search in a text
--translit transliterate output; usage: prog
[-t | -s STRING] --translit
There is no output, when I run prog --translit. I need the string looking so:
usage: prog [-h] [-t] [-s STRING] [[-t | -s STRING] --translit]
When I run prog --translit, the string of output should be:
prog: error: argument --translit: usage: [[-t | -s STRING] --translit]
How can I do this?
--translitshould be an option recognized bytextandsearchsubcommands.parser.usageto what every string you want. That can be done when you create the parser, or anytime before you show the help or error message.