zulip/examples: Use argparse-based zulip.add_default_arguments.
This commit is contained in:
		
							parent
							
								
									38df8ef87b
								
							
						
					
					
						commit
						4a76284af1
					
				
					 13 changed files with 57 additions and 71 deletions
				
			
		| 
						 | 
				
			
			@ -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=<bot's email address> --api-key=<bot's api key> [options] <recipients>
 | 
			
		||||
| 
						 | 
				
			
			@ -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))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue