'How can we create different Options Using Click in Python for calling multiple functions with same command
Here is the sample code for creating click options and args for running this.
'''
import click
class Test:
def test1(start_date, end_date):
s = f"hello test1 data for start_date and end_date"
return s
def test2():
t = f"hello test2 data for start_date and end_date"
return t
@click.group()
def run():
pass
@run.command("metrics")
@click.option("--month",
"-m",
required=True,
is_flag=True,
help="Metrics data to be extracted on month basis")
@click.argument("start_date", type=str, required=True)
@click.argument("end_date", type=str, required=True)
def get_metrics_per_month(start_date, end_date):
test1(start_date, end_date)
if __name__ == '__main__':
run(obj={})
'''
So I have created the option -m or --month for calling the function with the same command but with different options so in order to call another function test2 i need to call it same way and put different option ?
@run.command("metrics")
@click.option("--team",
"-t",
required=True,
is_flag=True,
help="Metrics data to be extracted on team basis")
@click.argument("start_date", type=str, required=True).
@click.argument("end_date", type=str, required=True)
def get_metrics_per_team(start_date, end_date):
test2(start_date, end_date)
Also here is the setup.py file for installing the requirements so that we can use run as run command.
from setuptools import setup
setup(
name="cli",
version='1.0',
include_package_data=True,
install_requires=[
'Click',
],
entry_points='''
[console_scripts]
run=cli:run
''',
)
After I have written the second function I am not seeing the first option --month it says the error:-
Error: No such option: -m
Also, second -t option is being displayed but when I am giving the arguments for start_date and end_date it is reporting error:-
Usage: run metrics [OPTIONS] START_DATE END_DATE
Try 'run metrics --help' for help.
Error: Missing argument 'END_DATE'.
Any suggestions how can have multiple options to same command?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
