parse_commands.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from dataclasses import dataclass
  2. import yaml
  3. @dataclass()
  4. class Command:
  5. name: str
  6. docstring: str | None = None
  7. signature: str | None = None
  8. def parse_command_file(filepath: str) -> str:
  9. content = open(filepath, 'r').read()
  10. lines = content.split('\n')
  11. commands: list[Command] = []
  12. idx = 0
  13. docs: list[str] = []
  14. while idx < len(lines):
  15. line = lines[idx]
  16. idx += 1
  17. if line.startswith('# '):
  18. docs.append(line[2:])
  19. elif line.strip().endswith('() {'):
  20. name = line.split()[0][:-2]
  21. while lines[idx].strip() != '}':
  22. idx += 1
  23. docstring, signature = None, name
  24. docs_dict = yaml.safe_load('\n'.join(docs).replace('@yaml', ''))
  25. if docs_dict is not None:
  26. docstring = docs_dict.get('docstring')
  27. arguments = docs_dict.get('arguments', None)
  28. if 'signature' in docs_dict:
  29. signature = docs_dict['signature']
  30. else:
  31. if arguments is not None:
  32. for param, settings in arguments.items():
  33. if 'required' in settings:
  34. signature += f' <{param}>'
  35. else:
  36. signature += f' [<{param}>]'
  37. command = Command(name, docstring, signature)
  38. commands.append(command)
  39. docs = []
  40. function_docs = ''
  41. for cmd in commands:
  42. if cmd.docstring is not None:
  43. function_docs += f'{cmd.signature or cmd.name} - {cmd.docstring}\n'
  44. return function_docs
  45. if __name__ == '__main__':
  46. import sys
  47. if len(sys.argv) < 2:
  48. print('Usage: python parse_commands.py <file>')
  49. sys.exit(1)
  50. filepath = sys.argv[1]
  51. filepaths = filepath.split(',')
  52. for filepath in filepaths:
  53. docs = parse_command_file(filepath)
  54. print(docs)