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 subprocess
|
||||
import sys
|
||||
from textwrap import indent
|
||||
|
||||
def list_cli_commands(cli_dir):
|
||||
"""List all available CLI script names in the given directory (without .py extension)."""
|
||||
return sorted(
|
||||
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("__")
|
||||
)
|
||||
|
||||
def get_help_for_cli_command(cli_script):
|
||||
"""Return the --help output for a given CLI script path."""
|
||||
def extract_docstring(cli_script_path):
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[sys.executable, cli_script, "--help"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
return result.stdout.strip()
|
||||
except subprocess.CalledProcessError as e:
|
||||
return f"(⚠️ Help not available: {e})"
|
||||
|
||||
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)
|
||||
with open(cli_script_path, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
if line.strip().startswith(('"""', "'''")):
|
||||
return line.strip().strip('"\'')
|
||||
if line.strip().startswith("DESCRIPTION"):
|
||||
return line.split("=", 1)[1].strip().strip("\"'")
|
||||
except Exception:
|
||||
pass
|
||||
return "-"
|
||||
|
||||
def main():
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
@ -42,23 +30,29 @@ def main():
|
||||
|
||||
available_cli_commands = list_cli_commands(cli_dir)
|
||||
|
||||
# Custom --help handler
|
||||
if "--help" in sys.argv or "-h" in sys.argv:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="CyMaIS CLI – proxy to tools in ./cli/",
|
||||
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))
|
||||
# Special case: user ran `cymais playbook --help`
|
||||
if len(sys.argv) >= 3 and sys.argv[1] in available_cli_commands and sys.argv[2] == "--help":
|
||||
cli_script_path = os.path.join(cli_dir, f"{sys.argv[1]}.py")
|
||||
subprocess.run([sys.executable, cli_script_path, "--help"])
|
||||
sys.exit(0)
|
||||
|
||||
# Standard execution flow
|
||||
parser = argparse.ArgumentParser(description="CyMaIS CLI – proxy to tools in ./cli/")
|
||||
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")
|
||||
# Global --help
|
||||
if "--help" in sys.argv or "-h" in sys.argv or len(sys.argv) == 1:
|
||||
print("CyMaIS CLI – proxy to tools in ./cli/\n")
|
||||
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()
|
||||
|
||||
cli_script_path = os.path.join(cli_dir, f"{args.cli_command}.py")
|
||||
|
Loading…
x
Reference in New Issue
Block a user