lc_task.cli module¶
- lc_task.cli.CliParserConfig¶
Command line parser generator config type (see
gen_cli_parser()).alias of
Dict[Union[Type[Task],Tuple[Union[str,Tuple[str, …]],str]],Optional[CliParserConfig]]
- class lc_task.cli.CliTask(raise_exceptions: bool = False, args: Namespace | None = None)¶
Bases:
TaskBase task for tasks instantiated via command line.
Integrates with
ArgumentParserfor defining command line parsers.Examples:
Create a CLI task to count the number of lines in a file:
>>> import tempfile >>> from argparse import ArgumentParser >>> from pathlib import Path >>> from lc_task import TaskResult, taskclass >>> from lc_task.cli import CliTask >>> @taskclass ... class CountLinesTask(Task): ... input_path: typing.Optional[Path] = None ... def _perform_task(self) -> None: ... if self.input_path is None or not self.input_path.is_file(): ... raise FileNotFoundError(self.input_path) ... num_lines = 0 ... with self.input_path.open() as input_file: ... for _ in input_file: ... num_lines += 1 ... print(num_lines) ... >>> @taskclass ... class CountLinesCliTask(CliTask): ... _task_cls = CountLinesTask ... command = "linecount" ... description = "Count the number of lines in a file" ... @classmethod ... def gen_command_parser(cls, parser: typing.Optional[ArgumentParser] = None) -> ArgumentParser: ... parser = super(CountLinesCliTask, cls).gen_command_parser(parser) ... parser.add_argument("input_path", type=Path, help="Path to input file") ... return parser ... >>> with tempfile.NamedTemporaryFile() as tmp_file: ... # Write ten lines to file ... for _ in range(10): ... __ = tmp_file.write(b"\n") ... # Flush writes ... tmp_file.flush() ... _ = CountLinesCliTask.run_command(argv=[tmp_file.name]) 10
- _gen_task() Task | None¶
Generate new task.
Default implementation merges
CliTask.argswith instance ofCliTask._task_clsif it’s notNone.
- _perform_task() None¶
Perform the task.
Child classes should set attributes on
Task.resultin this function.
- classmethod gen_command_parser(parser: ArgumentParser | None = None) ArgumentParser¶
Generate command line parser.
- Parameters:
parser – parser to add arguments to.
- Returns:
Updated command line parser.
- Raises:
ValueError – if class variables
CliTask.commandandCliTask.descriptionare not set.
- lc_task.cli.gen_cli_parser(root_parser: ArgumentParser, parser_config: lc_task.cli.CliParserConfig) ArgumentParser¶
Generate commmand line parser from configuration.
The configuration is a dict that maps subcommands to
CliTaskclasses. Uses the configuration to generate a subcommand-style CLI likegit(e.g.git clone,git pull, etc).- Parameters:
root_parser – root parser to which all other parsers will be attached.
parser_config – mapping of subcommands to
CliTaskdefining the CLI hierarchy.
- Returns:
root_parserwith the subcommands defined inparser_config.- Raises:
TypeError – if subcommand isn’t of type
CliParserConfig.
Examples:
Given the parser config:
{ ("amphibians", "Amphibian animals"): { FrogsTask: {}, }, (("invertebrates", "i"), "Invertebrate animals"): { WormsTask: {}, ArthropodsTask: {}, }, ("reptiles", "Reptile animals"): { SnakesTask: {}, }, (("vertebrates", "v"), "Vertebrate animals"): { FishTask: {}, BirdsTask: {}, }, }
- The following command line paths would be valid (assuming
animals.pyis the root Python file): animals.py amphibiansanimals.py amphibians frogsanimals.py invertebratesanimals.py ianimals.py invertebrates wormsanimals.py i wormsanimals.py invertebrates arthropodsanimals.py i arthropodsanimals.py reptilesanimals.py reptiles snakesanimals.py vertebratesanimals.py vanimals.py vertebrates fishanimals.py v fishanimals.py vertebrates birdsanimals.py v birds