Advertisement
smeech

parse_help.py

Feb 18th, 2024 (edited)
120
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2. # A script to parse `espanso --help` recursively throughout all its levels.
  3.  
  4. import subprocess
  5. import sys
  6.  
  7. def parse_help(command):
  8.     try:
  9.         help_text = subprocess.check_output(command + ['--help'], stderr=subprocess.STDOUT, text=True)
  10.     except subprocess.CalledProcessError as e:
  11.         print(f"Error: Failed to get help for {' '.join(command)}. Make sure the command exists and supports --help option.")
  12.         return
  13.  
  14.     print(help_text)
  15.  
  16.     # Parse for subcommands
  17.     subcommands = []
  18.     lines = iter(help_text.split('\n'))
  19.     for line in lines:
  20.         if line.strip().startswith('SUBCOMMANDS:'):
  21.             subcommand_line = next(lines, '').strip()
  22.             while subcommand_line:
  23.                 subcommand = subcommand_line.split()[0]
  24.                 subcommands.append(subcommand)
  25.                 subcommand_line = next(lines, '').strip()
  26.             break
  27.  
  28.     # Recursively parse help for subcommands
  29.     for subcommand in subcommands:
  30.         print(f"\nSubcommand: {subcommand}\n{'='*len(subcommand)}")
  31.         parse_help(command + [subcommand])
  32.  
  33. if __name__ == "__main__":
  34.     if len(sys.argv) != 2:
  35.         print("Usage: python script.py <command>")
  36.         sys.exit(1)
  37.  
  38.     command = sys.argv[1]
  39.     parse_help([command])
  40.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement