yapx.run¶
yapx.run¶
Use given functions to construct an ArgumentParser, parse the args, invoke the appropriate command and return any result.
PARAMETER | DESCRIPTION |
---|---|
command
|
the root command function
TYPE:
|
subcommands
|
a list or mapping of subcommand functions
TYPE:
|
args
|
arguments to parse (default=
TYPE:
|
default_args
|
arguments to parse when no arguments are given.
TYPE:
|
**kwargs
|
passed to the ArgumentParser constructor
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
... |
Examples:
import yapx
def print_nums(*args: int):
print("Args: ", *args)
return args
def find_evens(_context: yapx.Context):
return [x for x in _context.relay_value if x % 2 == 0]
def find_odds(_context: yapx.Context):
return [x for x in _context.relay_value if x % 2 != 0]
cli_args = ["find-odds", "1", "2", "3", "4", "5"]
result = yapx.run(print_nums, [find_evens, find_odds], args=cli_args)
assert result == [1, 3, 5]
yapx.run_commands¶
Use given functions to construct an ArgumentParser, parse the args, invoke the appropriate command and return any result.
yapx.run_commands(...)
is equivalent to yapx.run(None, ...)
, to be used when
there is no root command.
PARAMETER | DESCRIPTION |
---|---|
*args
|
...
TYPE:
|
**kwargs
|
...
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
... |
Examples:
import yapx
def find_evens(*args: int):
return [x for x in args if x % 2 == 0]
def find_odds(*args: int):
return [x for x in args if x % 2 != 0]
cli_args = ["find-odds", "1", "2", "3", "4", "5"]
result = yapx.run_commands([find_evens, find_odds], args=cli_args)
assert result == [1, 3, 5]
yapx.build_parser¶
Use given functions to construct an ArgumentParser
.
PARAMETER | DESCRIPTION |
---|---|
command
|
the root command function
TYPE:
|
subcommands
|
a list or mapping of subcommand functions
TYPE:
|
**kwargs
|
passed to the ArgumentParser constructor
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ArgumentParser
|
... |
Examples:
import yapx
import argparse
def print_nums(*args: int):
print("Args: ", *args)
return args
def find_evens(_context: yapx.Context):
return [x for x in _context.relay_value if x % 2 == 0]
def find_odds(_context: yapx.Context):
return [x for x in _context.relay_value if x % 2 != 0]
cli_args = ["find-odds", "1", "2", "3", "4", "5"]
parser = yapx.build_parser(print_nums, [find_evens, find_odds])
assert isinstance(parser, argparse.ArgumentParser)
yapx.build_parser_from_spec¶
Use given spec to construct an ArgumentParser
.
PARAMETER | DESCRIPTION |
---|---|
spec
|
...
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ArgumentParser
|
... |
Examples:
import yapx
from typing import Any
parser_spec: dict[str, Any] = {
"expected_prog": {
"description": "expected_description",
"arguments": {
"value": {"type": "int", "default": 69},
"flag": {"action": "store_true"},
},
"subparsers": {"expected_subcmd": {"description": "expected_description"}},
},
}
parser: yapx.ArgumentParser = yapx.build_parser_from_spec(parser_spec)
yapx.build_parser_from_file¶
Use given file to construct an ArgumentParser
.
Supports files in json or yaml format.
PARAMETER | DESCRIPTION |
---|---|
path
|
path to file containing spec.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ArgumentParser
|
... |
Examples:
Full spec for parsers and arguments:
.parser_ref:
description: "..."
add_help: true
arguments: {}
subparsers: {}
.argument_ref:
flags: []
pos: false
required: false
default: null
nargs: 1
type: str
choices: []
help: ""
metavar: ""
action: ""
group: ""
exclusive: false
const: null
Example spec in YAML:
'test-cli':
description: "halp"
add_help: true
arguments:
value:
type: int
default: 69
subparsers:
'test-subcmd':
arguments: {}
subparsers: {}