#!python

'''
Usage:
  mdoc
  mdoc <command>

'''

import os, sys
import json
import docopt
from collections import namedtuple
from mercury import mercury_docs


DocEntry = namedtuple('DocEntry', 'syntax manual')


def main(args):

  if not args.get('<command>'):
    print('\n'.join(mercury_docs.SCRIPT_NAMES))

  else:
    command = args.get('<command>', '')
    if command.endswith('?'):
      search_exp = command.rstrip('?')
      for name in mercury_docs.SCRIPT_NAMES:
        if name.startswith(search_exp):
          print(name)

    elif command.startswith('?'):
      search_exp = command.lstrip('?')
      for name in mercury_docs.SCRIPT_NAMES:
        if name.endswith(search_exp):
          print(name)

    else:
      if command in mercury_docs.SCRIPT_NAMES:
        print(mercury_docs.DOC_REGISTRY[command])
      else:
        print(f"""
        \nThere is no Mercury command "{command}".\nAppend "?" to a string to list all commands which start with that string.\nPrepend "?" to a string to list all commands which end with that string.\n""")

if __name__ == '__main__':
  args = docopt.docopt(__doc__)
  main(args)

