Skip to content

[QUESTION] How can I split my commands into different "command.py" files? #306

@GrGohan

Description

@GrGohan

First check

  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already searched in Google "How to X in Click" and didn't find any information.

Description

I am just getting started with the library Typer for building CLI's. I apologize in advance if this issue has been raised more than once, but I could not find it on Google to help me with the correct implementation.
There are too many commands in my cli.py and I want to moved them to a separate folder “commands”, but I cannot figure out how to implement a dynamic loader for these commands in cli.py.

my_app/
 __init__.py
 commands/
       __init__.py
       command1.py
       command2.py
 cli.py

I want start by creating a class "Command" for each command with a single function "handle (**options)" that contains the functionality. But I am not sure if I am forming the class correctly. I want to make a class "AsyncCommand" because the command can be complex and can be properly decomposed into methods and encapsulated within one class

# a_test_command.py
from typer.core import TyperCommand
class AsyncCommand(TyperCommand):
    @staticmethod
    async def handle('a-test-command'):
        print('test_command')

Ideally, when starting cli.py, I want to check which modules are there and load them dynamically, leaving the cli.py file clean. But I do not understand at all how it is better and more correct to implement it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion or problem

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions