the first commit
This commit is contained in:
@@ -0,0 +1,311 @@
|
||||
# Scott — Usage Guide
|
||||
|
||||
**scott** is a framework for creating scoped, task-oriented terminal tools. It wraps
|
||||
Python's `argparse` and adds a dynamic subcommand system: each subcommand lives in its
|
||||
own module, is loaded on demand, declares its own CLI options, and is dispatched by the
|
||||
framework. You write small, self-contained command modules; Scott handles argument
|
||||
parsing, help text, validation, and error reporting.
|
||||
|
||||
---
|
||||
|
||||
## 1. Installation
|
||||
|
||||
Scott is a standard setuptools package. From the package root:
|
||||
|
||||
```sh
|
||||
# create and activate a virtual environment (recommended)
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate
|
||||
|
||||
# build a distributable wheel/sdist
|
||||
pip install --upgrade build
|
||||
python -m build
|
||||
|
||||
# install the built wheel
|
||||
pip install dist/scott-1.0-*.whl
|
||||
|
||||
# ...or install in editable mode for development
|
||||
pip install -e .
|
||||
```
|
||||
|
||||
Once installed, import the framework with:
|
||||
|
||||
```python
|
||||
from scott import Scott
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. Anatomy of a Scott tool
|
||||
|
||||
A Scott application is a plain Python script (the *entry point*) plus a `modules/`
|
||||
directory of subcommand packages, laid out like this:
|
||||
|
||||
```
|
||||
my-tool/
|
||||
├── my-tool.py # entry point script
|
||||
└── modules/
|
||||
├── cleanup/
|
||||
│ └── command.py # implements the `cleanup` subcommand
|
||||
├── pack/
|
||||
│ └── command.py # implements the `pack` subcommand
|
||||
└── unpack/
|
||||
└── command.py # implements the `unpack` subcommand
|
||||
```
|
||||
|
||||
Each subcommand name maps to `modules/<name>/command.py`. The entry point registers the
|
||||
commands it wants to expose, and Scott imports and dispatches them dynamically.
|
||||
|
||||
### 2.1 The entry point
|
||||
|
||||
```python
|
||||
from scott import Scott
|
||||
|
||||
|
||||
def main():
|
||||
# app_dir: path of this script — Scott resolves it to locate the modules/ directory
|
||||
# app_desc: description shown in the tool's help output
|
||||
scott = Scott(__file__, "A toolset to manage EPUB files")
|
||||
|
||||
commands = [
|
||||
"cleanup",
|
||||
"pack",
|
||||
"unpack",
|
||||
]
|
||||
for command in commands:
|
||||
scott.register_command(command)
|
||||
|
||||
scott.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
```
|
||||
|
||||
Running the tool:
|
||||
|
||||
```sh
|
||||
$ python my-tool.py --help
|
||||
usage: my-tool.py [-h] {cleanup,pack,unpack} ...
|
||||
|
||||
A toolset to manage EPUB files
|
||||
|
||||
positional arguments:
|
||||
{cleanup,pack,unpack}
|
||||
cleanup This will cleanup
|
||||
pack This will pack
|
||||
unpack This will unpack
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. The command module contract
|
||||
|
||||
Every command module (`modules/<name>/command.py`) is a plain Python module. Scott
|
||||
recognizes the following functions:
|
||||
|
||||
| Function | Required | Purpose |
|
||||
| ------------------------- | -------- | ------------------------------------------------------------------------ |
|
||||
| `scott_run(args)` | **Yes** | The command's entry point. Called with the parsed `argparse` namespace. |
|
||||
| `scott_description()` | Optional | Returns a short help string shown in the command list. |
|
||||
| `scott_arguments(parser)` | Optional | Registers command-specific options on the command's subparser. |
|
||||
|
||||
### 3.1 `scott_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 scott_run(args):
|
||||
# args contains any options registered via scott_arguments(),
|
||||
# plus args.command (the subcommand name)
|
||||
print("command: pack")
|
||||
```
|
||||
|
||||
### 3.2 `scott_description()` — optional
|
||||
|
||||
Returns a one-line description used as the command's help text in the parent help
|
||||
listing.
|
||||
|
||||
```python
|
||||
def scott_description():
|
||||
return "This will pack"
|
||||
```
|
||||
|
||||
### 3.3 `scott_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 scott_arguments(parser):
|
||||
parser.add_argument(
|
||||
"file_path",
|
||||
help="The path where the file to be unpacked."
|
||||
)
|
||||
```
|
||||
|
||||
A complete command module:
|
||||
|
||||
```python
|
||||
# modules/unpack/command.py
|
||||
|
||||
def scott_description():
|
||||
return "This will unpack"
|
||||
|
||||
def scott_arguments(parser):
|
||||
parser.add_argument(
|
||||
"file_path",
|
||||
help="The path where the file to be unpacked."
|
||||
)
|
||||
|
||||
def scott_run(args):
|
||||
print(f"command: unpack, file: {args.file_path}")
|
||||
```
|
||||
|
||||
Which produces:
|
||||
|
||||
```sh
|
||||
$ python my-tool.py unpack --help
|
||||
usage: my-tool.py unpack [-h] file_path
|
||||
|
||||
positional arguments:
|
||||
file_path The path where the file to be unpacked.
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. API reference
|
||||
|
||||
### `Scott(app_dir: str, app_desc: str = "")`
|
||||
|
||||
Creates the framework and its argument parser.
|
||||
|
||||
- `app_dir` — typically `__file__` from the entry script. Scott resolves it to the
|
||||
containing directory, which is used to (a) locate the `modules/` packages and (b) is
|
||||
prepended to `sys.path` so the tool works no matter which directory you invoke it from.
|
||||
- `app_desc` — description shown at the top of the tool's help output.
|
||||
|
||||
### `Scott.register_command(cmd_name: str)`
|
||||
|
||||
Registers a subcommand by dynamically importing `modules.<cmd_name>.command`. During
|
||||
registration Scott:
|
||||
|
||||
1. Imports the module; if it cannot be imported, prints a warning to *stderr* and skips
|
||||
the command.
|
||||
2. Verifies `scott_run` exists, is callable, and takes **exactly one** parameter;
|
||||
otherwise warns to *stderr* and skips the command.
|
||||
3. Creates an `argparse` subparser named `cmd_name`, using `scott_description()` (if
|
||||
present) as its help text.
|
||||
4. Calls `module.scott_arguments(subparser)` (if present) so the command can add options.
|
||||
|
||||
Skipped commands never appear in help output and cannot be invoked.
|
||||
|
||||
### `Scott.run()`
|
||||
|
||||
Parses the process arguments and dispatches to the selected command's
|
||||
`scott_run(args)`. Behavior on failure is covered in the next section.
|
||||
|
||||
---
|
||||
|
||||
## 5. Error handling and exit codes
|
||||
|
||||
| Situation | Behavior | Exit code |
|
||||
| --------------------------------------- | -------------------------------------------------------------------------------------------- | --------- |
|
||||
| Command runs successfully | Normal output. | `0` |
|
||||
| Exception raised inside `scott_run` | Prints `!! Error Encountered !!`, `(command): <error>`, and the command's help to *stderr*. | `0` |
|
||||
| Unknown command / missing required args | argparse error message, then the tool's full help is printed. | `2` |
|
||||
| No command given | Help is printed (a command is **required**). | `2` |
|
||||
|
||||
Example of a command that raised an exception:
|
||||
|
||||
```sh
|
||||
$ python my-tool.py unpack /tmp/book.epub
|
||||
command: unpack
|
||||
|
||||
!! Error Encountered !!
|
||||
(unpack): The unpack command is not yet implemented.
|
||||
usage: my-tool.py unpack [-h] file_path
|
||||
|
||||
positional arguments:
|
||||
file_path The path where the file to be unpacked.
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
```
|
||||
|
||||
Note that errors inside a command are caught and reported but do not propagate — the
|
||||
process still exits `0`. Commands that need to signal failure should handle it
|
||||
themselves (e.g. via `sys.exit(1)` inside `scott_run`).
|
||||
|
||||
---
|
||||
|
||||
## 6. A complete, minimal example
|
||||
|
||||
```
|
||||
hello-tool/
|
||||
├── hello-tool.py
|
||||
└── modules/
|
||||
└── greet/
|
||||
└── command.py
|
||||
```
|
||||
|
||||
`hello-tool.py`:
|
||||
|
||||
```python
|
||||
from scott import Scott
|
||||
|
||||
|
||||
def main():
|
||||
scott = Scott(__file__, "A tiny Scott-powered tool")
|
||||
scott.register_command("greet")
|
||||
scott.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
```
|
||||
|
||||
`modules/greet/command.py`:
|
||||
|
||||
```python
|
||||
def scott_description():
|
||||
return "Greet someone"
|
||||
|
||||
def scott_arguments(parser):
|
||||
parser.add_argument("name", help="Who to greet")
|
||||
|
||||
def scott_run(args):
|
||||
print(f"Hello, {args.name}!")
|
||||
```
|
||||
|
||||
Run it:
|
||||
|
||||
```sh
|
||||
$ python hello-tool.py greet World
|
||||
Hello, World!
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. Notes
|
||||
|
||||
- **Work from any directory.** The entry script's directory is added to `sys.path`
|
||||
automatically, so `python /path/to/my-tool.py pack` works from anywhere.
|
||||
- **Commands are loaded at registration.** Modules are imported once, when
|
||||
`register_command()` is called, and reused for dispatch; only registered commands are
|
||||
loaded.
|
||||
- **Bytecode is disabled.** Scott sets `sys.dont_write_bytecode = True` on import, so no
|
||||
`__pycache__` directories are created for the framework or your modules.
|
||||
- **Layout matters.** The `modules/` directory must be a sibling of the entry script, and
|
||||
each command must be a package directory named exactly like the command
|
||||
(`modules/<name>/command.py`).
|
||||
- **License.** Scott is distributed under the DuxTechnica Limited Use Source Code License
|
||||
Agreement (DT-LUSCL) — see `LICENSE` for details.
|
||||
|
||||
Reference in New Issue
Block a user