I had to experiment some to get this right. The main Python file imports the individual files with the commands and then calls the add_command() method to register the commands. In each individual command, the 'click' package is imported and then the command definition decorator (and the pass-context-object decorator) is added to the function call.
One difficulty I ran into was having a function act as both a CLI command and as a function that can be called by other modules. The function calls need to be distinct: defining the CLI command (which is basically a wrapper for the function that does the work) and then the function itself. This same file also has an example of the "Click Group" setup, which is then what is added to the top-level Click structure.
Hope this helps.
Peter
On Jul 29, 2021, 4:42 PM -0400, Eric Lease Morgan <[log in to unmask]>, wrote:
> On Jul 29, 2021, at 12:46 PM, Eric Lease Morgan <[log in to unmask]> wrote:
>
> > So far, Click has been working for me: https://click.palletsprojects.com/en/8.0.x/
>
>
> My Click script is coming along quite nicely, but now I have a Python noobie question. My script is getting longer and longer with various definitions looking something like this:
>
> @click.command()
> def config() :
> # do cool stuff here
>
> @click.command()
> @click.argument( 'location' )
> def list( location ) :
> # do more cool stuff
>
> @click.command()
> @click.argument( 'carrel' )
> def harvest( carrel ) :
> # do even more cool stuff
>
> What is the technique I should use if I want to put each of these little functions into individual files? In the end, I'd like to have a whole set of little files where each one does a specific operation. I suppose I could create file full of functions, but then that file would be very long.
>
> --
> Eric "Sort Of Embarrassed" Morgan
|