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())
Answer from Stephen Rauch on Stack OverflowJust 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())
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.
I tried with Python 3.7 and Click 7 the following code:
import click
@click.command()
@click.option('-w', '--width', type=int, default=0)
@click.option('--option2')
@click.argument('argument')
def app(width, option2, argument):
click.echo("params: {} {} {}".format(width, option2, argument))
assert width == 3
assert option2 == '4'
assert argument == 'arg'
app(["arg", "--option2", "4", "-w", 3], standalone_mode=False)
app(["arg", "-w", 3, "--option2", "4" ], standalone_mode=False)
app(["-w", 3, "--option2", "4", "arg"], standalone_mode=False)
All the app calls are working fine!
This use-case is described in the docs.
Sometimes, it might be interesting to invoke one command from another command. This is a pattern that is generally discouraged with Click, but possible nonetheless. For this, you can use the Context.invoke() or Context.forward() methods.
cli = click.Group()
@cli.command()
@click.option('--count', default=1)
def test(count):
click.echo('Count: %d' % count)
@cli.command()
@click.option('--count', default=1)
@click.pass_context
def dist(ctx, count):
ctx.forward(test)
ctx.invoke(test, count=42)
They work similarly, but the difference is that Context.invoke() merely invokes another command with the arguments you provide as a caller, whereas Context.forward() fills in the arguments from the current command.