44# Released subject to the New BSD License
55# Please see http://en.wikipedia.org/wiki/BSD_licenses
66
7+ import argparse
78from getpass import getpass
8- from optparse import OptionParser
99
1010from .config import parse_config_file , create_client_from_config , get_config_defaults
1111
1212
1313def command_line ():
14- p = OptionParser ()
15- p . add_option (
14+ parser = argparse . ArgumentParser ()
15+ parser . add_argument (
1616 "-H" , "--host" , dest = "host" , action = "store" , help = "IMAP host connect to"
1717 )
18- p . add_option (
18+ parser . add_argument (
1919 "-u" ,
2020 "--username" ,
2121 dest = "username" ,
2222 action = "store" ,
2323 help = "Username to login with" ,
2424 )
25- p . add_option (
25+ parser . add_argument (
2626 "-p" ,
2727 "--password" ,
2828 dest = "password" ,
2929 action = "store" ,
3030 help = "Password to login with" ,
3131 )
32- p . add_option (
32+ parser . add_argument (
3333 "-P" ,
3434 "--port" ,
3535 dest = "port" ,
@@ -38,23 +38,25 @@ def command_line():
3838 default = None ,
3939 help = "IMAP port to use (default is 993 for TLS, or 143 otherwise)" ,
4040 )
41- p .add_option (
41+
42+ ssl_group = parser .add_mutually_exclusive_group ()
43+ ssl_group .add_argument (
4244 "-s" ,
4345 "--ssl" ,
4446 dest = "ssl" ,
4547 action = "store_true" ,
4648 default = None ,
4749 help = "Use SSL/TLS connection (default)" ,
4850 )
49- p .add_option (
50- "" ,
51+ ssl_group .add_argument (
5152 "--insecure" ,
5253 dest = "insecure" ,
5354 action = "store_true" ,
5455 default = False ,
5556 help = "Use insecure connection (i.e. without SSL/TLS)" ,
5657 )
57- p .add_option (
58+
59+ parser .add_argument (
5860 "-f" ,
5961 "--file" ,
6062 dest = "file" ,
@@ -63,44 +65,40 @@ def command_line():
6365 help = "Config file (same as livetest)" ,
6466 )
6567
66- opts , args = p .parse_args ()
67- if args :
68- p .error ("unexpected arguments %s" % " " .join (args ))
68+ args = parser .parse_args ()
6969
70- if opts .file :
70+ if args .file :
7171 if (
72- opts .host
73- or opts .username
74- or opts .password
75- or opts .port
76- or opts .ssl
77- or opts .insecure
72+ args .host
73+ or args .username
74+ or args .password
75+ or args .port
76+ or args .ssl
77+ or args .insecure
7878 ):
79- p .error ("If -f/--file is given no other options can be used" )
79+ parser .error ("If -f/--file is given no other options can be used" )
8080 # Use the options in the config file
81- opts = parse_config_file (opts .file )
82- else :
83- if opts .ssl and opts .insecure :
84- p .error ("Can't use --ssl and --insecure at the same time" )
81+ args = parse_config_file (args .file )
82+ return args
8583
86- opts .ssl = not opts .insecure
84+ args .ssl = not args .insecure
8785
88- # Scan through options , filling in defaults and prompting when
89- # a compulsory option wasn't provided.
90- compulsory_opts = ("host" , "username" , "password" )
91- for name , default_value in get_config_defaults ().items ():
92- value = getattr (opts , name , default_value )
93- if name in compulsory_opts and value is None :
94- value = getpass (name + ": " )
95- setattr (opts , name , value )
86+ # Scan through arguments , filling in defaults and prompting when
87+ # a compulsory argument wasn't provided.
88+ compulsory_args = ("host" , "username" , "password" )
89+ for name , default_value in get_config_defaults ().items ():
90+ value = getattr (args , name , default_value )
91+ if name in compulsory_args and value is None :
92+ value = getpass (name + ": " )
93+ setattr (args , name , value )
9694
97- return opts
95+ return args
9896
9997
10098def main ():
101- opts = command_line ()
99+ args = command_line ()
102100 print ("Connecting..." )
103- client = create_client_from_config (opts )
101+ client = create_client_from_config (args )
104102 print ("Connected." )
105103 banner = '\n IMAPClient instance is "c"'
106104
0 commit comments