Skip to content

Commit f28067f

Browse files
committed
issue2551195: port scripts to argparse
1 parent 5e1c141 commit f28067f

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Fixed:
4949
Change how in-reply-to threading works in the mailgw. If there is
5050
more than one issue with a matching parent message, fall back to
5151
subject matching. See upgrading.txt for details. (John Rouillard)
52+
- issue2551195 - port scripts from optparse to argparse (Ralf Schlatterbeck)
5253

5354
Features:
5455

scripts/imapServer.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
"""\
33
This script is a wrapper around the mailgw.py script that exists in roundup.
44
It runs as service instead of running as a one-time shot.
@@ -34,17 +34,18 @@
3434
import getpass
3535
import logging
3636
import imaplib
37-
import optparse
37+
import argparse
3838
import os
3939
import re
4040
import time
41+
import sys
4142

4243
from roundup.anypy.my_input import my_input
4344

4445
logging.basicConfig()
4546
log = logging.getLogger('roundup.IMAPServer')
4647

47-
version = '0.1.2'
48+
version = '0.1.3'
4849

4950
class RoundupMailbox:
5051
"""This contains all the info about each mailbox.
@@ -301,10 +302,11 @@ def getItems(s):
301302

302303
def main():
303304
"""This is what is called if run at the prompt"""
304-
parser = optparse.OptionParser(
305-
version=('%prog ' + version),
306-
usage="""usage: %prog [options] (home server)...
305+
parser = argparse.ArgumentParser(
306+
formatter_class=argparse.RawDescriptionHelpFormatter,
307+
epilog="""
307308
309+
The server takes pairs of home/server.
308310
So each entry has a home, and then the server configuration. Home is just
309311
a path to the roundup issue tracker. The server is something of the form:
310312
@@ -315,57 +317,63 @@ def main():
315317
Without mailbox the INBOX is used.
316318
317319
Examples:
318-
%prog /home/roundup/trackers/test imaps://[email protected]/test
319-
%prog /home/roundup/trackers/test imap.example.com \
320-
/home/roundup/trackers/test2 imap.example.com/test2
321-
"""
320+
%(prog)s /home/roundup/trackers/test imaps://[email protected]/test
321+
%(prog)s /home/roundup/trackers/test imap.example.com \\
322+
/home/roundup/trackers/test2 imap.example.com/test2
323+
""" % dict(prog = sys.argv [0])
322324
)
323-
parser.add_option('-d', '--delay', dest='delay', type='float',
325+
parser.add_argument('args', nargs='*')
326+
parser.add_argument('-d', '--delay', dest='delay', type=float,
324327
metavar='<sec>', default=5,
325328
help="Set the delay between checks in minutes. (default 5)"
326329
)
327-
parser.add_option('-p', '--pid-file', dest='pidfile',
330+
parser.add_argument('-p', '--pid-file', dest='pidfile',
328331
metavar='<file>', default=None,
329332
help="The pid of the server process will be written to <file>"
330333
)
331-
parser.add_option('-n', '--no-daemon', dest='daemon',
334+
parser.add_argument('-n', '--no-daemon', dest='daemon',
332335
action='store_false', default=True,
333336
help="Do not fork into the background after running the first check."
334337
)
335-
parser.add_option('-v', '--verbose', dest='verbose',
338+
parser.add_argument('--version', action="store_true",
339+
help="Print version and exit")
340+
parser.add_argument('-v', '--verbose', dest='verbose',
336341
action='store_const', const=logging.INFO,
337342
help="Be more verbose in letting you know what is going on."
338343
" Enables informational messages."
339344
)
340-
parser.add_option('-V', '--very-verbose', dest='verbose',
345+
parser.add_argument('-V', '--very-verbose', dest='verbose',
341346
action='store_const', const=logging.DEBUG,
342347
help="Be very verbose in letting you know what is going on."
343348
" Enables debugging messages."
344349
)
345-
parser.add_option('-q', '--quiet', dest='verbose',
350+
parser.add_argument('-q', '--quiet', dest='verbose',
346351
action='store_const', const=logging.ERROR,
347352
help="Be less verbose. Ignores warnings, only prints errors."
348353
)
349-
parser.add_option('-Q', '--very-quiet', dest='verbose',
354+
parser.add_argument('-Q', '--very-quiet', dest='verbose',
350355
action='store_const', const=logging.CRITICAL,
351356
help="Be much less verbose. Ignores warnings and errors."
352357
" Only print CRITICAL messages."
353358
)
354359

355-
(opts, args) = parser.parse_args()
356-
if (len(args) == 0) or (len(args) % 2 == 1):
360+
args = parser.parse_args()
361+
if args.version:
362+
print('%s %s' % (sys.argv [0], version))
363+
sys.exit(0)
364+
if not len(args.args) or len(args.args) % 2 == 1:
357365
parser.error('Invalid number of arguments. '
358366
'Each site needs a home and a server.')
359367

360-
if opts.verbose == None:
361-
opts.verbose = logging.WARNING
368+
if args.verbose == None:
369+
args.verbose = logging.WARNING
362370

363-
log.setLevel(opts.verbose)
364-
myServer = IMAPServer(delay=opts.delay, pidfile=opts.pidfile,
365-
daemon=opts.daemon)
366-
for i in range(0,len(args),2):
367-
home = args[i]
368-
server = args[i+1]
371+
log.setLevel(args.verbose)
372+
myServer = IMAPServer(delay=args.delay, pidfile=args.pidfile,
373+
daemon=args.daemon)
374+
for i in range(0,len(args.args),2):
375+
home = args.args[i]
376+
server = args.args[i+1]
369377
if not os.path.exists(home):
370378
parser.error('Home: "%s" does not exist' % home)
371379

scripts/schema-dump.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
"""
44
Use recently documented XML-RPC API to dump
@@ -23,7 +23,7 @@
2323
from roundup.anypy import xmlrpc_
2424
import pprint
2525
import textwrap
26-
from optparse import OptionParser
26+
from argparse import ArgumentParser
2727

2828
sname = os.path.basename(sys.argv[0])
2929
usage = """\
@@ -82,25 +82,23 @@ def format_yaml(var):
8282
return out
8383

8484
if __name__ == "__main__":
85-
if len(sys.argv) < 2 or "-h" in sys.argv or "--help" in sys.argv:
86-
sys.exit(usage)
87-
if "--version" in sys.argv:
85+
parser = ArgumentParser()
86+
parser.add_argument("url", nargs=1)
87+
parser.add_argument("--raw", action='store_true')
88+
parser.add_argument("--yaml", action='store_true')
89+
parser.add_argument("--json", action='store_true')
90+
parser.add_argument("-v", "--version", action='store_true')
91+
args = parser.parse_args()
92+
if args.version:
8893
sys.exit(sname + " " + __version__)
8994

90-
parser = OptionParser()
91-
parser.add_option("--raw", action='store_true')
92-
parser.add_option("--yaml", action='store_true')
93-
parser.add_option("--json", action='store_true')
94-
(options, args) = parser.parse_args()
95-
96-
url = args[0]
97-
roundup_server = xmlrpc_.client.ServerProxy(url, allow_none=True)
95+
roundup_server = xmlrpc_.client.ServerProxy(args.url[0], allow_none=True)
9896
schema = roundup_server.schema()
99-
if options.raw:
97+
if args.raw:
10098
print(str(schema))
101-
elif options.yaml:
99+
elif args.yaml:
102100
print(format_yaml(schema))
103-
elif options.json:
101+
elif args.json:
104102
print(format_json(schema))
105103
else:
106104
print(format_pprint(schema))

0 commit comments

Comments
 (0)