lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
410 stars 52 forks source link

Add `main` decorator for simple parsing of entry functions #176

Closed JesseFarebro closed 1 year ago

JesseFarebro commented 1 year ago

Adds a decorator main that will parse command line arguments based on the type annotations of that function.

This decorator supports:

Example:

import pathlib

import simple_parsing as sp

@sp.decorators.main
def mkdir(path: pathlib.Path, /, *, parents: bool = False, exist_ok: bool = False) -> None:
    """ Make a directory.

    Args:
        path: Path at which to create the directory.
        parents: Whether to create parent directories.
        exist_ok: If exist_ok is True `FileExistsError` will be raised.
    """
    path.mkdir(parents=parents, exist_ok=exist_ok)

if __name__ == '__main__':
    mkdir()