在前面三篇介紹 argparse
的文章中,咱們全面瞭解了 argparse
的能力,相信很多小夥伴們都已經摩拳擦掌,想要打造一個屬於本身的命令行工具。html
本文將以咱們平常工做中最多見的 git
命令爲例,講解如何使用 argparse
庫來實現一個真正可用的命令行程序。python
本系列文章默認使用 Python 3 做爲解釋器進行講解。
若你仍在使用 Python 2,請注意二者之間語法和庫的使用差別哦~
複製代碼
你們不妨回憶一下,平時最常使用 git
子命令都有哪些?git
當你寫好一段代碼或增刪一些文件後,會用以下命令查看文件狀態:github
git status
複製代碼
確認文件狀態後,會用以下命令將的一個或多個文件(夾)添加到暫存區:編程
git add [pathspec [pathspec ...]]
複製代碼
而後使用以下命令提交信息:bash
git commit -m "your commit message"
複製代碼
最後使用以下命令將提交推送到遠程倉庫:函數
git push
複製代碼
咱們將使用 argparse
和 gitpython
庫來實現這 4 個子命令。工具
gitpython 是一個和 git
倉庫交互的 Python 第三方庫。 咱們將借用它的能力來實現真正的 git
邏輯。ui
安裝:this
pip install gitpython
複製代碼
在實現前,咱們不妨先思考下會用到 argparse
的哪些功能?整個程序的結構是怎樣的?
argparse
嵌套解析器
必不可少set_defaults
功能git add [pathspec [pathspec ...]]
,咱們須要實現位置參數,並且數量是任意個git commit --message msg
或 git commit -m msg
,咱們須要實現選項參數,且便可長選項,又可短選項程序結構
cli
函數來做爲統一的入口,它負責構建解析器,並解析命令行參數handle_xxx
函數響應對應的子命令則基本結構以下:
import os
import argparse
from git.cmd import Git
def cli():
""" git 命名程序入口 """
pass
def handle_status(git, args):
""" 處理 status 命令 """
pass
def handle_add(git, args):
""" 處理 add 命令 """
pass
def handle_commit(git, args):
""" 處理 -m <msg> 命令 """
pass
def handle_push(git, args):
""" 處理 push 命令 """
pass
if __name__ == '__main__':
cli()
複製代碼
下面咱們將一步步地實現咱們的 git
程序。
假定咱們在 argparse-git.py 文件中實現咱們的 git
程序。
咱們須要構建一個父解析器,做爲程序的根解析器,程序名稱指定爲 git
。而後在上面添加子解析器,爲後續的子命令的解析作準備:
def cli():
""" git 命名程序入口 """
parser = argparse.ArgumentParser(prog='git')
subparsers = parser.add_subparsers(
title='These are common Git commands used in various situations',
metavar='command')
複製代碼
add_subparsers
中的 title
和 metavar
參數主要用於命令行幫助信息,最終的效果以下:
usage: git [-h] command ...
optional arguments:
-h, --help show this help message and exit
These are common Git commands used in various situations:
command
...
複製代碼
咱們須要在 cli
函數中添加一個用於解析 status
命令的子解析器 status_parser
,並指定其對應的處理函數爲 handle_status
。
def cli():
...
# status
status_parser = subparsers.add_parser(
'status',
help='Show the working tree status')
status_parser.set_defaults(handle=handle_status)
複製代碼
須要說明的是,在 status_parser.set_defaults
函數中,能接收任意名稱的關鍵字參數,這個參數值會存放於父解析器解析命令行參數後的變量中。
好比,在本文示例程序中,咱們爲每一個子解析器定義了 handle
,那麼 args = parser.parse_args()
中的 args
將具備 handle
屬性,咱們傳入不一樣的子命令,那麼這個 handle
就是不一樣的響應函數。
定義了 status
的子解析器後,咱們再實現下 handle_status
便可實現 status
命令的響應:
def handle_status(git, args):
""" 處理 status 命令 """
cmd = ['git', 'status']
output = git.execute(cmd)
print(output)
複製代碼
不難看出,咱們最後調用了真正的 git status
來實現,並打印了輸出。
你可能會對 handle_status
的函數簽名感到困惑,這裏的 git
和 args
是怎麼傳入的呢?這實際上是由咱們本身控制的,將在本文最後講解。
一樣,咱們須要在 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)
複製代碼
一樣,咱們須要在 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)
複製代碼
一樣,咱們須要在 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
對象,用以處理對應命令至此,咱們就實現了一個簡單的 git
命令行,使用 python argparse-git.py -h
查看幫助以下:
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 。
本文簡單介紹了平常工做中經常使用的 git
命令,而後提出實現它的思路,最終一步步地使用 argparse
和 gitpython
實現了 git
程序。是否是頗有成就感呢?
關於 argparse
的講解將告一段落,回顧下 argparse
的四步曲,加上今天的內容,感受它仍是挺清晰、簡單的。 不過,這還只是打開了命令行大門的一扇門。
你是否想過,argparse
的四步曲雖然理解簡單,但略微麻煩。有沒有更簡單的方式? 若是我很熟悉命令行幫助語法,我能不能寫個幫助字符串就把全部的命令行元信息給定義出來?而後就直接輕鬆愉快地獲取解析後的參數信息呢?
在下篇文章中,將爲你們講解另外一個站在一個全新的思路,又無比強大的庫 docopt
。
『講解開源項目系列』 啓動——讓對開源項目感興趣的人再也不畏懼、讓開源項目的發起者再也不孤單。跟着咱們的文章,你會發現編程的樂趣、使用和發現參與開源項目如此簡單。歡迎聯繫咱們給咱們投稿,讓更多人愛上開源、貢獻開源~