使用 argparse Python3 的任务自动化

Automation of tasks with argparse Python3

您好,有人可以提供帮助。我正在学习使用 argparse,我想使用命令调用 school.py 作为学校开始的例子。到目前为止,我有这个但正在努力处理这些争论。我这样做是对的还是我做错了什么?

if __name__ == '__main__':


parser = argparse.ArgumentParser(description="This allows quick opening of applications used within the school day")

parser.add_argument("start", help="This will open all the standard applications used within the school day.")
parser.add_argument("engine", help="This will show the Engineering folder within Documents")
parser.add_argument("bus", help="This will show the Business folder within Documents")
parser.add_argument("cs", help="This will show the Computer Science folder within Documents")
parser.add_argument("python", help="This will open the PyCharm application")

args = parser.parse_args()

try:
    if len(sys.argv) > 1:
        if sys.argv[1] == "engine":
            engineering()
        elif sys.argv[1] == "cs":
            computer_science()
        elif sys.argv[1] == "python":
            python()
        elif sys.argv[1] == "bus":
            business()
        elif sys.argv[1] == "start":
            std_day()
except:
    print("An error has occurred")

我的错误是

usage: autoSchoolDay.py [-h] start engine bus cs python

autoSchoolDay.py:错误:需要以下参数:引擎、总线、cs、python

parser = argparse.ArgumentParser(description="This allows quick opening of applications used within the school day")

parser.add_argument('command', choices=['start', 'engine', 'bus', 'cs', 'python'])

args = parser.parse_args()

try:
    if args.command:
        if args.command == "engine":
            engineering()
        elif args.command == "cs":
            computer_science()
        elif args.command == "python":
            python()
        elif args.command == "bus":
            business()
        elif args.command == "start":
            std_day()
except Exception as e:
    print("An error has occurred", e)