首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用argparse Python3实现任务自动化

使用argparse Python3实现任务自动化
EN

Stack Overflow用户
提问于 2021-03-02 04:47:13
回答 1查看 38关注 0票数 0

嗨,有谁能帮上忙吗?我正在学习使用argparse,我想使用命令来调用school.py,例如学校开学。到目前为止,我已经掌握了这一点,但正在努力处理这些争论。我是做对了还是做错了什么?

代码语言:javascript
运行
复制
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")

我的错误是

代码语言:javascript
运行
复制
usage: autoSchoolDay.py [-h] start engine bus cs python

autoSchoolDay.py:错误:需要以下参数: engine、bus、cs、python

EN

Stack Overflow用户

回答已采纳

发布于 2021-03-02 04:59:40

代码语言:javascript
运行
复制
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)
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66429397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档