嗨,有谁能帮上忙吗?我正在学习使用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:错误:需要以下参数: engine、bus、cs、python
发布于 2021-03-02 04:59:40
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)
https://stackoverflow.com/questions/66429397
复制相似问题