From aecdc6a456499921f73d9d1f80ab8bd27416adfd Mon Sep 17 00:00:00 2001 From: Joseph Burling Date: Fri, 22 Apr 2022 12:28:16 -0500 Subject: [PATCH] build(retrocookie): reflect updates to source template from generated project 'science-institute_brain-lab' --- .../__main__.py | 83 +++++++++++++++++++ ...okiecutter.__project_name}}.code-workspace | 2 + 2 files changed, 85 insertions(+) create mode 100644 datajoint-workflow/{{cookiecutter.github_repo}}/src/{{cookiecutter.__pkg_import_name}}/__main__.py diff --git a/datajoint-workflow/{{cookiecutter.github_repo}}/src/{{cookiecutter.__pkg_import_name}}/__main__.py b/datajoint-workflow/{{cookiecutter.github_repo}}/src/{{cookiecutter.__pkg_import_name}}/__main__.py new file mode 100644 index 0000000..d624255 --- /dev/null +++ b/datajoint-workflow/{{cookiecutter.github_repo}}/src/{{cookiecutter.__pkg_import_name}}/__main__.py @@ -0,0 +1,83 @@ +"""Module-level command-line interface + +This serves as an example command-line interface for the main package module. + +Example: + Usage as a console entrypoint: + + {{cookiecutter.__pkg_import_name}} --help + {{cookiecutter.__pkg_import_name}} --version + + Usage as a script: + + python -m {{cookiecutter.__pkg_import_name}} + + Usage from python: + + from {{cookiecutter.__pkg_import_name}} import main; main(...) +""" + +import argparse +import sys +from typing import Any, Sequence + +from {{cookiecutter.__pkg_import_name}} import version + + +def parse_args(args: Sequence[str]) -> argparse.Namespace: + """_Parse command line parameters_ + + Args: + args (list[str]): + Command line parameters as list of strings. + (example `["--help"]`) + + Returns: + A Namespace of command line parameters. + """ + + class ArgumentDefaultsRawDescriptionHelpFormatter( + argparse.ArgumentDefaultsHelpFormatter, + argparse.RawDescriptionHelpFormatter, + argparse.MetavarTypeHelpFormatter, + ): + """Combination of different formatters""" + + pass + + parser = argparse.ArgumentParser( + prog="{{cookiecutter.__pkg_import_name}}", + epilog=__doc__, + formatter_class=ArgumentDefaultsRawDescriptionHelpFormatter, + ) + + parser.add_argument( + "--version", + action="version", + version=f"%(prog)s v{version}", + ) + + parsed_args = parser.parse_args(args) + + return parsed_args + + +def main(**kwargs: Any) -> None: + """_Operate on cli args_.""" + print("main module operations here.") + + +def cli() -> None: + """_Calls [`__main__.main`][{{cookiecutter.__pkg_import_name}}.__main__.main], passing the cli + arguments extracted from `sys.argv`_. + + This function can be used as entry point to create console scripts on package + install. + """ + + args = parse_args(sys.argv[1:]) + main(**vars(args)) + + +if __name__ == "__main__": + cli() diff --git a/datajoint-workflow/{{cookiecutter.github_repo}}/{{cookiecutter.__project_name}}.code-workspace b/datajoint-workflow/{{cookiecutter.github_repo}}/{{cookiecutter.__project_name}}.code-workspace index 3a8d608..f896790 100644 --- a/datajoint-workflow/{{cookiecutter.github_repo}}/{{cookiecutter.__project_name}}.code-workspace +++ b/datajoint-workflow/{{cookiecutter.github_repo}}/{{cookiecutter.__project_name}}.code-workspace @@ -385,6 +385,8 @@ "-i", "'!src/{{cookiecutter.__pkg_import_name}}/__init__.py'", "-i", + "'!src/{{cookiecutter.__pkg_import_name}}/__main__.py'", + "-i", "'!src/{{cookiecutter.__pkg_import_name}}/entrypoint.py'", "${workspaceFolder}" ],