Argsparse-код фактически никогда не выполняется. Выполняя скрипт из командной строки, вы вызываете main()
, который просто печатает и завершает работу. Вы должны вызвать parse_args()
в функции main()
, чтобы это работало.
import argparse
# Personally, I think these belong in the main()
# function as well, but they don't need to be.
parser = argparse.ArgumentParser(
description="I wish this description would output",
epilog="Please out the epilog"
)
parser.add_argument(
"-l",
type=str,
default="info",
help="logging level. Default is info. Use debug if you have problems."
)
def main():
args = parser.parse_args() # Parses arguments
print("goodbye")
if __name__ == "__main__":
main() # Calls main
Производит:
~/Desktop $ python untitled.py --help
usage: untitled.py [-h] [-l L]
I wish this description would output
optional arguments:
-h, --help show this help message and exit
-l L logging level. Default is info. Use debug if you have problems.
Please out the epilog
jcollado утверждает, что ваш код работал в Ubuntu нормально - мне это очень любопытно.