mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-05-18 10:40:33 +02:00
Renamed file
This commit is contained in:
parent
9dd08396bc
commit
b5e27a4c89
68
main.py
68
main.py
@ -4,36 +4,24 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from textwrap import indent
|
|
||||||
|
|
||||||
def list_cli_commands(cli_dir):
|
def list_cli_commands(cli_dir):
|
||||||
"""List all available CLI script names in the given directory (without .py extension)."""
|
|
||||||
return sorted(
|
return sorted(
|
||||||
os.path.splitext(f.name)[0] for f in os.scandir(cli_dir)
|
os.path.splitext(f.name)[0] for f in os.scandir(cli_dir)
|
||||||
if f.is_file() and f.name.endswith(".py") and not f.name.startswith("__")
|
if f.is_file() and f.name.endswith(".py") and not f.name.startswith("__")
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_help_for_cli_command(cli_script):
|
def extract_docstring(cli_script_path):
|
||||||
"""Return the --help output for a given CLI script path."""
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
with open(cli_script_path, "r", encoding="utf-8") as f:
|
||||||
[sys.executable, cli_script, "--help"],
|
for line in f:
|
||||||
capture_output=True,
|
if line.strip().startswith(('"""', "'''")):
|
||||||
text=True,
|
return line.strip().strip('"\'')
|
||||||
check=True
|
if line.strip().startswith("DESCRIPTION"):
|
||||||
)
|
return line.split("=", 1)[1].strip().strip("\"'")
|
||||||
return result.stdout.strip()
|
except Exception:
|
||||||
except subprocess.CalledProcessError as e:
|
pass
|
||||||
return f"(⚠️ Help not available: {e})"
|
return "-"
|
||||||
|
|
||||||
def build_full_help(cli_dir, available_cli_commands):
|
|
||||||
"""Return a composed help string with help snippets from each CLI command."""
|
|
||||||
help_output = ["Available CLI commands:\n"]
|
|
||||||
for cmd in available_cli_commands:
|
|
||||||
cli_path = os.path.join(cli_dir, f"{cmd}.py")
|
|
||||||
help_snippet = get_help_for_cli_command(cli_path)
|
|
||||||
help_output.append(f"🔹 {cmd}\n{indent(help_snippet, ' ')}\n")
|
|
||||||
return "\n".join(help_output)
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||||
@ -42,23 +30,29 @@ def main():
|
|||||||
|
|
||||||
available_cli_commands = list_cli_commands(cli_dir)
|
available_cli_commands = list_cli_commands(cli_dir)
|
||||||
|
|
||||||
# Custom --help handler
|
# Special case: user ran `cymais playbook --help`
|
||||||
if "--help" in sys.argv or "-h" in sys.argv:
|
if len(sys.argv) >= 3 and sys.argv[1] in available_cli_commands and sys.argv[2] == "--help":
|
||||||
parser = argparse.ArgumentParser(
|
cli_script_path = os.path.join(cli_dir, f"{sys.argv[1]}.py")
|
||||||
description="CyMaIS CLI – proxy to tools in ./cli/",
|
subprocess.run([sys.executable, cli_script_path, "--help"])
|
||||||
formatter_class=argparse.RawDescriptionHelpFormatter
|
|
||||||
)
|
|
||||||
parser.add_argument("cli_command", choices=available_cli_commands, help="The CLI command to run (proxied from ./cli/)")
|
|
||||||
parser.add_argument("cli_args", nargs=argparse.REMAINDER, help="Arguments to pass to the CLI script")
|
|
||||||
parser.print_help()
|
|
||||||
print()
|
|
||||||
print(build_full_help(cli_dir, available_cli_commands))
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# Standard execution flow
|
# Global --help
|
||||||
parser = argparse.ArgumentParser(description="CyMaIS CLI – proxy to tools in ./cli/")
|
if "--help" in sys.argv or "-h" in sys.argv or len(sys.argv) == 1:
|
||||||
parser.add_argument("cli_command", choices=available_cli_commands, help="The CLI command to run (proxied from ./cli/)")
|
print("CyMaIS CLI – proxy to tools in ./cli/\n")
|
||||||
parser.add_argument("cli_args", nargs=argparse.REMAINDER, help="Arguments to pass to the CLI script")
|
print("Usage:")
|
||||||
|
print(" cymais <command> [options]\n")
|
||||||
|
print("Available commands:")
|
||||||
|
for cmd in available_cli_commands:
|
||||||
|
path = os.path.join(cli_dir, f"{cmd}.py")
|
||||||
|
desc = extract_docstring(path)
|
||||||
|
print(f" {cmd:25} {desc}")
|
||||||
|
print("\nUse 'cymais <command> --help' for details on each command.")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
# Default flow
|
||||||
|
parser = argparse.ArgumentParser(add_help=False)
|
||||||
|
parser.add_argument("cli_command", choices=available_cli_commands)
|
||||||
|
parser.add_argument("cli_args", nargs=argparse.REMAINDER)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
cli_script_path = os.path.join(cli_dir, f"{args.cli_command}.py")
|
cli_script_path = os.path.join(cli_dir, f"{args.cli_command}.py")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user