console
module¶
- create_console_parser()[source]¶
Create CharGer’s commandline parser.
See
CharGerConfig
for the detailed specification of each parameter.- Return type
- parse_console(args=None)[source]¶
Create a
CharGerConfig
object based on the command-line arguments or the given args.- Return type
- setup_logger()[source]¶
Set up stderr logging format.
The logging format and colors can be overridden by setting up the environment variables such as
LOGURU_FORMAT
. See Loguru documentation for details.- Return type
argtype
module¶
- class PathType(exists=None, type=None)[source]¶
Parse file path arguments as a
pathlib.Path
object.Type factory for ArgumentParser.add_argument() that validates the file path arguments. Adapted from https://stackoverflow.com/a/33181083
- Parameters
exists (Optional[bool]) – The path must exist when True, and the path must not exist when False. Skip the check when None.
type (Union[None, str, Callable[[pathlib.Path], bool]]) –
file
will be checked byis_file()
.dir
will be checked byis_dir()
. It also accepts a function that takesPath
object and returns True for valid paths. Skip the check when None.
Examples
>>> parser = argparse.ArgumentParser()
Add an argument that must be an existing file, but can also be specified as a dash (
-
) in the command,>>> parser.add_argument('existing_file', type=PathType(exists=True, type='file'))
Add an argument for a new path that must NOT exist, but the parent folder exists,
>>> from pathlib import Path >>> CreatablePathType = PathType(exists=False, type=lambda p: p.parent.is_dir()) >>> parser.add_argument('non_existing_folder', type=CreatablePathType)