Overview

module url https://pypi.org/project/py-runner-base.html
git repository https://bitbucket.org/arrizza-public/py-runner-base
git command git clone git@bitbucket.org:arrizza-public/py-runner-base.git
verification report https://arrizza.com/web-ver/py-runner-base-report.html
version info
OS Language #Runs Last Run Cov%
macOS 15.6 Python 3.12 38 2026-07-14 100%
macOS 26.6 Python 3.12 9 2026-08-11 100%
macOS 26.6.1 Python 3.12 9 2026-09-04 0%
win32 Windows 11 Python 3.12 44 2026-09-05 100%
Ubuntu 24.04 noble Python 3.12 474 2026-09-07 100%
Ubuntu 26.04 resolute Python 3.12 318 2026-11-18 99%

Summary

This project is a base class for running various commands in python from the command line.

see sample/toolbox_app.py for an example use.

see doc/api.md for the API available.

how to run

Don't specify a command gives you help. Note that you can use check() in toolbox_runner.py or run() in toolbox_app.py to specify a default command.

./doit 
WARN no command chosen, nothing to do
     py-runner-base: v0.1.0
     Choose one of these:
        task1               : run task1
        task2a              : run task2 step-a
        task2b              : run task2 step-b
        task3               : run task3
        task4               : run task4

Note: that there are several sorting orders available, see property sort_order in base_runner.py or cmd_map.py.

  • Add an argument arg1:
$ ./doit task1 arg1
==== doit: starting...
     running Task1 args:['arg1']
     doit: run_it rc=0
  • Run a command "task1" that has no prerequisites:
./doit task1
==== doit: starting...
     running Task1 args:[]
     doit: run_it rc=0
  • Run a command "task2b" that has a prerequisite:
./doit task2b
==== doit: starting...
     running Task2 step-a
     running Task2 step-b
     doit: run_it rc=0

how to add commands

See the sample/taskxx.py files for example tasks. Note register() functions that allow you to specify command names for any tasks you want to add.

    # --------------------
    def register(self, runner):
        self._runner = runner
        self._runner.add_cmd('task2a', self._run_step_a, 'run task2 step-a')
        self._runner.add_cmd('task2b', self._run_step_b, 'run task2 step-b', prereqs=['task2a'])

Common usage scenarios

For runnable examples see sample/toolbox* scripts. Run these zpm doit task4 (or task1, etc.)

Typically three classes are needed:

  • xx_app.py: hold all the references, do all the setup and configuration, and then run the commands.
  • xx_runner.py: derives from RunnerBase and handles any special command setup, argument handling, etc.
  • xx_taskx.py : one or more classes to hold the code that implements the command(s)

xx_app.py

Typical:

# import task class(es) here

class ToolboxApp:
    def __init__(self):
        # any initialization here

        # create an instance of parent of RunnerBase 
        self._runner = ToolboxRunner()

        # build toolbox from the base and the mixins wanted
        tasks = (
            Task1,  # _ do Task1
            Task2,  # _ do Task2
        )

        # register() all the tasks.
        # register() can take a tuple, a list, or an individual class
        # Each tasks is class name, the register() instantiates it.
        self._runner.register(tasks)
        self._runner.register([Task3])
        self._runner.register(Task4)

    # the main.py invokes this function
    def run(self):
        # parse the CLI commands the user typed in
        self._runner.get_cli_info()
        # if there's any problems return
        if self._runner.rc != 0:
            return

        # do a double-check. Your version may or may not need this.
        self._runner.check()
        if self._runner.rc != 0:
            return

        # all is well, so run any/all the commands specified by the user
        # note that you can specify that only one command be called, or N, whatever is needed
        self._runner.run_all()

        # note that main.py will use svc.rc to ensure sys.exit() returns with the right rc. 
        svc.rc += self._runner.rc

xx_taskx.py

A typical class to register and run a command:

class Task1:
    def __init__(self):
        # needed to access any CLI args (or can use sys.argv[])
        self._runner = None

    def register(self, runner):
        # save the runner parent instance
        self._runner = runner

        # register a command "task1", the function to run and some help text
        self._runner.add_cmd('task1', self._run, 'run task1')

    def _run(self):
        # the command to run when task1 is declared
        # must return an rc so the sys.exit() can be set correctly
        errs = 0
        svc.log.line(f'running Task1 args:{self._runner.args}')
        return errs

xx_runner.py

The runner parent class:

from src.py_runner_base.runner_base import RunnerBase


# other imports as needed

class ToolboxRunner(RunnerBase):
    def __init__(self):
        super().__init__()
        # initialize the base runner and share the same logger
        self.init_runner_base(svc.log)

    def handle_unknown_arg(self, arg):
        # not a recognized command/task/step so assume it is an arg
        self._args.append(arg)
        return True

    def check(self):
        if self.nothing_chosen():
            svc.rc += 1
            svc.log.warn('no command chosen, nothing to do')
            self.report_err_msg()

API

init_runner_base

  • initialize the base content
  • init.init_runner_base(some_logger, prefix)
    • logger: optional; set the logger instance; defaults to FalconLogger
    • prefix: set the prefix to use for any logging

