轻松创建Python CLI应用程序!
项目描述
mando是围绕argparse的包装器,并允许您在几秒钟内编写完整的CLI应用程序,同时保持所有灵活性。
安装
$ pip install mando
问题
虽然argparse非常适合只有一个默认命令的简单命令行应用程序,但当您需要添加多个命令并管理它们时,事情会变得非常混乱和冗长。但别担心,mando来帮忙!
快速入门
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-style :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.
让我们试试吧!
$ python git.py push --all myrepo
Pushing to myrepo. All: True, dry run: False, force: False, thin: False
$ python git.py push --all -f myrepo
Pushing to myrepo. All: True, dry run: False, force: True, thin: False
$ python git.py push --all -fn myrepo
Pushing to myrepo. All: True, dry run: True, force: True, thin: False
$ python git.py push --thin -fn myrepo
Pushing to myrepo. All: False, dry run: True, force: True, thin: True
$ python git.py push --thin
usage: git.py push [-h] [--all] [-n] [-f] [--thin] repository
git.py push: error: too few arguments
惊讶吗?是的,mando 从文档字符串中获得了简短选项和帮助!您可以在文档字符串中放入更多内容,如果还不够,还有一个 @arg 装饰器来自定义传递给 argparse 的参数。
类型注解
mando 识别 Python 3 风格的类型注解,并在给命令传递的参数类型不正确时警告用户。
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'
Mando 有许多其他选项。例如,它支持不同的文档字符串风格(Sphinx、Google 和 NumPy),支持通过 argcomplete 包进行 shell 自动补全,并支持自定义格式类。有关完整文档,请访问 https://mando.readthedocs.org/。
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分发
mando-0.7.1.tar.gz (37.9 kB 查看散列)
构建分发
mando-0.7.1-py2.py3-none-any.whl (28.1 kB 查看散列)
关闭
mando-0.7.1.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 18baa999b4b613faefb00eac4efadcf14f510b59b924b66e08289aa1de8c3500 |
|
MD5 | b45233627e6eb72893993a7254737acc |
|
BLAKE2b-256 | 3524cd70d5ae6d35962be752feccb7dca80b5e0c2d450e995b16abd6275f3296 |
关闭
mando-0.7.1-py2.py3-none-any.whl 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 26ef1d70928b6057ee3ca12583d73c63e05c49de8972d620c278a7b206581a8a |
|
MD5 | b7758958ce20b31e5bb80e16d7e7b83a |
|
BLAKE2b-256 | d2f0834e479e47e499b6478e807fb57b31cc2db696c4db30557bb6f5aea4a90b |