overall changes

This commit is contained in:
2026-09-16 22:34:34 +05:30
parent 45a1a35c2f
commit 5faca0fc02
7 changed files with 410 additions and 359 deletions
+144
View File
@@ -0,0 +1,144 @@
---
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:
```python
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:
```sh
$ 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.
```python
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.
```python
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.
```python
def s_arguments(parser):
parser.add_argument(
"posarg",
help="The positional argument."
)
```
A complete command module:
```python
# 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:
```sh
$ 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
```