Using Click Commands in Python

爱⌒轻易说出口 提交于 2021-02-11 13:42:06

问题


I have a Python program and I want to run it using command:

myprogram --readpower=some argument

Code

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()

The problem I am facing is that the command -

myprogram --readpower = some argument

does not work. I have to write the command as :

myprogram read_csv --readpower = some argument

回答1:


Just get rid of the group since you don't want it. Use a command directly like:

Code:

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))

Test Code:

if __name__ == "__main__":
    cli('--readpower=3'.split())
    cli('--version'.split())



回答2:


Click is actually doing what it is supposed to do. You created a group cli and added the single command read_csv. So click needs to know the command to know what to invoke because there could possibly be more and even nested commands. You could create a standalone click command read_csvand register it in your setup.py file like so:

# 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))

And the setup:

# 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
    ''',
)

So you can set multiple entry points to your application and mix multiple standalone commands with grouped (and even possibly nested) commands.



来源:https://stackoverflow.com/questions/49870868/using-click-commands-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!