Skip to content

cli

Tools for constructing a nice CLI.

PathToDir

Bases: Action

Checks a paths point to an existing directory.

Source code in src/orsm/cli/cli.py
 95
 96
 97
 98
 99
100
101
102
103
104
class PathToDir(argparse.Action):
    """
    Checks a paths point to an existing directory.
    """
    def __call__(self, parser, args, values, option_string=None):
        assert isinstance(values, str) and str
        file_path = Path(values)
        assert file_path.exists(), f"{file_path = } does not exist."
        assert file_path.is_dir(), f"{file_path = } is not a directory."
        setattr(args, self.dest, file_path)

PathToFile

Bases: Action

Checks a paths point to an existing file.

Source code in src/orsm/cli/cli.py
83
84
85
86
87
88
89
90
91
92
class PathToFile(argparse.Action):
    """
    Checks a paths point to an existing file.
    """
    def __call__(self, parser, args, values, option_string=None):
        assert isinstance(values, str) and str
        file_path = Path(values)
        assert file_path.exists(), f"{file_path = } does not exist."
        assert file_path.is_file(), f"{file_path = } is not a file."
        setattr(args, self.dest, file_path)

setup_standard_parser(*argparse_args, **argparse_kwargs)

Sets up a standard argument parser.

Parameters:

Name Type Description Default
argparse_args

Positional combinations passed to the parser.

()
argparse_kwargs

Keyword combinations passed to the parser.

{}

Returns:

Type Description
argparse.ArgumentParser :

An argument parser.

Source code in src/orsm/cli/cli.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def setup_standard_parser(*argparse_args, **argparse_kwargs) -> argparse.ArgumentParser:
    """
    Sets up a standard argument parser.

    Parameters
    ----------
    argparse_args :
        Positional combinations passed to the parser.
    argparse_kwargs :
        Keyword combinations passed to the parser.

    Returns
    -------
    argparse.ArgumentParser :
        An argument parser.

    """
    epilogue = dedent(f"""
    Version: 
        {repo.repo_name()} {repo.repo_version()}

    Author: 
        {repo.repo_author()}

    Maintained by: 
        {repo.repo_author()}
        <{repo.repo_email()}>
    """)
    parser = argparse.ArgumentParser(*argparse_args, **argparse_kwargs, formatter_class=HelpFormatter, allow_abbrev=False, epilog=epilogue)
    parser.add_argument("--version", help="Show the version of this program.", action=ShowVersion, nargs=0)
    # How we want to control the logging.
    parser.add_argument("--trace", help="Enable program tracing. Extremely verbose!", action=set_verbosity_level(logging.TRACE), nargs=0)
    parser.add_argument("--debug", help="Enable debug logging. Very verbose!", action=set_verbosity_level(logging.DEBUG), nargs=0)
    parser.add_argument("--verbose", help="Enable verbose logging. Quite verbose!", action=set_verbosity_level(logging.INFO), nargs=0)
    parser.add_argument("--quiet", help="Supress all logging. Quite quiet!", action=set_verbosity_level(logging.WARNING), nargs=0)
    parser.add_argument("--silent", help="Suppress most logging and output. Absolutely silent!", action=set_verbosity_level(logging.ERROR), nargs=0)
    parser.add_argument("--log_files", type=str, metavar="FILENAME", help="Store log output to files. Split into regular output and error output.", action=SetLogFileAction, nargs="?")
    parser.add_argument("--suppress_console_output", help="Whether to suppress output to the console.", action=SetConsoleSuppression, nargs=0)
    parser.add_argument("--profiling", help="Show results from profilers", action=SetProfiling, nargs=0)
    return parser

standard_parse(*argparse_args, **argparse_kwargs)

Perform a standard parse.

Parameters:

Name Type Description Default
argparse_args

Positional combinations passed to the parser.

()
argparse_kwargs

Keyword combinations passed to the parser.

{}

Returns:

Type Description
argparse.Namespace :

The namespace generated from the standard parse.

Source code in src/orsm/cli/cli.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def standard_parse(*argparse_args, **argparse_kwargs) -> argparse.Namespace:
    """
    Perform a standard parse.

    Parameters
    ----------
    argparse_args :
        Positional combinations passed to the parser.
    argparse_kwargs :
        Keyword combinations passed to the parser.

    Returns
    -------
    argparse.Namespace :
        The namespace generated from the standard parse.

    """
    parser = setup_standard_parser(*argparse_args, **argparse_kwargs)
    return parser.parse_args()

unit_test_parse(*argparse_args, **argparse_kwargs)

Parse command line ahead of forwarding onto the unittests.

Notes

To allow our command line combinations to be parsed ahead of running unit tests. Useful for logging. Taken from: https://stackoverflow.com/a/44248445/5134817

Parameters:

Name Type Description Default
argparse_args

Positional combinations passed to the parser.

()
argparse_kwargs

Keyword combinations passed to the parser.

{}

Returns:

Name Type Description
None None
Source code in src/orsm/cli/cli.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def unit_test_parse(*argparse_args, **argparse_kwargs) -> None:
    """
    Parse command line ahead of forwarding onto the unittests.

    Notes
    -----
    To allow our command line combinations to be parsed ahead of running unit tests. Useful for logging.
    Taken from: https://stackoverflow.com/a/44248445/5134817

    Parameters
    ----------
    argparse_args :
        Positional combinations passed to the parser.
    argparse_kwargs :
        Keyword combinations passed to the parser.

    Returns
    -------
    None :

    """
    import sys
    import unittest
    parser = setup_standard_parser(*argparse_args, **argparse_kwargs)
    ns, parsed_args = parser.parse_known_args(namespace=unittest)
    sys.argv[:] = sys.argv[:1] + parsed_args