handle_unknown_arg

  • callback when an unknown CLI arg is found by get_cli_info().
  • def handle_unknown_arg(self, arg)
    • arg - the CLI argument found
    • should return True if no error occurred, False when an error occurs
  • Normally this is overridden when using this base class
    def handle_unknown_arg(self, arg):
        if arg == 'special-arg':
            return True
    
        svc.log.warn(f'unknown arg: "{arg}"')
        return False
    

nothing_chosen

  • returns True if the cmd list from the CLI is empty, False otherwise

report_err_msg

  • reports the list of registered commands and their descriptions

get_cli_info

  • get commands and args from the command line via sys.argv

sort_order (property and setter)

  • set the order that the commands are executed
    • created - (default) run as created via calls to register()
    • entered - run as entered
    • alpha - sort alphabetically
    • index - sort by index (see the add_cmd sort_idx arg)

cmds

  • returns the current set of commands that can be run

run_all

  • run all of the commands requested by the CLI. The commands are sorted according to the sort_order()
  • see run_cmd() for running individual commands.

run_cmd

  • run the given command. Typically run via run_all().
  • run_cmd(cmd_name)
    • cmd_name - the cmd name
    • returns an integer rc

register mainline

  • called by mainline apps with a list of task classes or a single task class
  • These tasks are instatianted and then registered by calling the register callback in that class.

register callback

  • callback in task classes called by runner_base
  • def register(self, runner):
    • runner - reference to the py_runner instance
  • These are used to add commands and arguments for that task class
    • commands are added by add_cmd
    • args are added by one of:
      • add_bool_arg
      • add_int_arg
      • add_str_arg
      • add_float_arg

add_cmd

  • called by task classes to add a command to the current list
  • self._runner.add_cmd(cmd, cmd_fn, desc, prereqs, sort_idx)
    • cmd - the command name
    • cmd_fn - the function to invoke when this command is named
    • desc - help for this command
    • prereqs - any pre-requisite commands for this cmd
    • sort_idx - the sorting value/index for this cmd (see sort_order)

add_bool_arg

  • used to define a boolean argument, typically done in the task class's register() callback
  • self._runner.add_bool_arg(cmd, arg, default)
    • cmd - the command this arg is used by
    • arg - the full argument
    • default - the default value to use; default is False
    • the arg is available in the run functions e.g
    # in register()
    self._runner.add_bool_arg('task4', '--bool1')
    # ... skip ...
    
    # in run_fn()
    if self._runner.args.bool1:
        self.do_something()
    
    • note the name is trimmed similar to argparse: initial "--" are removed and embedded "-" are converted to "_"

add_int_arg

  • used to define an integer argument, typically done in the task class's register() callback
  • self._runner.add_int_arg(cmd, arg, default)
    • cmd - the command this arg is used by
    • arg - the full argument
    • default - the default value to use; default is 0
    • the arg is available in the run functions e.g
    # in register()
    self._runner.add_int_arg('task4', '--int1', default=42)
    # ... skip ...
    
    # in run_fn()
    print(f'the value is {self._runner.args.int1}')
    
    • note the name is trimmed similar to argparse: initial "--" are removed and embedded "-" are converted to "_"

add_float_arg

  • used to define a float argument, typically done in the task class's register() callback
  • self._runner.add_float_arg(cmd, arg, default)
    • cmd - the command this arg is used by
    • arg - the full argument
    • default - the default value to use; default is 0.0
    • the arg is available in the run functions e.g
    # in register()
    self._runner.add_float_arg('task4', '--float1', default=3.14)
    # ... skip ...
    
    # in run_fn()
    print(f'the value is {self._runner.args.float1}')
    
    • note the name is trimmed similar to argparse: initial "--" are removed and embedded "-" are converted to "_"

add_str_arg

  • used to define a string argument, typically done in the task class's register() callback
  • self._runner.add_float_arg(cmd, arg, default)
    • cmd - the command this arg is used by
    • arg - the full argument
    • default - the default value to use; default is '' (empty string)
    • the arg is available in the run functions e.g
    # in register()
    self._runner.add_str_arg('task4', '--str1', default='hello world')
    # ... skip ...
    
    # in run_fn()
    print(f'the value is {self._runner.args.str1}')
    
    • note the name is trimmed similar to argparse: initial "--" are removed and embedded "-" are converted to "_"

rc

  • returns the current return code.
  • Note the return code is a sum of all values returned by the invoked command functions

args

  • returns references CLI args entered
  • used to get the value for the current value for that arg e.g.
    # in register()
    self._runner.add_int_arg('task4', '--int1', default=42)
    # ... skip...
    # in run()
    if self._runner.args.int1 > 40:
        self._do_something(self._runner.args.int1)
    
  • Note: "-n" is predefined. This causes run_cmd() to only write a log line to stdout.

is_given

  • self._runner.is_given('--int1')
    • returns True if the --int1 was used on the CLI, false otherwise

dry_run

  • returns True if this a dry run. see "-n" CLI arg.

delete_cmd

  • delete the given command from the list of commands to run
  • delete_cmd(cmd_name)
    • cmd_name - the cmd to remove

- John Arrizza