Kodi plugin to to play various online streams (mostly Latvian)

cli.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. """
  3. kodiswift.cli.cli
  4. ------------------
  5. The main entry point for the kodiswift console script. CLI commands can be
  6. registered in this module.
  7. :copyright: (c) 2012 by Jonathan Beluch
  8. :license: GPLv3, see LICENSE for more details.
  9. """
  10. from __future__ import absolute_import
  11. import sys
  12. from optparse import OptionParser
  13. from kodiswift.cli.app import RunCommand
  14. from kodiswift.cli.create import CreateCommand
  15. # TODO: Make an ABC for Command
  16. COMMANDS = {
  17. RunCommand.command: RunCommand,
  18. CreateCommand.command: CreateCommand,
  19. }
  20. # TODO: Make this usage dynamic based on COMMANDS dict
  21. USAGE = """%prog <command>
  22. Commands:
  23. create
  24. Create a new plugin project.
  25. run
  26. Run an kodiswift plugin from the command line.
  27. Help:
  28. To see options for a command, run `kodiswift <command> -h`
  29. """
  30. def main():
  31. """The entry point for the console script kodiswift.
  32. The 'xbcmswift2' script is command bassed, so the second argument is always
  33. the command to execute. Each command has its own parser options and usages.
  34. If no command is provided or the -h flag is used without any other
  35. commands, the general help message is shown.
  36. """
  37. parser = OptionParser()
  38. if len(sys.argv) == 1:
  39. parser.set_usage(USAGE)
  40. parser.error('At least one command is required.')
  41. # spy sys.argv[1] in order to use correct opts/args
  42. command = sys.argv[1]
  43. if command == '-h':
  44. parser.set_usage(USAGE)
  45. opts, args = parser.parse_args()
  46. if command not in COMMANDS.keys():
  47. parser.error('Invalid command')
  48. # We have a proper command, set the usage and options list according to the
  49. # specific command
  50. manager = COMMANDS[command]
  51. if hasattr(manager, 'option_list'):
  52. for args, kwargs in manager.option_list:
  53. parser.add_option(*args, **kwargs)
  54. if hasattr(manager, 'usage'):
  55. parser.set_usage(manager.usage)
  56. opts, args = parser.parse_args()
  57. # Since we are calling a specific comamnd's manager, we no longer need the
  58. # actual command in sys.argv so we slice from position 1
  59. manager.run(opts, args[1:])