flat.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. import json
  3. import os
  4. import sys
  5. sys.path.insert(0, os.getcwd())
  6. import clap
  7. argv = sys.argv[:]
  8. base, filename = os.path.split(argv.pop(0))
  9. filename_ui = os.path.splitext(filename)[0] + '.json'
  10. ifstream = open(os.path.join(base, filename_ui), 'r')
  11. model = json.loads(ifstream.read())
  12. ifstream.close()
  13. args = list(clap.formatter.Formatter(argv).format())
  14. mode = clap.builder.Builder(model).build().get()
  15. parser = clap.parser.Parser(mode).feed(args)
  16. checker = clap.checker.RedChecker(parser)
  17. try:
  18. checker.check()
  19. fail = False
  20. except clap.errors.MissingArgumentError as e:
  21. print('missing argument for option: {0}'.format(e))
  22. fail = True
  23. except clap.errors.UnrecognizedOptionError as e:
  24. print('unrecognized option found: {0}'.format(e))
  25. fail = True
  26. except clap.errors.ConflictingOptionsError as e:
  27. print('conflicting options found: {0}'.format(e))
  28. fail = True
  29. except clap.errors.RequiredOptionNotFoundError as e:
  30. fail = True
  31. print('required option not found: {0}'.format(e))
  32. except clap.errors.InvalidOperandRangeError as e:
  33. print('invalid number of operands: {0}'.format(e))
  34. fail = True
  35. except clap.errors.UIDesignError as e:
  36. print('UI has design error: {0}'.format(e))
  37. fail = True
  38. except Exception as e:
  39. print('fatal: unhandled exception: {0}'.format(e))
  40. fail = True
  41. finally:
  42. if fail: exit()
  43. else: ui = parser.parse().ui().finalise()
  44. if '--version' in ui:
  45. print('using clap (RedCLAP) version {0}'.format(clap.__version__))
  46. exit()
  47. if '--help' in ui:
  48. helper = clap.helper.Helper(filename, mode)
  49. print(helper.full(deep=('--verbose' in ui)).render())
  50. exit(0)
  51. if '--echo' in ui:
  52. print(ui.get('-e'))
  53. for msg in ui.get('-e'):
  54. print(msg[0])