Skip to content

Instantly share code, notes, and snippets.

@smeech
Last active January 31, 2025 16:13
Show Gist options
  • Select an option

  • Save smeech/0f5cfc29375996c31a898fc76edbf69f to your computer and use it in GitHub Desktop.

Select an option

Save smeech/0f5cfc29375996c31a898fc76edbf69f to your computer and use it in GitHub Desktop.
[Parse Help] Espanso: Parse entirety of `espanso --help` recursively #espanso #python
#!/usr/bin/env python3
# A script to parse `espanso --help` recursively throughout all its levels.
import subprocess
import sys
def parse_help(command):
try:
help_text = subprocess.check_output(command + ['--help'], stderr=subprocess.STDOUT, text=True)
except subprocess.CalledProcessError as e:
print(f"Error: Failed to get help for {' '.join(command)}. Make sure the command exists and supports --help option.")
return
print(help_text)
# Parse for subcommands
subcommands = []
lines = iter(help_text.split('\n'))
for line in lines:
if line.strip().startswith('SUBCOMMANDS:'):
subcommand_line = next(lines, '').strip()
while subcommand_line:
subcommand = subcommand_line.split()[0]
subcommands.append(subcommand)
subcommand_line = next(lines, '').strip()
break
# Recursively parse help for subcommands
for subcommand in subcommands:
print(f"\nSubcommand: {subcommand}\n{'='*len(subcommand)}")
parse_help(command + [subcommand])
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <command>")
sys.exit(1)
command = sys.argv[1]
parse_help([command])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment