From 1dfa84a2707767e1b42a8494f14d01ffdf00cec1 Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Thu, 5 Jun 2025 11:36:19 +0100 Subject: [PATCH 1/2] Slitting out the generation of the argument parser for the CLI The generation of the argument parser is now a seperate function from calling it. This allows downstream programs to add extra functionality to their argument parser if needed. --- src/labthings_fastapi/server/cli.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/labthings_fastapi/server/cli.py b/src/labthings_fastapi/server/cli.py index a551109b..6f029253 100644 --- a/src/labthings_fastapi/server/cli.py +++ b/src/labthings_fastapi/server/cli.py @@ -10,8 +10,12 @@ from . import ThingServer, server_from_config -def parse_args(argv: Optional[list[str]] = None) -> Namespace: - """Process command line arguments for the server""" +def get_default_parser(): + """Return the default CLI parser for LabThings + + This can be used instead of `parse_args` if more arguments are needed + """ + parser = ArgumentParser() parser.add_argument("-c", "--config", type=str, help="Path to configuration file") parser.add_argument("-j", "--json", type=str, help="Configuration as JSON string") @@ -29,8 +33,13 @@ def parse_args(argv: Optional[list[str]] = None) -> Namespace: default=5000, help="Bind socket to this port. If 0, an available port will be picked.", ) - args = parser.parse_args(argv) - return args + return parser + + +def parse_args(argv: Optional[list[str]] = None) -> Namespace: + """Process command line arguments for the server""" + parser = get_default_parser() + return parser.parse_args(argv) def config_from_args(args: Namespace) -> dict: From 6884adb1a11de77d5073bbc997bde2168d1179d6 Mon Sep 17 00:00:00 2001 From: JohemianKnapsody <43779362+JohemianKnapsody@users.noreply.github.com> Date: Fri, 6 Jun 2025 15:02:46 +0100 Subject: [PATCH 2/2] Update src/labthings_fastapi/server/cli.py Co-authored-by: Julian Stirling --- src/labthings_fastapi/server/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/labthings_fastapi/server/cli.py b/src/labthings_fastapi/server/cli.py index 6f029253..759d9484 100644 --- a/src/labthings_fastapi/server/cli.py +++ b/src/labthings_fastapi/server/cli.py @@ -39,6 +39,7 @@ def get_default_parser(): def parse_args(argv: Optional[list[str]] = None) -> Namespace: """Process command line arguments for the server""" parser = get_default_parser() + # Use parser to parse CLI arguments and return the namespace with attributes set. return parser.parse_args(argv)