From ccd311c519e349e1419c71924eac9a138cd59ada Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 6 Mar 2014 20:21:13 -0800 Subject: [PATCH] irkerd: Transition from getopt to argparse This gives us long options and removes the need to code our own usage string and choice-base argument processing. I also removed the blurb about options from the module docstring, to avoid duplicating information. --- irkerd | 91 +++++++++++++++++++++------------------------------------- 1 file changed, 32 insertions(+), 59 deletions(-) diff --git a/irkerd b/irkerd index 619cdbb..bb23f9a 100755 --- a/irkerd +++ b/irkerd @@ -12,15 +12,12 @@ a list of such strings; in the latter case the message is broadcast to all listed channels. Note that the channel portion of the URL need *not* have a leading '#' unless the channel name itself does. -Options: -d sets the debug-message level (probably only of interest to -developers). -l sets a logfile to capture message traffic from -channels. -n sets the nick and -p the nickserv password. The -V -option prints the program version and exits. - Design and code by Eric S. Raymond . See the project resource page at . -Requires Python 2.6 or 2.5 with the simplejson library installed. +Requires Python 2.7, or: +* 2.6 with the argparse package installed. +* 2.5 with the argparse and simplejson packages installed. """ from __future__ import with_statement @@ -47,7 +44,7 @@ version = "2.6" import Queue import SocketServer -import getopt +import argparse import logging try: import simplejson as json # Faster, also makes us Python-2.4-compatible @@ -876,68 +873,44 @@ class IrkerUDPHandler(SocketServer.BaseRequestHandler): #socket = self.request[1] irker.handle(data) -def usage(): - sys.stdout.write(""" -Usage: - irkerd [-d debuglevel] [-l logfile] [-n nick] [-p password] [-i channel message] [-V] [-h] - -Options - -d set debug level - -l set logfile - -n set nick-style - -p set nickserv password - -i immediate mode - -V return irkerd version - -h print this help dialog -""") if __name__ == '__main__': - log_level = None - immediate = None - nick_template = "irker%03d" - password = None - logfile = None - try: - (options, arguments) = getopt.getopt(sys.argv[1:], "d:i:l:n:p:Vh") - except getopt.GetoptError as e: - sys.stderr.write("%s" % e) - usage() - sys.exit(1) - for (opt, val) in options: - if opt == '-d': # Enable debug/progress messages - if val.lower() not in LOG_LEVELS: - sys.stderr.write('invalid log level %r (choices: %s)\n' % ( - val, ', '.join(LOG_LEVELS))) - sys.exit(1) - log_level = getattr(logging, val.upper()) - elif opt == '-i': # Immediate mode - send one message, then exit. - immediate = val - elif opt == '-l': # Logfile mode - report traffic read in - logfile = val - elif opt == '-n': # Force the nick - nick_template = val - elif opt == '-p': # Set a nickserv password - password = val - elif opt == '-V': # Emit version and exit - sys.stdout.write("irkerd version %s\n" % version) - sys.exit(0) - elif opt == '-h': - usage() - sys.exit(0) + parser = argparse.ArgumentParser( + description=__doc__.strip().splitlines()[0]) + parser.add_argument( + '-d', '--log-level', metavar='LEVEL', choices=LOG_LEVELS, + help='file of trusted certificates for SSL/TLS') + parser.add_argument( + '-l', '--log-file', metavar='PATH', + help='file for saving captured message traffic') + parser.add_argument( + '-n', '--nick', metavar='NAME', default='irker%03d', + help="nickname (optionally with a '%%.*d' server connection marker)") + parser.add_argument( + '-p', '--password', metavar='PASSWORD', + help='NickServ password') + parser.add_argument( + '-i', '--immediate', action='store_const', const=True, + help='disconnect after sending each message') + parser.add_argument( + '-V', '--version', action='version', + version='%(prog)s {0}'.format(version)) + args = parser.parse_args() handler = logging.StreamHandler() LOG.addHandler(handler) - if log_level: + if args.log_level: + log_level = getattr(logging, args.log_level.upper()) LOG.setLevel(log_level) irker = Irker( - logfile=logfile, - nick_template=nick_template, - nick_needs_number=re.search("%.*d", nick_template), - password=password, + logfile=args.log_file, + nick_template=args.nick, + nick_needs_number=re.search('%.*d', args.nick), + password=args.password, ) LOG.info("irkerd version %s" % version) - if immediate: + if args.immediate: irker.irc.add_event_handler("quit", lambda _c, _e: sys.exit(0)) irker.handle('{"to":"%s","privmsg":"%s"}' % (immediate, arguments[0]), quit_after=True) irker.irc.spin() -- 2.26.2