Skip to content

Instantly share code, notes, and snippets.

@binarycat0
Created April 13, 2020 16:15
Show Gist options
  • Save binarycat0/a56dd4a8de310f230ddeab08f913309a to your computer and use it in GitHub Desktop.
Save binarycat0/a56dd4a8de310f230ddeab08f913309a to your computer and use it in GitHub Desktop.
import argparse
import json
import subprocess
from collections import defaultdict
def _execute(*args) -> str:
p = subprocess.Popen(args, stdout=subprocess.PIPE)
res, _ = p.communicate()
return res.decode('utf8')
def get_command_help(cli: str, cmd: str) -> str:
return _execute(cli, 'help', cmd)
def get_help(cli: str) -> str:
return _execute(cli, 'help')
def main(cli: str, blockchain_name: str, version: str):
main_help = get_help(cli)
result = {blockchain_name: {version: defaultdict(lambda: dict())}}
cmd_group = None
for line in main_help.split('\n'):
if not line:
continue
# define group
if '==' in line:
cmd_group = line.replace('=', '').strip()
continue
# define command
cmd = line.split(' ')[0].strip()
result[blockchain_name][version][cmd_group][cmd] = get_command_help(cli, cmd)
# save result
json.dump(result, open(f'{cli}-{version}.json', 'w'))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('blockchain_cli', type=str, help='blockchain cli')
parser.add_argument('blockchain', type=str, help='blockchain name')
parser.add_argument('version', type=str, help='version')
args = parser.parse_args()
main(args.blockchain_cli, args.blockchain, args.version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment