# HG changeset patch
# User Raphaël Gomès <rgomes@octobus.net>
# Date 1678811402 -3600
#      Tue Mar 14 17:30:02 2023 +0100
# Node ID d6071d0f7edb2ca21c96855076eda68b0b5be81f
# Parent  560613c17607999a49682e5bdbb5125e3bbf0e4d
packaging: define `poulpe` as both a package and a CLI

This makes it easier for users to just `pip install poulpe` as well as
use `poulpe SUBCOMMAND SUBCOMMAND_ARGS` instead of manipulating $PATH.

diff --git a/python-libs/poulpe/cli.py b/python-libs/poulpe/cli.py
new file mode 100644
--- /dev/null
+++ b/python-libs/poulpe/cli.py
@@ -0,0 +1,29 @@
+from importlib import resources
+import subprocess
+
+import click
+
+
+@click.command(
+    no_args_is_help=True,
+    context_settings={
+        "ignore_unknown_options": True,
+        "allow_interspersed_args": False,
+    },
+)
+@click.argument('subcommand')
+@click.argument('subcommand_args', nargs=-1, type=click.UNPROCESSED)
+def exec_subcommand(subcommand, subcommand_args):
+    """Wrapper around Poulpe binaries for ease of calling"""
+    subcommand_file = resources.files("poulpe.bin").joinpath(subcommand)
+
+    if not subcommand_file.is_file():
+        raise click.BadParameter(f"Subcommand `{subcommand}` does not exist.")
+
+    with resources.as_file(subcommand_file) as f:
+        cmd = [str(f)] + list(subcommand_args)
+        exit(subprocess.call(cmd))
+
+
+if __name__ == '__main__':
+    exec_subcommand()
diff --git a/setup.cfg b/setup.cfg
--- a/setup.cfg
+++ b/setup.cfg
@@ -2,11 +2,21 @@
 name = poulpe
 version = 0.0.0
 
+[options]
+install_requires =
+    click == 8.1
+    importlib-resources
+    toml
+packages = find_namespace:
+package_dir =
+    = python-libs
 
-[options]
-packages=poulpe
-package_dir=
-    =python-libs
+[options.package_data]
+poulpe.bin = *
 
 [options.packages.find]
-where=python-libs
+where = python-libs
+
+[options.entry_points]
+console_scripts =
+    poulpe = poulpe.cli:exec_subcommand
\ No newline at end of file