Skip to content

Commit 6af29e7

Browse files
committed
new py5_tools __main__ module
1 parent c6299b5 commit 6af29e7

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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")
53+
print("Available commands:")
54+
for cmd in TOOLS_MAP:
55+
print(f" {cmd}")
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}")
66+
sys.exit(1)
67+
68+
# Import the argparse instance from the module
69+
if not hasattr(module, "parser"):
70+
print(f"Error: {full_module_name} does not expose a 'parser' instance.")
71+
sys.exit(1)
72+
73+
# Use the instance to parse the remaining command line arguments
74+
parsed_args = module.parser.parse_args(remaining_args)
75+
76+
if hasattr(module, "main"):
77+
module.main(parsed_args)
78+
else:
79+
print(f"Error: {full_module_name} does not have a 'main' function.")
80+
sys.exit(1)
81+
82+
83+
if __name__ == "__main__":
84+
main()

py5-resources/py5-module/src/py5_tools/tools/install_jdk.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
)
4040

4141

42-
def main():
43-
args = parser.parse_args()
42+
def main(args=None):
43+
args = args or parser.parse_args()
4444

4545
try:
4646
import jdk

0 commit comments

Comments
 (0)