integrations/twitter: Upgrade to argparse.

This commit is contained in:
Eeshan Garg 2017-08-02 00:51:25 -02:30
parent 3b04b55ffd
commit 18a73324a9
2 changed files with 32 additions and 34 deletions

View file

@ -26,7 +26,7 @@
from __future__ import print_function from __future__ import print_function
import os import os
import sys import sys
import optparse import argparse
from six.moves.configparser import ConfigParser, NoSectionError, NoOptionError from six.moves.configparser import ConfigParser, NoSectionError, NoOptionError
import zulip import zulip
@ -40,9 +40,9 @@ def write_config(config, since_id, user):
with open(CONFIGFILE, 'wb') as configfile: with open(CONFIGFILE, 'wb') as configfile:
config.write(configfile) config.write(configfile)
parser = optparse.OptionParser(r""" parser = zulip.add_default_arguments(argparse.ArgumentParser(r"""
%prog --user foo@example.com --api-key 0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --twitter-id twitter_handle --site=https://zulip.example.com twitter-bot --user foo@example.com --api-key 0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --twitter-id twitter_handle --site=https://zulip.example.com
Slurp tweets on your timeline into a specific zulip stream. Slurp tweets on your timeline into a specific zulip stream.
@ -72,20 +72,19 @@ parser = optparse.OptionParser(r"""
Depends on: https://github.com/bear/python-twitter version 3.1 Depends on: https://github.com/bear/python-twitter version 3.1
(`pip install python-twitter`) (`pip install python-twitter`)
""") """))
parser.add_option('--twitter-id', parser.add_argument('--twitter-id',
help='Twitter username to poll for new tweets from"', help='Twitter username to poll for new tweets from"',
metavar='URL') metavar='URL')
parser.add_option('--stream', parser.add_argument('--stream',
help='Default zulip stream to write tweets to') help='Default zulip stream to write tweets to')
parser.add_option('--limit-tweets', parser.add_argument('--limit-tweets',
default=15, default=15,
type='int', type=int,
help='Maximum number of tweets to push at once') help='Maximum number of tweets to push at once')
parser.add_option_group(zulip.generate_option_group(parser)) options = parser.parse_args()
(options, args) = parser.parse_args()
if not options.twitter_id: if not options.twitter_id:
parser.error('You must specify --twitter-id') parser.error('You must specify --twitter-id')

View file

@ -26,7 +26,7 @@
from __future__ import print_function from __future__ import print_function
import os import os
import sys import sys
import optparse import argparse
from six.moves.configparser import ConfigParser, NoSectionError, NoOptionError from six.moves.configparser import ConfigParser, NoSectionError, NoOptionError
import zulip import zulip
@ -41,9 +41,9 @@ def write_config(config, since_id):
with open(CONFIGFILE, 'wb') as configfile: with open(CONFIGFILE, 'wb') as configfile:
config.write(configfile) config.write(configfile)
parser = optparse.OptionParser(r""" parser = zulip.add_default_arguments(argparse.ArgumentParser(r"""
%prog --user username@example.com --api-key a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 \ twitter-search-bot --user username@example.com --api-key a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 \
--search="@nprnews,quantum physics" --search="@nprnews,quantum physics"
Send Twitter search results to a Zulip stream. Send Twitter search results to a Zulip stream.
@ -89,24 +89,23 @@ new application under your Twitter account:
Make sure to go the application you created and click "create my Make sure to go the application you created and click "create my
access token" as well. Fill in the values displayed. access token" as well. Fill in the values displayed.
""") """))
parser.add_option('--search', parser.add_argument('--search',
dest='search_terms', dest='search_terms',
help='Terms to search on', help='Terms to search on',
action='store') action='store')
parser.add_option('--stream', parser.add_argument('--stream',
dest='stream', dest='stream',
help='The stream to which to send tweets', help='The stream to which to send tweets',
default="twitter", default="twitter",
action='store') action='store')
parser.add_option('--limit-tweets', parser.add_argument('--limit-tweets',
default=15, default=15,
type='int', type=int,
help='Maximum number of tweets to send at once') help='Maximum number of tweets to send at once')
parser.add_option_group(zulip.generate_option_group(parser)) opts = parser.parse_args()
(opts, args) = parser.parse_args()
if not opts.search_terms: if not opts.search_terms:
parser.error('You must specify a search term.') parser.error('You must specify a search term.')