using argparse to parse flags

This commit is contained in:
iou1name 2017-11-27 01:18:22 -05:00
parent ffd5a62241
commit 0e482e413f

View File

@ -4,6 +4,7 @@
ASCII ASCII
""" """
from io import BytesIO from io import BytesIO
import argparse
import requests import requests
from PIL import Image, ImageFont, ImageDraw from PIL import Image, ImageFont, ImageDraw
@ -86,10 +87,10 @@ def char_color(pixel, code="irc"):
"light blue": (65,105,225), "pink":(255,192,203), "grey": (128,128,128), "light blue": (65,105,225), "pink":(255,192,203), "grey": (128,128,128),
"silver": (192,192,192)} "silver": (192,192,192)}
colors_irc = {"white": "0", "black": "1", "blue": "2", "green": "3", "red": "4", colors_irc = {"white": "0", "black": "1", "blue": "2", "green": "3",
"brown": "5", "purple": "6", "orange": "7", "yellow": "8", "light green": "9", "red": "4", "brown": "5", "purple": "6", "orange": "7", "yellow": "8",
"teal": "10", "cyan": "11", "light blue": "12", "pink": "13", "grey": "14", "light green": "9", "teal": "10", "cyan": "11", "light blue": "12",
"silver": "15"} "pink": "13", "grey": "14", "silver": "15"}
colors_ansi = {"white": "[1;37m", "black": "[0;30m", "blue": "[0;34m", colors_ansi = {"white": "[1;37m", "black": "[0;30m", "blue": "[0;34m",
"green": "[0;32m", "red": "[0;31m", "brown": "[0;33m", "green": "[0;32m", "red": "[0;31m", "brown": "[0;33m",
@ -210,37 +211,34 @@ def handle_gif(image, output, reverse=False):
@module.rate(user=60) @module.rate(user=60)
@module.require_chanmsg(message="It's impolite to whisper.") @module.require_chanmsg(message="It's impolite to whisper.")
@module.commands('ascii') @module.commands('ascii')
@module.example('.ascii [-r] https://www.freshports.org/images/freshports.jpg') @module.example('.ascii [-rc] https://www.freshports.org/images/freshports.jpg')
def ascii(bot, trigger): def ascii(bot, trigger):
""" """
Downloads an image and converts it to ascii. Downloads an image and converts it to ascii.
""" """
reverse = False parser = argparse.ArgumentParser()
color = None parser.add_argument("imagePath")
if trigger.group(3) == "-r": parser.add_argument("-r", "--reverse", action="store_true")
imagePath = trigger.group(4) parser.add_argument("-c", "--color", action="store_true")
reverse = True parser.set_defaults(reverse=False, color=False)
elif trigger.group(3) == "-c": args = parser.parse_args(trigger.group(2).split())
imagePath = trigger.group(4)
color = "irc"
else:
imagePath = trigger.group(2)
if not web.secCheck(bot, imagePath): if args.color:
args.color = "irc"
if not web.secCheck(bot, args.imagePath):
return bot.reply("Known malicious site. Ignoring.") return bot.reply("Known malicious site. Ignoring.")
if not imagePath.startswith("http"): if not args.imagePath.startswith("http"):
bot.reply("Internet requests only.") bot.reply("Internet requests only.")
return return
image = open_image(imagePath) image = open_image(args.imagePath)
image_ascii = image_to_ascii(image, reverse, color) image_ascii = image_to_ascii(image, args.reverse, args.color)
bot.say(image_ascii) bot.say(image_ascii)
if __name__=='__main__': if __name__=='__main__':
import argparse
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Converts an image file to ascii art.") description="Converts an image file to ascii art.")
parser.add_argument( parser.add_argument(