我有一个Python程序,我想使用命令运行它:
myprogram --readpower=some argument
import click
import csv
import json
import sys
@click.group(invoke_without_command=True)
@click.option('--version', is_flag=True, default=False, help='Prints out pyisg package version being used')
def cli(version):
"""
This utility is used to convert a CSV file to JSON file
"""
if version:
print("This is version 1.0 software")
sys.exit()
@cli.command()
@click.option('--readpower', type=str, default="",
help='Path of the csv file which is to be read)')
def read_csv(readpower,readspike,readdip):
{
if readpower:
print("reading power")
}
if __name__ == "__main__":
cli()
我面临的问题是命令 -
myprogram --readpower = some argument
不起作用。我必须将命令编写为:
myprogram read_csv --readpower = some argument
答案 0 :(得分:1)
因为你不想要它,所以摆脱这个群体。直接使用命令:
import click
import sys
@click.command()
@click.option('--version', is_flag=True, default=False,
help='Prints out pyisg package version being used')
@click.option('--readpower', type=str, default="",
help='Path of the csv file which is to be read)')
def cli(version, readpower):
"""
This utility is used to convert a CSV file to JSON file
"""
if version:
click.echo("This is version 1.0 software")
sys.exit()
if readpower != '':
click.echo("reading power {}".format(readpower))
if __name__ == "__main__":
cli('--readpower=3'.split())
cli('--version'.split())
答案 1 :(得分:0)
Click实际上正在做它应该做的事情。您创建了一个组cli
并添加了单个命令read_csv
。因此,click需要知道命令以了解要调用的内容,因为可能存在更多甚至嵌套的命令。您可以创建一个独立的单击命令read_csv
并将其注册到setup.py
文件中,如下所示:
# app.py
import click
# as a standalone command
@click.command()
@click.option('--readpower', type=str, default="")
def read_csv(readpower):
click.echo("doing stuff...")
if readpower:
click.echo("reading {}...".format(readpower))
@click.group()
def cli():
pass
@cli.command()
def do_stuff():
click.echo("doing stuff...")
# as a subcommand in a group
@cli.command()
@click.option('--readpower', type=str, default="")
def read_csv(readpower):
click.echo("doing something...")
if readpower:
click.echo("reading {}...".format(readpower))
设置:
# setup.py
from setuptools import setup
setup(
name='app',
version='0.1',
py_modules=['app'],
install_requires=['Click'],
entry_points='''
[console_scripts]
read_csv=app:read_csv
cli=app:cli
''',
)
因此,您可以为应用程序设置多个入口点,并将多个独立命令与分组(甚至可能嵌套)命令混合使用。