前言
我們的游戲資源處理工具是Python實(shí)現(xiàn)的,功能包括csv解析,UI材質(zhì)處理,動(dòng)畫資源解析、批處理,Androd&iOS自動(dòng)打包等功能。該項(xiàng)目是由其他部門繼承過來的,由于絕大部分代碼不符合我們的業(yè)務(wù)需求,所以進(jìn)行了大重構(gòu)。刪除了所有業(yè)務(wù)代碼,僅保留了python代碼框架。項(xiàng)目中命令行參數(shù)解析是自己實(shí)現(xiàn)的,極其不優(yōu)雅,也忍了這么久。打算找時(shí)間用click重寫。所以最近學(xué)習(xí)了click,下面本文的內(nèi)容是click的入門教程,初學(xué)者們可以來一起學(xué)習(xí)學(xué)習(xí)。
官網(wǎng)鏡像地址: http://click.uoota.com/6/
支持:
- 命令的任意嵌套
- 自動(dòng)生成幫助信息
- 支持在運(yùn)行時(shí)子命令的延遲加載
安裝方法是使用 pip:
1
|
pip install click |
下面一小段代碼是其官方主頁的例子,貼出來下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import click @click .command() @click .option( '--count' , default = 1 , help = 'Number of greetings.' ) @click .option( '--name' , prompt = 'Your name' , help = 'The person to greet.' ) def hello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for x in range (count): click.echo( 'Hello %s!' % name) if __name__ = = '__main__' : hello() |
運(yùn)行:
1
2
3
4
5
|
$ python hello.py - - count = 3 Your name: John Hello John! Hello John! Hello John! |
查看幫助信息:
1
2
3
4
5
6
7
8
9
|
$ python hello.py - - help Usage: hello.py [OPTIONS] Simple program that greets NAME for a total of COUNT times. Options: - - count INTEGER Number of greetings. - - name TEXT The person to greet. - - help Show this message and exit. |
總結(jié)
以上就是介紹Python中命令行工具click的安裝與使用的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流。