Skip to content Skip to sidebar Skip to footer

Using Click Commands In Python

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

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

Solution 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.


Post a Comment for "Using Click Commands In Python"