3.4 KiB
title
| title |
|---|
| The Anatomy of a Scott application |
Anatomy of a Scott application
A Scott application is a plain Python script (the entry point) plus a commands/ directory of
command packages, laid out like this:
scott-app/
├── main.py # entry point script
└── commands/
├── cmd1/
│ └── handler.py # implements the `cmd1` command
├── cmd2/
│ └── handler.py # implements the `cmd2` command
└── cmd3/
└── handler.py # implements the `cmd3` command
Each command name maps to commands/<name>/handler.py. The entry point registers the
commands it wants to expose, and Scott imports and dispatches them dynamically.
The entry point
For the above example this would be the main.py script. Typically, this would look like:
from scott import Scott
def main():
# app_dir: path of this script — Scott resolves it to locate the tools/ directory
# app_desc: description shown in the tool's help output
scott = Scott(__file__, "A toolset to do something")
commands = [
"cmd1",
"cmd2",
"cmd3",
]
for cmd in commands:
scott.register(cmd)
scott.run()
if __name__ == "__main__":
main()
Running the tool:
$ python main.py --help
usage: main.py [-h] {cmd1,cmd2,cmd3} ...
A toolset to do something
positional arguments:
{cmd1,cmd2,cmd3}
cmd1 description for cmd1
cmd2 description for cmd2
cmd3 description for cmd3
options:
-h, --help show this help message and exit
The command contract
Every command module (commands/<name>/handler.py) is a plain Python module. Scott recognizes the
following functions within handler.py:
| Function | Required | Purpose |
|---|---|---|
s_run(args) |
Yes | The command's entry point. Called with the parsed argparse namespace. |
s_description() |
Optional | Returns a short help string shown in the command list. |
s_arguments(parser) |
Optional | Registers command-specific options on the command's subparser. |
s_run(args) — required
The command's main function. It must accept exactly one argument (the parsed argparse
namespace); registration is skipped with a warning otherwise.
def s_run(args):
# args contains any options registered via scott_arguments(),
# plus args.command (the subcommand name)
print("command: cmd1")
s_description() — optional
Returns a one-line description used as the command's help text in the parent help listing.
def s_description():
return "description for cmd1"
s_arguments(parser) — optional
Receives the command's argparse subparser so the command can define its own positional arguments
and options. They arrive in args at dispatch time.
def s_arguments(parser):
parser.add_argument(
"posarg",
help="The positional argument."
)
A complete command module:
# commands/cmd1/handler.py
def s_description():
return "description for cmd1"
def s_arguments(parser):
parser.add_argument(
"posarg",
help="The positional argument."
)
def s_run(args):
print(f"command: cmd1, posarg: {args.posarg}")
Which produces:
$ python main.py cmd1 --help
usage: main.py cmd1 [-h] posarg
positional arguments:
posarg The positional argument.
options:
-h, --help show this help message and exit