I will do a command line application with plugin capability, each new plugin will be invoked by a sub command from a __main__.py
script.
I used to use argparse
, I wonder if it's possible with argparse
to implement sub command + plugin looking like (I found some tool but using deprecated packages) ?
myfantasticCLI
├── __main__.py
└── plugins
├── create.py
├── notify.py
└── test.py
I know that I could use argparse
for sub command, but don't know how to use it in a dynamic loading way. :/
If you initialize the
argparse
subparsers withthen after parsing
args.cmd
will be the name of the chosen subparser or command.Then a simple
if
tree could import and run the desired modulesThere are fancier ways of doing this, but I prefer starting with the obvious.
Also my focus is on getting the
cmd
name from the parser, not on the details of importing a module given the name. You don't needargparse
to test theimport given a name
part of the problem.