import sys
import re

from textblob import TextBlob


class Plugin:
    matcher = re.compile(r'translate (?:from ([^ ]+) )?(?:to ([^ ]+) )?(.*)') 

    def on_command(self, bot, msg, stdin, stdout, reply):
        match = self.matcher.match(msg["message"])
        if not match:
            return
        from_lang = match.group(1) or None  # let autodetect decide
        to_lang   = match.group(2) or "en"
        message   = TextBlob(match.group(3))
        try:
            translated = message.translate(from_lang=from_lang, to=to_lang)
        except:
            return
        print(translated, file=stdout)

    def on_help(self):
        return "Usage: translate [from <language>] [to <language>] <text>"

@@ -13,16 +13,32 @@
 mport re
+%0Aimport argparse
 %0A%0Afrom t
@@ -62,17 +62,16 @@
 xtBlob%0A%0A
-%0A
 class Pl
@@ -80,217 +80,720 @@
 in:%0A
+%0A
     
-matcher = re.compile(r'translate (?:from (%5B%5E %5D+) )?(?:to (%5B%5E %5D+) )?(.*)') %0A%0A    def on_command(self, bot, msg, stdin, stdout, reply):%0A        match = self.matcher.match(msg%5B%22message%22%5D)%0A        if not match
+parser = argparse.ArgumentParser(add_help=False)%0A    parser.add_argument(%22-from%22,%22--from-language%22, default=None, nargs=%22?%22)  # let autodetect decide%0A    parser.add_argument(%22-to%22  ,%22--to-language%22,   default=%22en%22, nargs=%22?%22)%0A    parser.add_argument(%22message%22, nargs=%22*%22)%0A%0A    def on_command(self, bot, msg, stdin, stdout, reply):%0A        # pre-process args%0A        # this might mess up if %22from%22 or %22to%22 is left out and%0A        # the message contains %22from%22 or %22to%22%0A        self._push_character(msg%5B%22args%22%5D, %22from%22, %22-%22, 1)%0A        self._push_character(msg%5B%22args%22%5D, %22to%22,   %22-%22, 1)%0A%0A        try:%0A            args = self.parser.parse_args(msg%5B%22args%22%5D%5B1:%5D)%0A        except (argparse.ArgumentError, SystemExit)
 :%0A  
@@ -809,24 +809,25 @@
  return%0A
+%0A
         
 from_lan
@@ -822,110 +822,272 @@
     
-from_lang = match.group(1) or None  # let autodetect decide%0A        to_lang   = match.group(2) or %22en%22
+# get message from the appropriate place%0A        if args.message:%0A            message = %22 %22.join(args.message)%0A        else:%0A            message = stdin.read().strip()%0A%0A        # translate%0A        from_lang = args.from_language%0A        to_lang   = args.to_language
 %0A   
@@ -1117,21 +1117,14 @@
 ob(m
-atch.group(3)
+essage
 )%0A  
@@ -1379,12 +1379,226 @@
 e%3E%5D 
+%5B
 %3Ctext%3E
+%5D
 %22%0A
+%0A    @staticmethod%0A    def _push_character(args, target, character, n):%0A        try:%0A            i = args.index(target)%0A            args%5Bi%5D = (character * n) + args%5Bi%5D%0A        except ValueError:%0A            pass%0A

Improve translate plugin