Reputation: 9
Hi: I'm new to click and here is a example of how I failed to test my sub-command: for some reasons I need to keep my python environment clean and I created a virtual environment to install click in "venv/lib/"
main.py
import click
@click.group()
def cli():
"""This is group command."""
pass
@click.command()
@click.option('--user', required=True, prompt='your name', help='input your name')
def user(user):
"""This is sub-group command."""
print(f'Do something here.{user}!')
cli.add_command(user)
if __name__ == '__main__':
cli()
I also created a shell script to activate virtual env.
run_tool.sh
# export PYTHONPATH=$PYTHONPATH:'/Volumes/di/Temp/weixintong/wxt_DI_Learn/click_test/venv/lib'
source /Volumes/di/Temp/weixintong/wxt_DI_Learn/click_test/venv/bin/activate
# I tried both activate virtual env and export PYTHONPATH. none of them worked.
cd /Volumes/di/Temp/weixintong/wxt_DI_Learn/click_test
# python3 main.py
then I created an alias in ~/.zshrc in order to create a shortcut to activate my virtual env.
nano ~/.zshrc
alias aa=/Volumes/di/Temp/weixintong/wxt_DI_Learn/click_test/run_tool.sh
source ~/.zshrc
here was the result:
> aa at 11:59:04
Usage: main.py [OPTIONS] COMMAND [ARGS]...
This is group command.
Options:
--help Show this message and exit.
Commands:
user This is sub-group command.
when I tested the sub-command I was stuck in the group-command
> aa user at 11:59:06
Usage: main.py [OPTIONS] COMMAND [ARGS]...
This is group command.
Options:
--help Show this message and exit.
Commands:
user This is sub-group command.
Upvotes: 0
Views: 114
Reputation: 3877
Your bash script isn't passing the "user" argument to the python script.
# Get the first argument after the script name, and use it as command
command="$1"
PYTHONPATH=$PYTHONPATH:'/Volumes/di/Temp/weixintong/wxt_DI_Learn/click_test/venv/lib'
source /Volumes/di/Temp/weixintong/wxt_DI_Learn/click_test/venv/bin/activate
cd /Volumes/di/Temp/weixintong/wxt_DI_Learn/click_test
python3 main.py $command
Upvotes: 0