numberlink.cli¶
Command line interface for the numberlink package.
This module provides a small command line utility that can register the environment, print textual representations of
puzzle boards, list bundled level identifiers, and open an interactive viewer. The primary entry points are
build_parser() for constructing the argument parser and main() for the console entry point.
The subcommand handlers are implemented as handle_register(), handle_viewer(), handle_board(), and
handle_levels(). Use load_grid_from_file() to load a grid from a filesystem path and load_bridges()
to parse an optional JSON list of bridge coordinates.
See also numberlink.config.VariantConfig for variant options and numberlink.env.NumberLinkRGBEnv
for environment details used by the viewer and board commands.
Module Contents¶
Functions¶
Build the top level command line argument parser. |
|
Load a grid specification from disk using |
|
Load an optional solution adjacent to a grid file. |
|
Load bridge coordinates from a JSON file. |
|
Create a |
|
Create a |
|
Handle the |
|
Handle the |
|
Handle the |
|
Handle the |
|
Command line entry point for the package. |
API¶
- numberlink.cli.build_parser() argparse.ArgumentParser[source]¶
Build the top level command line argument parser.
The returned
argparse.ArgumentParseris configured with the following subcommands and their handlershandle_register(),handle_viewer(),handle_board(), andhandle_levels().- Returns:
Configured argument parser ready to parse command line arguments.
- Return type:
- numberlink.cli.load_grid_from_file(path_obj: pathlib.Path) collections.abc.Sequence[str][source]¶
Load a grid specification from disk using
numberlink.levels.load_level_from_file().Return the list of row strings stored in the loaded
numberlink.levels.Level.- Parameters:
path_obj – Filesystem path to a grid file.
- Returns:
List of row strings read from the file.
- Raises:
ValueError – When the file contains no non empty rows.
- numberlink.cli.load_solution_for_grid(path_obj: pathlib.Path) list[list[Coord]] | None[source]¶
Load an optional solution adjacent to a grid file.
Use
numberlink.levels.load_level_from_file()to load the grid and return thenumberlink.levels.Level.solutionvalue when present,Nonewhen no solution was given with the grid.- Parameters:
path_obj – Filesystem path to the grid file.
- Returns:
Solution paths or
Nonewhen not available.
- numberlink.cli.load_bridges(bridges_path: pathlib.Path | None) collections.abc.Sequence[Coord] | None[source]¶
Load bridge coordinates from a JSON file.
Return a sequence of
(row, column)pairs. The JSON file must contain a top level list. Each entry must be a two element sequence representing a row and column index. Strings and byte sequences are rejected. The function converts numeric values to Python integers and returnsNonewhenbridges_pathisNone.- Parameters:
bridges_path – Path to a JSON file or
Noneto indicate no bridges should be loaded.- Returns:
Sequence of coordinate pairs or
Nonewhen no file was provided.- Return type:
collections.abc.Sequence[tuple[int, int]] or None
- Raises:
ValueError – If any JSON entry is not a two element sequence of numeric values.
- numberlink.cli.build_variant_from_args(default_variant: numberlink.config.VariantConfig, must_fill: bool | None, allow_diagonal: bool | None, bridges_enabled: bool | None, cell_switch: bool | None) numberlink.config.VariantConfig[source]¶
Create a
numberlink.config.VariantConfigfrom CLI overrides.For each override parameter, when the provided value is not
Nonethe returned variant uses that value. When the provided value isNonethe corresponding attribute fromdefault_variantis preserved.- Parameters:
default_variant – Default variant to use for attributes that are not overridden.
must_fill – Override for the
must_filloption when notNone.allow_diagonal – Override for the
allow_diagonaloption when notNone.bridges_enabled – Override for the
bridges_enabledoption when notNone.cell_switch – Override for the
cell_switching_modeoption when notNone.
- Returns:
New
numberlink.config.VariantConfiginstance with the requested overrides applied.- Return type:
- numberlink.cli.build_render_config_from_args(args: argparse.Namespace) numberlink.config.RenderConfig | None[source]¶
Create a
numberlink.config.RenderConfigfrom CLI arguments when overrides are present.Return
Nonewhen no render config arguments were provided, allowing the environment to use its default configuration. When any render config option is specified, build and return a newnumberlink.config.RenderConfigwith overrides applied.- Parameters:
args – Parsed CLI arguments containing optional render config fields.
- Returns:
New
numberlink.config.RenderConfiginstance when overrides are present, otherwiseNone.- Return type:
RenderConfig or None
- numberlink.cli.handle_register(args: argparse.Namespace) int[source]¶
Handle the
registersubcommand.This function calls
numberlink.registration.register_numberlink_v0()to register the environment id with the Gymnasium registry. When theargs.quietflag is not set a brief confirmation message is emitted to standard output. The function returns an exit code suitable for a console entry point.- Parameters:
args – Parsed arguments from
build_parser()with an attributequietof typebool.- Returns:
Process exit code.
0indicates success.- Return type:
- numberlink.cli.handle_viewer(args: argparse.Namespace) int[source]¶
Handle the
viewersubcommand.Open an interactive viewer when appropriate. The function loads an optional grid using
load_grid_from_file()and optional bridge coordinates usingload_bridges(). It builds anumberlink.config.VariantConfigfrom CLI overrides usingbuild_variant_from_args(). It creates anumberlink.env.NumberLinkRGBEnvinstance for the requested configuration and performs an initialnumberlink.env.NumberLinkRGBEnv.reset().When the
--apply-solutionflag is provided and a stored solution is available the solution is applied by issuing calls tonumberlink.env.NumberLinkRGBEnv.step(). When--render-modeisansithe textual representation fromnumberlink.env.NumberLinkRGBEnv._render_text()is printed. For other render modes anumberlink.viewer.NumberLinkVieweris constructed and itsnumberlink.viewer.NumberLinkViewer.loop()method is invoked.The environment is always closed by calling
numberlink.env.NumberLinkRGBEnv.close()in afinallyblock.- Parameters:
args – Parsed arguments from
build_parser().- Returns:
Process exit code.
0indicates success.- Return type:
- numberlink.cli.handle_board(args: argparse.Namespace) int[source]¶
Handle the
boardsubcommand.Print a textual rendering of a requested level. The function behaves similarly to
handle_viewer()for argument parsing and variant construction. It creates anumberlink.env.NumberLinkRGBEnvwithrender_modeset toansiand prints the textual board returned bynumberlink.env.NumberLinkRGBEnv._render_text()after callingnumberlink.env.NumberLinkRGBEnv.reset().When the
--apply-solutionflag is provided the function attempts to retrieve a stored solution vianumberlink.env.NumberLinkRGBEnv.get_solution(). If a solution is found it is applied stepwise usingnumberlink.env.NumberLinkRGBEnv.step()and the resulting board is printed.The environment is closed by calling
numberlink.env.NumberLinkRGBEnv.close()in afinallyblock.- Parameters:
args – Parsed arguments from
build_parser().- Returns:
Process exit code.
0indicates success.- Return type:
- numberlink.cli.handle_levels(args: argparse.Namespace) int[source]¶
Handle the
levelssubcommand and print bundled level identifiers.The function enumerates the keys of
numberlink.levels.LEVELSand optionally filters them by substring whenargs.containsis set.- Parameters:
args – Parsed arguments from
build_parser()with an optional attributecontainsused for substring filtering.- Returns:
Process exit code.
0indicates success.- Return type:
- numberlink.cli.main(argv: collections.abc.Sequence[str] | None = None) int[source]¶
Command line entry point for the package.
The function constructs the parser via
build_parser(), parses the provided argument list, and dispatches to the handler bound on the chosen subparser. When no subcommand is provided the parser help message is printed and the function returns0.