前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python编写完善的命令行工具,只需要一行代码

Python编写完善的命令行工具,只需要一行代码

作者头像
somenzz
发布2021-07-01 16:40:34
9430
发布2021-07-01 16:40:34
举报
文章被收录于专栏:Python七号Python七号

提到编写命令行工具,你可能会想到用 sys.argv 或者标准库 argparse,没错,这两个很常用,不过情况复杂时没有那么方便和高效,因此今天分享两个工具,让你编写命令行工具不费吹灰之力。

1. python-fire

python-fire 是一个三方库,可以将任何 Python 对象变成一个命令行接口。

使用前先 pip install fire 下。

可以把你的函数直接变成命令行接口:

import fire

def hello(name="World"):
  return "Hello %s!" % name

if __name__ == '__main__':
  fire.Fire(hello)

然后在命令行,就可以执行这些命令:

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)

然后就可以这样执行:

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

除此之外,还有这样的功能:

执行后自动进入交互模式:

command -- --interactive

比如:

查看执行的调用顺序:

python arg_demo2.py double 10 -- --trace

结果如下:

还可以为你生成 shell 自动补全命令的脚本,真的很贴心:

python arg_demo2.py double 10 -- --completion

2. mando

mando 是一个基于 argparse 的装饰器,可以让你在几秒内编写出一个灵活、可维护的命令行工具。

使用前先 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

真实执行结果:

$ 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 参数说明,因此可以显示帮助文档。

$ 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 的类型提示,因此传错了参数,也会有报错提示:

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()

执行:

$ 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七号」,每周学习一个小技术。

留言讨论

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-06-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python七号 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. python-fire
  • 2. mando
  • 最后的话
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档