nested.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. command = clap.builder.Builder(model).insertHelpCommand().build().get()
  15. parser = clap.parser.Parser(command).feed(args)
  16. checker = clap.checker.RedChecker(parser)
  17. try:
  18. err = None
  19. checker.check()
  20. fail = False
  21. except clap.errors.MissingArgumentError as e:
  22. print('missing argument for option: {0}'.format(e))
  23. fail = True
  24. except clap.errors.UnrecognizedOptionError as e:
  25. print('unrecognized option found: {0}'.format(e))
  26. fail = True
  27. except clap.errors.ConflictingOptionsError as e:
  28. print('conflicting options found: {0}'.format(e))
  29. fail = True
  30. except clap.errors.RequiredOptionNotFoundError as e:
  31. fail = True
  32. print('required option not found: {0}'.format(e))
  33. except clap.errors.InvalidOperandRangeError as e:
  34. print('invalid number of operands: {0}'.format(e))
  35. fail = True
  36. except clap.errors.UIDesignError as e:
  37. print('UI has design error: {0}'.format(e))
  38. fail = True
  39. except Exception as e:
  40. print('fatal: unhandled exception: {0}: {1}'.format(str(type(e))[8:-2], e))
  41. fail, err = True, e
  42. finally:
  43. if fail:
  44. if err is not None: raise err
  45. else: exit()
  46. else: ui = parser.parse().ui().finalise()
  47. if '--version' in ui:
  48. print('using clap (RedCLAP) version {0}'.format(clap.__version__))
  49. exit()
  50. if clap.helper.HelpRunner(ui=ui, program=filename).adjust(options=['-h', '--help']).run().displayed(): exit(0)