From 0125c544ee8e66049e63dced98e9837476a604d4 Mon Sep 17 00:00:00 2001 From: James Lu Date: Tue, 21 Feb 2017 21:45:43 -0800 Subject: [PATCH] utils: add an IRCParser class based off argparse, modified from @IotaSpencer's code Closes #6. --- utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/utils.py b/utils.py index 01bcab1..c4b85ff 100644 --- a/utils.py +++ b/utils.py @@ -10,6 +10,7 @@ import re import importlib import os import collections +import argparse from .log import log from . import world, conf @@ -27,6 +28,11 @@ class NotAuthorizedError(Exception): """ pass +class InvalidArgumentsError(TypeError): + """ + Exception raised (by IRCParser and potentially others) when a bot command is given invalid arguments. + """ + class IncrementalUIDGenerator(): """ Incremental UID Generator module, adapted from InspIRCd source: @@ -547,3 +553,11 @@ def wrapArguments(prefix, args, length, separator=' ', max_args_per_line=0): strings.append(buf) return strings + +class IRCParser(argparse.ArgumentParser): + """ + Wrapper around argparse.ArgumentParser, without quitting on usage errors. + """ + + def error(self, message): + raise InvalidArgumentsError(message)