|
| 1 | +# ***************************************************************************** |
| 2 | +# |
| 3 | +# Part of the py5 library |
| 4 | +# Copyright (C) 2020-2026 Jim Schmitz |
| 5 | +# |
| 6 | +# This library is free software: you can redistribute it and/or modify it |
| 7 | +# under the terms of the GNU Lesser General Public License as published by |
| 8 | +# the Free Software Foundation, either version 2.1 of the License, or (at |
| 9 | +# your option) any later version. |
| 10 | +# |
| 11 | +# This library is distributed in the hope that it will be useful, but |
| 12 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
| 14 | +# General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | +# ***************************************************************************** |
| 20 | +import importlib |
| 21 | +import sys |
| 22 | + |
| 23 | +# Map the clean CLI subcommand to the actual module name in py5_tools.tools |
| 24 | +TOOLS_MAP = { |
| 25 | + "cmd": "py5cmd", |
| 26 | + "translate-imported2module": "py5translate_imported2module", |
| 27 | + "translate-module2imported": "py5translate_module2imported", |
| 28 | + "translate-processingpy2imported": "py5translate_processingpy2imported", |
| 29 | + "utils": "py5utils", |
| 30 | + "run-sketch": "run_sketch", |
| 31 | + "live-coding": "live_coding", |
| 32 | + "install-jdk": "install_jdk", |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +def main(): |
| 37 | + args = sys.argv[1:] |
| 38 | + |
| 39 | + # Check if the user is asking for general help or ran without arguments |
| 40 | + if not args or args[0] in {"-h", "--help"}: |
| 41 | + print("Usage: python -m py5_tools <command> [args...]\n") |
| 42 | + print("Available commands:") |
| 43 | + for cmd in TOOLS_MAP: |
| 44 | + print(f" {cmd}") |
| 45 | + sys.exit(0) |
| 46 | + |
| 47 | + # Get the first argument to figure out what the user wants |
| 48 | + command = args[0] |
| 49 | + remaining_args = args[1:] |
| 50 | + |
| 51 | + if command not in TOOLS_MAP: |
| 52 | + print(f"Error: Unknown command '{command}'\n", file=sys.stderr) |
| 53 | + print("Available commands:", file=sys.stderr) |
| 54 | + for cmd in TOOLS_MAP: |
| 55 | + print(f" {cmd}", file=sys.stderr) |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | + module_name = TOOLS_MAP[command] |
| 59 | + full_module_name = f"py5_tools.tools.{module_name}" |
| 60 | + |
| 61 | + try: |
| 62 | + # Dynamically load the requested tool module |
| 63 | + module = importlib.import_module(full_module_name) |
| 64 | + except ImportError as e: |
| 65 | + print(f"Error loading {full_module_name}: {e}", file=sys.stderr) |
| 66 | + sys.exit(1) |
| 67 | + |
| 68 | + # Import the argparse instance from the module |
| 69 | + if not hasattr(module, "parser"): |
| 70 | + print( |
| 71 | + f"Error: {full_module_name} does not expose a 'parser' instance.", |
| 72 | + file=sys.stderr, |
| 73 | + ) |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | + # Use the instance to parse the remaining command line arguments |
| 77 | + parsed_args = module.parser.parse_args(remaining_args) |
| 78 | + |
| 79 | + if hasattr(module, "main"): |
| 80 | + module.main(parsed_args) |
| 81 | + else: |
| 82 | + print( |
| 83 | + f"Error: {full_module_name} does not have a 'main' function.", |
| 84 | + file=sys.stderr, |
| 85 | + ) |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments