8.8 KiB
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:
# 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:
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
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:
$ 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.
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.
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.
def scott_arguments(parser):
parser.add_argument(
"file_path",
help="The path where the file to be unpacked."
)
A complete command module:
# 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:
$ 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 themodules/packages and (b) is prepended tosys.pathso 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:
- Imports the module; if it cannot be imported, prints a warning to stderr and skips the command.
- Verifies
scott_runexists, is callable, and takes exactly one parameter; otherwise warns to stderr and skips the command. - Creates an
argparsesubparser namedcmd_name, usingscott_description()(if present) as its help text. - 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:
$ 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:
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:
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:
$ python hello-tool.py greet World
Hello, World!
7. Notes
- Work from any directory. The entry script's directory is added to
sys.pathautomatically, sopython /path/to/my-tool.py packworks 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 = Trueon 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
LICENSEfor details.