irccat: handle bad options a little more gently

This commit is contained in:
Alec Leamas 2013-03-19 11:04:21 +01:00
parent 6cae3bbac9
commit d70083a558
1 changed files with 14 additions and 10 deletions

24
irccat
View File

@ -1,9 +1,7 @@
#!/usr/bin/env python
usage = """
Send data to IRC channel
usage: irccat [-s] <host> <port> <section> <text...>
Usage: irccat [-s] <host> <port> <section> <text...>
host: supybot host running irccat plugin.
port The port irccat plugin listen to.
@ -24,6 +22,13 @@ import os
import sys
import socket
def error(why):
print "Error: " + why
print "Use -h for help"
sys.exit(1)
sys.argv.pop(0)
try:
if sys.argv[0] == '-h' or sys.argv[0] == '--help':
@ -35,18 +40,17 @@ try:
elif 'IRCCAT_PASSWORD' in os.environ:
pw = os.environ['IRCCAT_PASSWORD']
else:
print usage
sys.exit(1)
error("neither -s nor IRCCAT_PASSWORD present.")
host = sys.argv.pop(0)
port = int(sys.argv.pop(0))
section = sys.argv.pop(0)
except (IndexError, ValueError):
print usage
sys.exit(1)
except ValueError:
error("illegal port number.")
except IndexError:
error("too few arguments.")
text = ' '.join(sys.argv)
if not text:
print usage
sys.exit(1)
error("too few arguments.")
s = socket.create_connection((host, port))
s.send('%s;%s;%s\n' % (section, pw, text))