From 2d578c548b8717f7b45fd61071546956b8cd4a1a Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Wed, 24 Sep 2025 17:20:34 +0200 Subject: [PATCH 1/2] :sparkles: create a minimal cli interface example --- pyproject.toml | 6 ++++++ src/python_package/cli.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/python_package/cli.py diff --git a/pyproject.toml b/pyproject.toml index fc7ca38..ec501b0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,3 +68,9 @@ requires = ["setuptools>=64", "setuptools_scm>=8"] [tool.isort] profile = "black" + +# Script entry points, i.e. command line commands available after installing the package +# e.g. implemented using argparse +# Then you can type: `python-package-hello -h` in the terminal +[project.scripts] +python-package-hello = "python_package.cli:main" diff --git a/src/python_package/cli.py b/src/python_package/cli.py new file mode 100644 index 0000000..dd1d220 --- /dev/null +++ b/src/python_package/cli.py @@ -0,0 +1,17 @@ +import argparse + +from .mockup import hello_world + + +def main(): + parser = argparse.ArgumentParser(description="Python Package CLI") + parser.add_argument( + "-n", + "--repeat", + type=int, + default=1, + help="Number of times to repeat the greeting hello world", + ) + args = parser.parse_args() + msg = hello_world(args.repeat) + print(msg) From b60a0f0c393ce3bdcba4808be84ca6b75271af3e Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Thu, 25 Sep 2025 12:51:24 +0200 Subject: [PATCH 2/2] :memo: document command line entry point --- developing.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/developing.md b/developing.md index d5424c9..4fce3ee 100644 --- a/developing.md +++ b/developing.md @@ -142,6 +142,28 @@ Please also update the project URL to your project: "Homepage" = "https://github.com/RasmussenLab/python_package" ``` +The template also sets a command line script entry point, which allows to run +the function `main` in the `mockup` module of the package as a command line script, +wrapping the `hello_world` function from the `mockup` module. +The template uses the standard library [argparse](https://docs.python.org/3/library/argparse.html) +module to parse parameters from the command line and creates a basic interface. + +```toml +# Script entry points, i.e. command line commands available after installing the package +# e.g. implemented using argparse +# Then you can type: `python-package-hello -h` in the terminal +[project.scripts] +python-package-hello = "python_package.cli:main" +``` + +You can therefore run the command line script using: + +```bash +python-package-hello -h +# print hello world 3 times +python-package-hello -n 3 +``` + ## Source directory layout of the package The source code of the package is located in the `src` directory, to have a project