From a9abca1c04b649726f40c0beee906a273e03b2d4 Mon Sep 17 00:00:00 2001 From: Mufanc <1714511787@qq.com> Date: Wed, 1 Jun 2022 11:02:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=20argparse=20?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=B8=AE=E5=8A=A9=E4=BF=A1=E6=81=AF=E7=9A=84?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- README.md | 6 +++--- main.py | 41 +++++++++++++++++++++++++---------------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 74d72ae..0475860 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.cache/* /venv/ /.idea/ -/.vscode/ \ No newline at end of file +/.vscode/ +/**/*.pyc \ No newline at end of file diff --git a/README.md b/README.md index 6544775..7a2e920 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ pip install -r requirements.txt * 列出所有课程和书籍的详细信息 ```shell -py main.py list -d +python main.py list -d ```
@@ -75,7 +75,7 @@ py main.py list -d * 根据书籍 id 执行刷课 ```shell -py main.py flash -i 51627-7B6911511DB6B33638F6C58531D8FBD3 +python main.py flash -i 51627-7B6911511DB6B33638F6C58531D8FBD3 ```
@@ -83,7 +83,7 @@ py main.py flash -i 51627-7B6911511DB6B33638F6C58531D8FBD3 - 根据当前打开的页面执行刷课 ```shell -py main.py flash -c +python main.py flash -c ``` 注意如果打开的是「教材学习」页(如下图),只会刷打开的这一本书籍的任务 diff --git a/main.py b/main.py index 5acb856..62213ef 100644 --- a/main.py +++ b/main.py @@ -2,41 +2,50 @@ import sys import asyncio from loguru import logger from automaton import utils -from argparse import ArgumentParser +import argparse + + +class Help(object): + args = ('-h', '--help') + kwargs = { + 'action': 'help', + 'default': argparse.SUPPRESS, + 'help': '显示本帮助并退出' + } async def main(): - parser = ArgumentParser('main.py') + parser = argparse.ArgumentParser('main.py', add_help=False) + parser.add_argument(*Help.args, **Help.kwargs) - # parser.add_argument('-v', dest='level', action='count', help='日志过滤等级,依次为 warning, info, debug, success') - subparsers = parser.add_subparsers(dest='method', help='模式选择') + parser.add_argument('-v', dest='level', action='count', help='日志过滤等级,依次为 WARNING, INFO, DEBUG') + subparsers = parser.add_subparsers(dest='method', help='模式选择', required=True) - method_list = subparsers.add_parser('list', help='列出所有课程和书籍') - method_list.add_argument('-d', '--detail', action='store_true', help='显示详细信息') + mode_list = subparsers.add_parser('list', add_help=False, help='列出所有课程和书籍') + mode_list.add_argument('-d', '--detail', action='store_true', help='显示详细信息') + mode_list.add_argument(*Help.args, **Help.kwargs) - method_flash = subparsers.add_parser('flash', help='对选定的课程执行刷课') - target = method_flash.add_mutually_exclusive_group() - target.add_argument('-i', '--id', help='指定书籍ID') + mode_flash = subparsers.add_parser('flash', add_help=False, help='对选定的课程执行刷课') + target = mode_flash.add_mutually_exclusive_group(required=True) + target.add_argument('-i', '--id', help='指定书籍 ID') target.add_argument('-c', '--current', action='store_true', help='限定为当前课程或书籍') - target.add_argument('-a', '--all', action='store_true', help='刷全部课程(慎用 除非你知道自己在进行什么操作)') - # method_flash.add_argument('-f', '--filter', help='任务过滤器,设置后只刷匹配的任务(尚未实现)') # Todo: 实现这个 + target.add_argument('-a', '--all', action='store_true', help='刷全部课程(慎用 除非你知道自己在做什么)') + mode_flash.add_argument(*Help.args, **Help.kwargs) args = parser.parse_args() - # logger.remove() - # logger.add(sys.stdout, level=['WARNING', 'INFO', 'DEBUG'][args.level or 0]) + logger.remove() + logger.add(sys.stdout, level=['WARNING', 'INFO', 'DEBUG'][args.level or 0]) if args.method == 'list': await utils.list_books(detail=args.detail) - elif args.method == 'flash': + else: if args.id: await utils.flash_by_id(args.id) elif args.current: await utils.flash_current() elif args.all: await utils.flash_all() - else: - parser.print_help() if __name__ == '__main__': loop = asyncio.new_event_loop()