summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2011-08-20 22:09:53 +0200
committerJoel Rosdahl <joel@rosdahl.net>2011-08-21 15:57:19 +0200
commit449da7e66ab11a85b2b7d6684848b050674ed152 (patch)
treeed9b18f55dcf344c3823bd01b712c47cbaa9181d
parentb808981c3abbdec62cb09454dc5851f177845ede (diff)
downloadminiircd-449da7e66ab11a85b2b7d6684848b050674ed152.tar.gz
miniircd-449da7e66ab11a85b2b7d6684848b050674ed152.zip
Use optparse instead of getopt
-rw-r--r--TODO1
-rwxr-xr-xminiircd88
2 files changed, 42 insertions, 47 deletions
diff --git a/TODO b/TODO
deleted file mode 100644
index a645b73..0000000
--- a/TODO
+++ /dev/null
@@ -1 +0,0 @@
-* Use optparse.
diff --git a/miniircd b/miniircd
index 98a076a..354ff65 100755
--- a/miniircd
+++ b/miniircd
@@ -22,7 +22,6 @@
version = "0.2.0"
-import getopt
import os
import select
import socket
@@ -30,6 +29,7 @@ import string
import sys
import re
import time
+from optparse import OptionParser
class Channel(object):
def __init__(self, server, name):
@@ -823,51 +823,47 @@ def display_usage():
print " -v, --verbose Be verbose (print some progress messages on stdout)."
def main(argv):
- try:
- optlist, arguments = getopt.getopt(
- argv[1:],
- "dhp:v",
- ["daemon",
- "debug",
- "help",
- "motd=",
- "password=",
- "ports=",
- "verbose"])
- except getopt.error, x:
- sys.stderr.write("Bad arguments: %s.\n" % x)
- sys.exit(17)
- motd = None
- password = None
- ports = [6667]
- verbose = False
- daemon = False
- debug = False
- for opt, val in optlist:
- if opt in ("-d", "--daemon"):
- daemon = True
- elif opt == "--debug":
- debug = True
- verbose = True
- elif opt in ("-h", "--help"):
- display_usage()
- sys.exit(0)
- elif opt == "--motd":
- motd = val
- elif opt in ("-p", "--password"):
- password = val.lower()
- elif opt == "--ports":
- ports = []
- for port in re.split("[,\s]+", val):
- try:
- ports.append(int(port))
- except ValueError:
- sys.stderr.write("Bad port: \"%s\".\n" % port)
- sys.exit(1)
- elif opt in ("-v", "--verbose"):
- verbose = True
- server = Server(ports, password, motd, verbose, debug)
- if daemon:
+ op = OptionParser(
+ version=version,
+ description="miniircd is a small and limited IRC server.")
+ op.add_option(
+ "-d", "--daemon",
+ action="store_true",
+ help="fork and become a daemon")
+ op.add_option(
+ "--debug",
+ action="store_true",
+ help="print debug messages to stdout")
+ op.add_option(
+ "--motd",
+ metavar="X",
+ help="display file X as message of the day")
+ op.add_option(
+ "-p", "--password",
+ metavar="X",
+ help="require connection password X; default: no password")
+ op.add_option(
+ "--ports",
+ metavar="X",
+ help="listen to ports X (a list separated by comma or whitespace);"
+ " default: 6667.")
+ op.add_option(
+ "--verbose",
+ action="store_true",
+ help="be verbose (print some progress messages to stdout)")
+ op.set_defaults(ports="6667")
+ (options, args) = op.parse_args(argv[1:])
+ if options.debug:
+ options.verbose = True
+ ports = []
+ for port in re.split(r"[,\s]+", options.ports):
+ try:
+ ports.append(int(port))
+ except ValueError:
+ op.error("bad port: %r" % port)
+ server = Server(
+ ports, options.password, options.motd, options.verbose, options.debug)
+ if options.daemon:
server.daemonize()
try:
server.start()