欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python編程編寫完善的命令行工具

 更新時間:2021年09月14日 14:56:45   作者:somenzz  
提到編寫命令行工具,你可能會想到用 sys.argv 或者標準庫 argparse,沒錯,這兩個很常用,不過情況復雜時沒有那么方便和高效,因此今天分享兩個工具,讓你編寫命令行工具不費吹灰之力

1. python-fire

python-fire 是一個三方庫,可以將任何 Python 對象變成一個命令行接口。

使用前先 pip install fire 下。

可以把你的函數(shù)直接變成命令行接口:

import fire
def hello(name="World"):
  return "Hello %s!" % name
if __name__ == '__main__':
  fire.Fire(hello)
 

然后在命令行,就可以執(zhí)行這些命令:

python hello.py  # Hello World!
python hello.py --name=David  # Hello David!
python hello.py --help  # Shows usage information.

也可以把可以把你的類直接變成命令行接口:

import fire
class Calculator(object):
  """A simple calculator class."""
  def double(self, number):
    return 2 * number
if __name__ == '__main__':
  fire.Fire(Calculator)

然后就可以這樣執(zhí)行:

python calculator.py double 10  # 20
python calculator.py double --number=15  # 30

除此之外,還有這樣的功能:

執(zhí)行后自動進入交互模式:

command -- --interactive

比如:

查看執(zhí)行的調(diào)用順序:

python arg_demo2.py double 10 -- --trace

結(jié)果如下:

還可以為你生成 shell 自動補全命令的腳本,真的很貼心:

python arg_demo2.py double 10 -- --completion

2. mando

mando 是一個基于 argparse 的裝飾器,可以讓你在幾秒內(nèi)編寫出一個靈活、可維護的命令行工具。

使用前先 pip install mando 下。

用法:

example.py

from mando import command, main
@command
def echo(text, capitalize=False):
    '''Echo the given text.'''
    if capitalize:
        text = text.upper()
    print(text)
if __name__ == '__main__':
    main()
 

命令行用法:

$ python example.py -h
usage: example.py [-h] {echo} ...
positional arguments:
  {echo}
    echo      Echo the given text.
optional arguments:
  -h, --help  show this help message and exit
$ python example.py echo -h
usage: example.py echo [-h] [--capitalize] text
Echo the given text.
positional arguments:
  text
optional arguments:
  -h, --help    show this help message and exit
  --capitalize
 

真實執(zhí)行結(jié)果:

$ python example.py echo spam
spam
$ python example.py echo --capitalize spam
SPAM

再復雜一點的:

from mando import command, main
@command
def push(repository, all=False, dry_run=False, force=False, thin=False):
    '''Update remote refs along with associated objects.
    :param repository: Repository to push to.
    :param --all: Push all refs.
    :param -n, --dry-run: Dry run.
    :param -f, --force: Force updates.
    :param --thin: Use thin pack.'''
     print ('Pushing to {0}. All: {1}, dry run: {2}, force: {3}, thin: {4}'
           .format(repository, all, dry_run, force, thin))
if __name__ == '__main__':
    main()
  

mando 可以理解 Sphinx 風格的文檔字符串中的 :param 參數(shù)說明,因此可以顯示幫助文檔。

$ python git.py push -h
usage: git.py push [-h] [--all] [-n] [-f] [--thin] repository
Update remote refs along with associated objects. 
positional arguments:
  repository     Repository to push to.
optional arguments:
  -h, --help     show this help message and exit
  --all          Push all refs.
  -n, --dry-run  Dry run.
  -f, --force    Force updates.
  --thin         Use thin pack.
 

mando 還可以理解 Python3 的類型提示,因此傳錯了參數(shù),也會有報錯提示:

from mando import command, main
@command
def duplicate(string, times: int):
    '''Duplicate text.
    :param string: The text to duplicate.
    :param times: How many times to duplicate.'''
    print(string * times)
if __name__ == '__main__':
    main()
 

執(zhí)行:

$ python3 test.py duplicate "test " 5
test test test test test
$ python3 test.py duplicate "test " foo
usage: test.py duplicate [-h] string times
test.py duplicate: error: argument times: invalid int value: 'foo'

最后的話

本文分享編寫建命令行工具的三方庫,使用起來非常簡單,我也是偶然在 GitHub 搜索到的,寫代碼前先在 GitHub 上搜一下真的是一個很好的習慣,以上就是Python編程編寫完善的命令行工具的詳細內(nèi)容,更多關(guān)于Python編寫完善的命令行工具的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 使用numba對Python運算加速的方法

    使用numba對Python運算加速的方法

    今天小編就為大家分享一篇使用numba對Python運算加速的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python中.py文件打包成exe可執(zhí)行文件詳解

    Python中.py文件打包成exe可執(zhí)行文件詳解

    這篇文章主要給大家介紹了在Python中.py文件打包成exe可執(zhí)行文件的相關(guān)資料,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • python OpenCV實現(xiàn)答題卡識別判卷

    python OpenCV實現(xiàn)答題卡識別判卷

    這篇文章主要為大家詳細介紹了python OpenCV實現(xiàn)答題卡識別判卷,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Python 裝飾器深入理解

    Python 裝飾器深入理解

    這篇文章主要介紹了Python 裝飾器深入理解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Python selenium自動化測試模型圖解

    Python selenium自動化測試模型圖解

    這篇文章主要介紹了Python selenium自動化測試模型圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Python實現(xiàn)二叉搜索樹

    Python實現(xiàn)二叉搜索樹

    二叉搜索樹(二叉排序樹)它的每個節(jié)點的數(shù)據(jù)結(jié)構(gòu)為1個父節(jié)點指針,1個左孩子指針,1個有孩子指針,還有就是自己的數(shù)據(jù)部分了,因為只有左右兩孩子,所以才叫二叉樹,在此基礎(chǔ)上,該二叉樹還滿足另外一個條件:每個結(jié)點的左孩子都不大于該結(jié)點&&每個結(jié)點的右孩子都大于該結(jié)點.
    2016-02-02
  • Pycharm中切換pytorch的環(huán)境和配置的教程詳解

    Pycharm中切換pytorch的環(huán)境和配置的教程詳解

    這篇文章主要介紹了Pycharm中切換pytorch的環(huán)境和配置,本文給大家介紹的非常詳細,對大家的工作或?qū)W習具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • python根據(jù)距離和時長計算配速示例

    python根據(jù)距離和時長計算配速示例

    這篇文章主要介紹了python根據(jù)距離和時長計算配速示例,需要的朋友可以參考下
    2014-02-02
  • Python中str is not callable問題詳解及解決辦法

    Python中str is not callable問題詳解及解決辦法

    這篇文章主要介紹了Python中str is not callable問題詳解及解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • python項目下生成requirements.txt方法

    python項目下生成requirements.txt方法

    這篇文章主要介紹了python項目下生成requirements.txt的方法,很多小伙伴不知道如何生成requirements.txt,本文就通過代碼示例給大家詳細介紹如何生成,,需要的朋友可以參考下
    2023-06-06

最新評論