Home > other >  How to call a function wrapped inside click from another code
How to call a function wrapped inside click from another code

Time:08-26

I have a python function which would like to call another function wrapped inside click.

So something like this:

@click.command()
@click.argument('expected', type=click.Path('r'))       
@click.argument('observed', type=click.Path('r')) 
@click.option('-o1', '--option1', default=None)
@click.option('--option2', type=click.INT, default=0)
main_compare(expected, observed, option1, option2):
 ......
 ...

and I am calling it inside another function (in another .py file) like this:

def callingFunction():
  main_compare(["expected",input_data_file, "observed", output_file, "--option1", option1, "--option2", option2])

Is this is the right way to call it? I am getting some errors. Can someone confirm the right way to call such a function wrapped inside click?

Thanks

CodePudding user response:

You can extract the function into a new one, call it from the click wrapper and the other code.

For instance:

def _main_compare(expected, observed, option1, option2):
    # your logic here
    ...


@click.command()
@click.argument('expected', type=click.Path('r'))       
@click.argument('observed', type=click.Path('r')) 
@click.option('-o1', '--option1', default=None)
@click.option('--option2', type=click.INT, default=0)
def main_compare(expected, observed, option1, option2):
   _main_compare(expected, observed, option1, option)


Other file:

from file import _main_compare

def callingFunction():
  _main_compare(*args)

CodePudding user response:

Thanks a lot for your suggestion. I should have mentioned that I cannot change the code which has main_compare at the moment. So I was wondering if there is a way out to call this directly...

  • Related