From 4a76284af11a805b897bc824d9a930ca30509766 Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Wed, 26 Jul 2017 23:09:28 -0230 Subject: [PATCH] zulip/examples: Use argparse-based zulip.add_default_arguments. --- zulip/examples/create-user | 15 +++++++-------- zulip/examples/edit-message | 13 ++++++------- zulip/examples/get-presence | 9 ++++----- zulip/examples/get-public-streams | 7 +++---- zulip/examples/list-members | 7 +++---- zulip/examples/list-subscriptions | 7 +++---- zulip/examples/print-events | 7 +++---- zulip/examples/print-messages | 7 +++---- zulip/examples/recent-messages | 9 ++++----- zulip/examples/send-message | 20 +++++++++----------- zulip/examples/subscribe | 9 ++++----- zulip/examples/unsubscribe | 9 ++++----- zulip/examples/upload-file | 9 ++++----- 13 files changed, 57 insertions(+), 71 deletions(-) diff --git a/zulip/examples/create-user b/zulip/examples/create-user index cd33761..f6ec2fb 100755 --- a/zulip/examples/create-user +++ b/zulip/examples/create-user @@ -24,7 +24,7 @@ from __future__ import print_function import sys from os import path -import optparse +import argparse usage = """create-user --new-email= --new-password= --new-full-name= --new-short-name= [options] @@ -37,13 +37,12 @@ Example: create-user --site=http://localhost:9991 --user=rwbarton@example.com -- sys.path.append(path.join(path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option_group(zulip.generate_option_group(parser)) -parser.add_option('--new-email') -parser.add_option('--new-password') -parser.add_option('--new-full-name') -parser.add_option('--new-short-name') -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +parser.add_argument('--new-email') +parser.add_argument('--new-password') +parser.add_argument('--new-full-name') +parser.add_argument('--new-short-name') +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/edit-message b/zulip/examples/edit-message index 0988bf6..cc28228 100755 --- a/zulip/examples/edit-message +++ b/zulip/examples/edit-message @@ -24,7 +24,7 @@ from __future__ import print_function import sys import os -import optparse +import argparse usage = """edit-message [options] --message= --subject= --content= --user= --api-key= @@ -38,12 +38,11 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option('--message-id', default="") -parser.add_option('--subject', default="") -parser.add_option('--content', default="") -parser.add_option_group(zulip.generate_option_group(parser)) -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +parser.add_argument('--message-id', default="") +parser.add_argument('--subject', default="") +parser.add_argument('--content', default="") +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/get-presence b/zulip/examples/get-presence index d5008ff..10144ea 100755 --- a/zulip/examples/get-presence +++ b/zulip/examples/get-presence @@ -24,7 +24,7 @@ from __future__ import print_function import sys from os import path -import optparse +import argparse usage = """get-presence --email= [options] @@ -34,10 +34,9 @@ Get presence data for another user. sys.path.append(path.join(path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option_group(zulip.generate_option_group(parser)) -parser.add_option('--email') -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +parser.add_argument('--email') +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/get-public-streams b/zulip/examples/get-public-streams index 7ec65cd..4ef8f42 100755 --- a/zulip/examples/get-public-streams +++ b/zulip/examples/get-public-streams @@ -24,7 +24,7 @@ from __future__ import print_function import sys import os -import optparse +import argparse usage = """get-public-streams --user= --api-key= [options] @@ -38,9 +38,8 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option_group(zulip.generate_option_group(parser)) -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/list-members b/zulip/examples/list-members index acf2d23..a5bc945 100755 --- a/zulip/examples/list-members +++ b/zulip/examples/list-members @@ -24,7 +24,7 @@ from __future__ import print_function import sys import os -import optparse +import argparse usage = """list-members --user= --api-key= [options] @@ -36,9 +36,8 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option_group(zulip.generate_option_group(parser)) -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/list-subscriptions b/zulip/examples/list-subscriptions index 84dd17d..e17ac85 100755 --- a/zulip/examples/list-subscriptions +++ b/zulip/examples/list-subscriptions @@ -24,7 +24,7 @@ from __future__ import print_function import sys import os -import optparse +import argparse usage = """list-subscriptions --user= --api-key= [options] @@ -37,9 +37,8 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option_group(zulip.generate_option_group(parser)) -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/print-events b/zulip/examples/print-events index b74c13a..b4f8f3e 100755 --- a/zulip/examples/print-events +++ b/zulip/examples/print-events @@ -24,7 +24,7 @@ from __future__ import print_function import sys import os -import optparse +import argparse from typing import Any, Dict @@ -39,9 +39,8 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option_group(zulip.generate_option_group(parser)) -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/print-messages b/zulip/examples/print-messages index 515b6d9..4f734eb 100755 --- a/zulip/examples/print-messages +++ b/zulip/examples/print-messages @@ -24,7 +24,7 @@ from __future__ import print_function import sys import os -import optparse +import argparse from typing import Any, Dict @@ -39,9 +39,8 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option_group(zulip.generate_option_group(parser)) -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/recent-messages b/zulip/examples/recent-messages index 457fb49..b7df950 100755 --- a/zulip/examples/recent-messages +++ b/zulip/examples/recent-messages @@ -25,7 +25,7 @@ from __future__ import print_function import sys import os import json -import optparse +import argparse usage = """recent-messages [options] --count= --user= --api-key= @@ -38,10 +38,9 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option('--count', default=100) -parser.add_option_group(zulip.generate_option_group(parser)) -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +parser.add_argument('--count', default=100) +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/send-message b/zulip/examples/send-message index dce615e..1e8f4b0 100755 --- a/zulip/examples/send-message +++ b/zulip/examples/send-message @@ -24,8 +24,9 @@ from __future__ import print_function import sys import os -import optparse sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +import argparse + import zulip usage = """send-message --user= --api-key= [options] @@ -37,15 +38,12 @@ Example: send-message --user=your-bot@example.com --api-key=a0b1c2d3e4f5a6b7c8d9 You can omit --user and --api-key arguments if you have a properly set up ~/.zuliprc """ -parser = optparse.OptionParser(usage=usage) -parser.add_option('--subject', default="test") -parser.add_option('--message', default="test message") -parser.add_option('--type', default='private') -parser.add_option_group(zulip.generate_option_group(parser)) -(options, args) = parser.parse_args() - -if len(args) == 0: - parser.error("You must specify recipients") +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +parser.add_argument('recipients', nargs='*') +parser.add_argument('--subject', default="test") +parser.add_argument('--message', default="test message") +parser.add_argument('--type', default='private') +options = parser.parse_args() client = zulip.init_from_options(options) @@ -53,6 +51,6 @@ message_data = { "type": options.type, "content": options.message, "subject": options.subject, - "to": args, + "to": options.recipients, } print(client.send_message(message_data)) diff --git a/zulip/examples/subscribe b/zulip/examples/subscribe index 9c2d92e..d7cf954 100755 --- a/zulip/examples/subscribe +++ b/zulip/examples/subscribe @@ -24,7 +24,7 @@ from __future__ import print_function import sys import os -import optparse +import argparse usage = """subscribe --user= --api-key= [options] --streams= @@ -38,10 +38,9 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option_group(zulip.generate_option_group(parser)) -parser.add_option('--streams', default='') -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +parser.add_argument('--streams', default='') +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/unsubscribe b/zulip/examples/unsubscribe index 1c3b12c..030b559 100755 --- a/zulip/examples/unsubscribe +++ b/zulip/examples/unsubscribe @@ -24,7 +24,7 @@ from __future__ import print_function import sys import os -import optparse +import argparse usage = """unsubscribe --user= --api-key= [options] --streams= @@ -38,10 +38,9 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import zulip -parser = optparse.OptionParser(usage=usage) -parser.add_option_group(zulip.generate_option_group(parser)) -parser.add_option('--streams', default='') -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +parser.add_argument('--streams', default='') +options = parser.parse_args() client = zulip.init_from_options(options) diff --git a/zulip/examples/upload-file b/zulip/examples/upload-file index 614f7ac..a6cb36c 100755 --- a/zulip/examples/upload-file +++ b/zulip/examples/upload-file @@ -23,8 +23,8 @@ from __future__ import print_function import importlib -import optparse import sys +import argparse from six.moves import StringIO as _StringIO sys.path.insert(0, './api') @@ -44,10 +44,9 @@ You can omit --user and --api-key arguments if you have a properly set up ~/.zul If no --file-path is specified, a placeholder text file will be used instead. """ -parser = optparse.OptionParser(usage=usage) -parser.add_option('--file-path') -parser.add_option_group(zulip.generate_option_group(parser)) -(options, args) = parser.parse_args() +parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage)) +parser.add_argument('--file-path') +options = parser.parse_args() client = zulip.init_from_options(options)