Skip to content

cli

Functions and definitions useful when working with ArgumentParser.

Attributes:

  • ArgumentCmdParser (TypeAlias) –

    generic type of a subparser of the ArgumentParser class.

Attributes

ArgumentCmdParser module-attribute

ArgumentCmdParser: TypeAlias = _SubParsersAction

Functions

existing_dir

existing_dir(path: str | Path)

Check if path points to an existing directory.

Example:

from argparse import ArgumentParser
from ipsl_common.cli import existing_dir
parser = ArgumentParser()
parser.add_argument("file", type=existing_dir)

Parameters:

  • path

    (str | Path) –

    path to a directory

Source code in ipsl_common/cli.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def existing_dir(path: str | Path):
    """Check if path points to an existing directory.

    Example:

        from argparse import ArgumentParser
        from ipsl_common.cli import existing_dir
        parser = ArgumentParser()
        parser.add_argument("file", type=existing_dir)

    Args:
        path: path to a directory
    """
    path = Path(path)
    if not path.exists():
        raise ArgumentTypeError(f'Path "{str(path)}" doesn\'t exist')
    if not path.is_dir():
        raise ArgumentTypeError(f'Path "{str(path)}" is not a directory')
    return path

existing_file

existing_file(path: str | Path)

Check if path points to an existing file.

Example:

from argparse import ArgumentParser
from ipsl_common.cli import existing_file
parser = ArgumentParser()
parser.add_argument("file", type=existing_file)

Parameters:

  • path

    (str | Path) –

    path to a file

Source code in ipsl_common/cli.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def existing_file(path: str | Path):
    """Check if path points to an existing file.

    Example:

        from argparse import ArgumentParser
        from ipsl_common.cli import existing_file
        parser = ArgumentParser()
        parser.add_argument("file", type=existing_file)

    Args:
        path: path to a file
    """
    path = Path(path)
    if not path.exists():
        raise ArgumentTypeError(f'Path "{str(path)}" doesn\'t exist')
    if not path.is_file():
        raise ArgumentTypeError(f'Path "{str(path)}" is not a file')
    return path