对于打字机来说,这是一个相当新的尝试,并试图让一个简单的CLI应用程序在我的终端上自动完成[TAB],但没有成功。下面是我的代码结构和代码本身:
vinicius.ferreira@FVFFPG4VQ6L4 dev-env % tree
.
├── README.md
├── dev_env
│ ├── __init__.py
│ ├── main.py
│ ├── services.py
│ ├── team_product1.py
│ └── team_product2.py
├── dist
│ ├── dev_env-0.1.2-py3-none-any.whl
│ └── dev_env-0.1.2.tar.gz
├── poetry.lock
├── pyproject.toml
└── tests
└── __init__.py
3 directories, 11 files
##### main.py #####
import typer
from . import team_product1
from . import team_product2
from . import services
app = typer.Typer()
app.add_typer(team_product1.app, name="team_product1")
app.add_typer(team_product2.app, name="team_product2")
app.add_typer(services.app, name="services")
if __name__ == "__main__":
app()
##### team_product1.py #####
import typer
app = typer.Typer()
@app.command()
def start():
print(f"Starting all services for Team Product 1 ...")
@app.command()
def stop():
print(f"Stopping all services for Team Product 1 ...")
@app.command()
def destroy():
print(f"Destroying all services for Team Product 1 ...")
if __name__ == "__main__":
app()
##### team_product2.py #####
import typer
app = typer.Typer()
@app.command()
def start():
print(f"Starting all services for Team Product 2 ...")
@app.command()
def stop():
print(f"Stopping all services for Team Product 2 ...")
@app.command()
def destroy():
print(f"Destroying all services for Team Product 2 ...")
if __name__ == "__main__":
app()
##### services.py #####
import typer
app = typer.Typer()
@app.command()
def start(name: str):
print(f"Starting service: {name}")
@app.command()
def stop(name: str):
print(f"Stopping service: {name}")
@app.command()
def destroy(name: str):
print(f"Destroying service: {name}")
if __name__ == "__main__":
app()
我能够使用poetry build
命令将它构建到一个.whl文件中,然后从项目根目录中为所有使用pip install dist/dev_env-0.1.2-py3-none-any.whl
的用户安装该包。
CLI应用程序运行正常,除了[TAB]自动完成,并且已经执行了dev-env --install-completion
并重新启动了我的终端。它提到“zsh completion installed in /Users/vinicius.ferreira/.zfunc/_dev-env”,文件内容如下
#compdef dev-env
_dev_env_completion() {
eval $(env _TYPER_COMPLETE_ARGS="${words[1,$CURRENT]}" _DEV_ENV_COMPLETE=complete_zsh dev-env)
}
compdef _dev_env_completion dev-env%
有谁能告诉我我错过了什么?
不知道它是否有帮助,但我的~/.zshrc
文件最后有这个:
zstyle ':completion:*' menu select
fpath+=~/.zfunc
我还注意到打字机教程显示完成应该安装在/home/user/.zshrc.
上。不知道为什么我的MacOS会安装在不同的地方。
1条答案
按热度按时间k5ifujac1#
我使用如下命令为typer项目获取zsh完成:
您可以将其放在.zshrc中,使其在所有shell中都可用(但需要安装dev-env