From d8caed033ee5e34e6ef4bb2aaf42ba88e342cba6 Mon Sep 17 00:00:00 2001 From: iou1name Date: Wed, 29 Jan 2020 14:15:43 -0500 Subject: [PATCH] argparse no longer prints to console --- modules/ascii.py | 12 ++++++------ modules/resistor.py | 9 ++++++--- modules/seen.py | 9 ++++++--- modules/slur.py | 9 ++++++--- modules/translate.py | 9 ++++++--- tools/__init__.py | 14 ++++++++++++++ 6 files changed, 44 insertions(+), 18 deletions(-) diff --git a/modules/ascii.py b/modules/ascii.py index b3e561a..dc490c4 100755 --- a/modules/ascii.py +++ b/modules/ascii.py @@ -3,7 +3,6 @@ ASCII """ from io import BytesIO -import argparse import requests from PIL import Image, ImageFont, ImageDraw @@ -11,6 +10,7 @@ from PIL import Image, ImageFont, ImageDraw import numpy as np import numpngw +import tools import module ASCII_CHARS = "$@%#*+=-:. " @@ -235,7 +235,7 @@ def ascii(bot, trigger): """ if len(trigger.args) < 2: return bot.msg() - parser = argparse.ArgumentParser(add_help=False) + parser = tools.FulviaArgparse() parser.add_argument("imagePath") parser.add_argument("-r", "--reverse", action="store_true", help="Reverse.") parser.add_argument("-c", "--color", action="store_true") @@ -243,10 +243,10 @@ def ascii(bot, trigger): parser.add_argument("-B", "--brail2", action="store_true") parser.add_argument("-a", "--animated", action="store_true") parser.add_argument("-h", "--help", action="store_true") - args = parser.parse_args(trigger.args[1:]) - - if args.help: - return bot.msg(parser.print_help()) + try: + args = parser.parse_args(trigger.args[1:]) + except Exception as e: + return bot.reply(type(e).__name__ + ": " + str(e)) if args.color: args.color = "irc" diff --git a/modules/resistor.py b/modules/resistor.py index 5ec2ccb..f228e18 100755 --- a/modules/resistor.py +++ b/modules/resistor.py @@ -3,8 +3,8 @@ Resistor color band codes. """ import re -import argparse +import tools from module import commands, example suffix = {"k": 1000, "m": 10000000} @@ -62,11 +62,14 @@ def resist(bot, trigger): """ if len(trigger.args) < 2: return bot.msg("Please specify a value") - parser = argparse.ArgumentParser() + parser = tools.FulviaArgparse() parser.add_argument("value", nargs="+") parser.add_argument("-r", "--reverse", action="store_true") parser.add_argument("-n", "--num_bands", type=int, choices=[3,4,5,6], default=4) - args = parser.parse_args(trigger.args[1:]) + try: + args = parser.parse_args(trigger.args[1:]) + except Exception as e: + return bot.reply(type(e).__name__ + ": " + str(e)) if args.reverse: # bands-to-value bot.msg(bands_to_value(" ".join(args.value))) diff --git a/modules/seen.py b/modules/seen.py index bdc058f..1fc9df7 100755 --- a/modules/seen.py +++ b/modules/seen.py @@ -4,13 +4,13 @@ When was this user last seen. """ import os import time -import argparse import threading from datetime import datetime from sqlite3 import OperationalError from requests.structures import CaseInsensitiveDict +import tools import config from tools.time import relativeTime from module import commands, example, hook, require_chanmsg, rate @@ -68,13 +68,16 @@ def seen(bot, trigger): if len(trigger.args) < 2: return bot.reply("Seen who?") - parser = argparse.ArgumentParser() + parser = tools.FulviaArgparse() parser.add_argument("nick") parser.add_argument("-l", "--last", action="store_true", default=True) parser.add_argument("-f", "--first", action="store_true") parser.add_argument("-m", "--message", action="store_true") parser.add_argument("-c", "--context", action="store_true") - args = parser.parse_args(trigger.args[1:]) + try: + args = parser.parse_args(trigger.args[1:]) + except Exception as e: + return bot.reply(type(e).__name__ + ": " + str(e)) if args.nick == bot.nick: return bot.reply("I'm right here!") diff --git a/modules/slur.py b/modules/slur.py index 2cb2892..106ef7f 100755 --- a/modules/slur.py +++ b/modules/slur.py @@ -5,8 +5,8 @@ All data comes from http://www.rsdb.org/. """ import random import sqlite3 -import argparse +import tools from module import commands @@ -33,11 +33,14 @@ def slur(bot, trigger): -s, --slur - specifies a particular slur to pick. -l, --list - prints a list of available races. """ - parser = argparse.ArgumentParser() + parser = tools.FulviaArgparse() parser.add_argument("-r", "--race", type=str, nargs='+') parser.add_argument("-s", "--slur", type=str) parser.add_argument("-l", "--list", action="store_true") - args = parser.parse_args(trigger.args[1:]) + try: + args = parser.parse_args(trigger.args[1:]) + except Exception as e: + return bot.reply(type(e).__name__ + ": " + str(e)) if args.list: races = bot.db.execute("SELECT DISTINCT race FROM slur").fetchall() diff --git a/modules/translate.py b/modules/translate.py index 97291d6..89e200d 100755 --- a/modules/translate.py +++ b/modules/translate.py @@ -3,10 +3,10 @@ Google translate that shit. """ import random -import argparse import requests +import tools from module import commands, example @@ -45,11 +45,14 @@ def tr2(bot, trigger): if len(trigger.args) < 2: return bot.reply("Translate what?") - parser = argparse.ArgumentParser() + parser = tools.FulviaArgparse() parser.add_argument("text", nargs=argparse.REMAINDER) parser.add_argument("-i", "--inlang", default="auto") parser.add_argument("-o", "--outlang", default="en") - args = parser.parse_args(trigger.args[1:]) + try: + args = parser.parse_args(trigger.args[1:]) + except Exception as e: + return bot.reply(type(e).__name__ + ": " + str(e)) args.text = " ".join(args.text) tr_text, in_lang = translate(args.text, in_lang=args.inlang, diff --git a/tools/__init__.py b/tools/__init__.py index afdd6bf..c7b1a68 100755 --- a/tools/__init__.py +++ b/tools/__init__.py @@ -3,6 +3,7 @@ Some helper functions and other tools. """ import re +import argparse import threading from collections import defaultdict @@ -199,3 +200,16 @@ def getOpSym(level): return "+" else: return " " + + +class FulviaArgparse(argparse.ArgumentParser): + """ + A custom ArgParser class that raises errors as exceptions rather than + printing them to stderr. + """ + def __init__(self, *args, **kwargs): + kwargs['add_help'] = False + super().__init__(*args, **kwargs) + + def error(self, message): + raise argparse.ArgumentError(None, message)