def handle_status(git, args): """ 处理 status 命令 """ cmd = ['git', 'status'] output = git.execute(cmd) print(output)不难看出 , 我们最后调用了真正的 git status 来实现 , 并打印了输出 。
你可能会对 handle_status 的函数签名感到困惑 , 这里的 git 和 args 是怎么传入的呢?这其实是由我们自己控制的 , 将在本文最后讲解 。
add 子命令
同样 , 我们需要在 cli 函数中添加一个用于解析 add 命令的子解析器 add_parser , 并指定其对应的处理函数为 handle_add 。
额外要做的是 , 要在子解析器 add_parser 上添加一个 pathspec 位置参数 , 且其数量是任意的:
def cli(): ... # add add_parser = subparsers.add_parser( 'add', help='Add file contents to the index') add_parser.add_argument( 'pathspec', help='Files to add content from', nargs='*') add_parser.set_defaults(handle=handle_add)然后 , 就是实现 handle_add 函数 , 我们需要用到表示文件路径的 args.pathspec:
def handle_add(git, args): """ 处理 add 命令 """ cmd = ['git', 'add'] + args.pathspec output = git.execute(cmd) print(output)commit 子命令
同样 , 我们需要在 cli 函数中添加一个用于解析 commit 命令的子解析器 commit_parser , 并指定其对应的处理函数为 handle_commit 。
额外要做的是 , 要在子解析器 commit_parser 上添加一个 -m/--message 选项参数 , 且要求必填:
def cli(): ... # commit commit_parser = subparsers.add_parser( 'commit', help='Record changes to the repository') commit_parser.add_argument( '--message', '-m', help='Use the given <msg> as the commit message', metavar='msg', required=True) commit_parser.set_defaults(handle=handle_commit)然后 , 就是实现 handle_commit 函数 , 我们需要用到表示提交信息的 args.message:
def handle_commit(git, args): """ 处理 -m <msg> 命令 """ cmd = ['git', 'commit', '-m', args.message] output = git.execute(cmd) print(output)push 子命令
同样 , 我们需要在 cli 函数中添加一个用于解析 push 命令的子解析器 push_parser , 并指定其对应的处理函数为 handle_push 。
它同 status 子命令的实现方式一致:
def cli(): ... # push push_parser = subparsers.add_parser( 'push', help='Update remote refs along with associated objects') push_parser.set_defaults(handle=handle_push)然后 , 就是实现 handle_push 函数 , 和 handle_status 类似:
def handle_push(git, args): cmd = ['git', 'push'] output = git.execute(cmd) print(output)解析参数
在定义完父子解析器 , 并添加参数后 , 我们就需要对参数做解析 , 这项工作也是实现在 cli 函数中:
def cli(): ... git = Git(os.getcwd()) args = parser.parse_args() if hasattr(args, 'handle'): args.handle(git, args) else: parser.print_help()
- 通过 git.cmd.Git 实例化出 git 对象 , 用来和 git 仓库交互
- 通过 parser.parse_args() 解析命令行
- 通过 hasattr(args, 'handle') 判断是否输入了子命令 。
- 由于每个子解析器都定义了 handle , 那么如果当用户在命令行不输入任何命令时 , args 就没有 handle 属性 , 那么我们就输出帮助信息
- 如果用户输入了子命令 , 那么就调用 args.handle , 传入 git 和 args 对象 , 用以处理对应命令
usage: git [-h] command ...?optional arguments: -h, --help show this help message and exit?These are common Git commands used in various situations: command status Show the working tree status add Add file contents to the index commit Record changes to the repository push Update remote refs along with associated objects然后我们就可以愉快地使用亲手打造的 git 程序啦!
想看整个源码 , 请戳 argparse-git.py[3] 。
小结
本文简单介绍了日常工作中常用的 git 命令 , 然后提出实现它的思路 , 最终一步步地使用 argparse 和 gitpython 实现了 git 程序 。是不是很有成就感呢?
关于 argparse 的讲解将告一段落 , 回顾下 argparse 的四步曲 , 加上今天的内容 , 感觉它还是挺清晰、简单的 。不过 , 这还只是打开了命令行大门的一扇门 。
推荐阅读
- 使用Streamlit从简单的Python脚本创建交互式WebApp
- 猛踩油门!令Python加速
- 使用Python检测虚假新闻
- 学习python你必须弄懂的 Python、Pycharm、Anaconda 三者的关系
- Python 图像处理 | 图像平滑之均值滤波、方框滤波、高斯滤波及中值滤波
- 教你用Python批量下载静态页面图片
- 斗记之巅峰之斗的由来,场关于斗记茶之旅的分享会分享茶会
- 用 Python 高效智能管理文件夹
- Python 100个样例代码
- 用Python从头开始实现简单遗传算法
