zulip_bots/run.py: Use argparse instead of optparse.

The optparse module is deprecated.
This commit is contained in:
Eeshan Garg 2017-07-23 15:36:10 -02:30 committed by Tim Abbott
parent e255c73fc3
commit e85be28119

View file

@ -3,7 +3,7 @@ from __future__ import print_function
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import optparse import argparse
import sys import sys
from types import ModuleType from types import ModuleType
from importlib import import_module from importlib import import_module
@ -43,25 +43,31 @@ def parse_args():
See lib/readme.md for more context. See lib/readme.md for more context.
''' '''
parser = optparse.OptionParser(usage=usage) parser = argparse.ArgumentParser(usage=usage)
parser.add_option('--quiet', '-q', parser.add_argument('name',
action='store',
nargs='?',
default=None,
help='the name of an existing bot to run')
parser.add_argument('--quiet', '-q',
action='store_true', action='store_true',
help='Turn off logging output.') help='Turn off logging output.')
parser.add_option('--config-file', parser.add_argument('--config-file',
action='store', action='store',
help='(alternate config file to ~/.zuliprc)') help='(alternate config file to ~/.zuliprc)')
parser.add_option('--path-to-bot', parser.add_argument('--path-to-bot',
action='store', action='store',
help='path to the file with the bot handler class') help='path to the file with the bot handler class')
parser.add_option('--force', parser.add_argument('--force',
action='store_true', action='store_true',
help='Try running the bot even if dependencies install fails.') help='Try running the bot even if dependencies install fails.')
(options, args) = parser.parse_args() options = parser.parse_args()
if not args and not options.path_to_bot: if not options.name and not options.path_to_bot:
error_message = """ error_message = """
You must either specify the name of an existing bot or You must either specify the name of an existing bot or
specify a path to the file (--path-to-bot) that contains specify a path to the file (--path-to-bot) that contains
@ -69,21 +75,17 @@ the bot handler class.
""" """
parser.error(error_message) parser.error(error_message)
return (options, args) return options
def main(): def main():
# type: () -> None # type: () -> None
(options, args) = parse_args() options = parse_args()
bot_name = None
if args:
bot_name = args[0]
if options.path_to_bot: if options.path_to_bot:
lib_module = import_module_from_source(options.path_to_bot, name=bot_name) lib_module = import_module_from_source(options.path_to_bot, name=options.name)
else: else:
lib_module = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot=bot_name)) lib_module = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot=options.name))
if not options.quiet: if not options.quiet:
logging.basicConfig(stream=sys.stdout, level=logging.INFO) logging.basicConfig(stream=sys.stdout, level=logging.INFO)