介绍
使用zshell可快速构建命令行应用
安装
pip安装
pip install -i https://pypi.org/project pyzshell
开始
编写第一个zshell应用
创建helloworld.py文件 导入模块
import zshell
创建一个应用
app=zshell.App()
编写命令
@app.shell
def helloworld():
return 'Hello World'
运行应用
app.run()
完整代码
#coding=utf-8
import zshell
app=zshell.App()
@app.shell
def helloworld():
return 'Hello World'
if __name__ == '__main__':
app.run()
运行helloworld.py文件
python helloworld.py
zshell:>>helloworld
Hello World
交互式运行
python 文件.py
python helloworld.py
zshell:>>helloworld
Hello World
非交互式运行
python 文件.py 命令 参数
python helloworld.py helloworld
Hello World
参数
变长参数
新建add函数,函数参数为元组变长参数,参数名必须是args
@app.shell
def add(*args):
num=0
for i in args:
num+=i
return num
输入add 1 2 3调用add命令,将参数值1,2,3相加得出结果为6
zshell:>>add 1 2 3
6
zshell:>>add 1 2 3 4
10
新建add2函数,函数参数为字典变长参数,参数名必须是kwargs
@app.shell
def add2(**kwargs):
return kwargs['a']+kwargs['b']
输入add2 -a 1 -b 1调用add2命令,相加得出结果为2
参数仅支持英文或以-、–开头的英文,-、–开头的参数zshell会自动去掉-、–符号
zshell:>>add2 -a 1 -b 1
2
zshell:>>add2 -a 1 -b 1 -c 1
2
非变长参数
新建add3函数,函数参数随意
@app.shell
def add3(a=0,b=0,c=0):
return a+b+c
输入add3 1 2 3调用add3命令,相加得出结果为6
zshell:>>add3 1 2 3
6
zshell:>>add3 1 2 3 4
6
zshell:>>add3 1
1
@app.shell详解
将函数添加到zshell命令
参数 |
说明 |
类型 |
默认 |
|---|---|---|---|
name |
命令名 使用|可分隔多个命令名 |
string |
函数名 |
desc |
命令描述 |
string |
“” |
args |
命令参数 |
list |
[] |
版本
1.1.4 新增内置命令
clear 清除控制台
修复已知bug
Metadata
Release files for pyzshell 1.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pyzshell-1.1.4.tar.gz | 4.4 kB | Without provenance | Details |
Release files / pyzshell-1.1.4.tar.gz
| Download URL | pyzshell-1.1.4.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
71b9c0acd0260034096c41d1b8a819ba15b8a36e3b72c4c77e3fd64c56a552e6
|
|
BLAKE2b-256 checksum How to use checksums |
78d64046b0f98a343f8929c8c49d5b3ec930d9bc98c9556b7fac8226b7e60c53
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.4.2 requests/2.22.0 setuptools/40.2.0 requests-toolbelt/0.9.1 tqdm/4.26.0 CPython/3.7.0
|
Security for pyzshell 1.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.