Jaymon / captain

command line python scripts for humans
MIT License
13 stars 1 forks source link

Subcommands #79

Closed Jaymon closed 4 months ago

Jaymon commented 1 year ago

I could do something similar to Endpoints and have a commands prefix:

commands/
  __init__.py
  foo.py
  bar/
    __init__.py
    che.py

And if commands/bar/che.py could have:

# commands.bar.che module

from captain import Command

class Boom(Command):
    def handle(self, *args, **kwargs):
        print(args, kwargs)

And then on the command line:

$ python bar che boom --one=1 --two=2

would call commands.bar.che.Boom(**{"one": ["1"], "two": ["2"]}).

In this way I could do arbitrary nested subcommands.

Jaymon commented 4 months ago

So the main file would be similar to an asgi or wsgi entry file with the addition of the if __name__ block:

from captain import Application

application = Application()

if __name__ == "__main__":
    application()

Then you can put all your commands in a commands module. The commands module will work exactly like endpoints's controllers. So Captain and Endpoints will basically work the exact same except one would be for http requests and the other would be for CLI requests.

Jaymon commented 4 months ago

Once I've got the new functionality in I think I should have a SimpleApplication that is the default, which is the current/old functionality. And then a ComplexApplication that contains all the new autoloading and submodule commands. The ComplexApplication is designed for primary command line interfaces with multiple subcommands while the SimpleApplication is for a simple script.

Jaymon commented 4 months ago

Rather than have simple and complex be explicit. I think I can have Application determine it, if there are already defined commands, then assume it's a simple command. If there aren't, then check for prefixes or autodiscover. If prefixes are explicitely passed in then load those. I think this covers all the bases with just the Application class