
import argparse


class ArgumentParser(argparse.ArgumentParser):

    def error(self, message):
        raise Exception('error: {}\n'.format(message))


class Module(object):
    cmd = ''
    description = ''
    args = []
    authors = []
    output = []

    def __init__(self):
        self.parser = ArgumentParser(prog=self.cmd, description=self.description, add_help=False)
        self.parser.add_argument('-h', '--help', action='store_true', help='show this help message')

    def set_args(self, args):
        self.args = args

    def log(self, event_type, event_data):
        self.output.append(dict(
            type=event_type,
            data=event_data
        ))

    def usage(self):
        self.log('', self.parser.format_usage())

    def help(self):
        self.log('', self.parser.format_help())

    def run(self, *args):
        self.parsed_args = None
        try:
            self.parsed_args = self.parser.parse_args(self.args)
            if self.parsed_args.help:
                self.help()
                self.parsed_args = None
        except Exception as e:
            self.usage()
            self.log('', e)


import argparse


class ArgumentErrorCallback(Exception):

    def __init__(self, message, level=''):
        self.message = message.strip() + '\n'
        self.level = level.strip()

    def __str__(self):
        return '{}: {}'.format(self.level, self.message)

    def get(self):
        return self.level, self.message


class ArgumentParser(argparse.ArgumentParser):

    def print_usage(self):
        raise ArgumentErrorCallback(self.format_usage())

    def print_help(self):
        raise ArgumentErrorCallback(self.format_help())

    def error(self, message):
        raise ArgumentErrorCallback(message, 'error')

    def exit(self, status, message=None):
        if message is not None:
            raise ArgumentErrorCallback(message)


class Module(object):
    cmd = ''
    description = ''
    args = []
    authors = []
    output = []

    def __init__(self):
        self.parser = ArgumentParser(prog=self.cmd, description=self.description)

    def set_args(self, args):
        self.args = args

    def log(self, event_type, event_data):
        self.output.append(dict(
            type=event_type,
            data=event_data
        ))

    def usage(self):
        self.log('', self.parser.format_usage())

    def help(self):
        self.log('', self.parser.format_help())

    def run(self):
        self.parsed_args = None
        if len(self.args) == 0:
            self.usage()
        else:
            try:
                self.parsed_args = self.parser.parse_args(self.args)
            except ArgumentErrorCallback as e:
                self.log(*e.get())

Improve the error handling, better use of ArgumentParser.
Improve the error handling, better use of ArgumentParser.

  
  import argparse
  
  
+ class ArgumentErrorCallback(Exception):
+ 
+     def __init__(self, message, level=''):
+         self.message = message.strip() + '\n'
+         self.level = level.strip()
+ 
+     def __str__(self):
+         return '{}: {}'.format(self.level, self.message)
+ 
+     def get(self):
+         return self.level, self.message
+ 
+ 
  class ArgumentParser(argparse.ArgumentParser):
  
+     def print_usage(self):
+         raise ArgumentErrorCallback(self.format_usage())
+ 
+     def print_help(self):
+         raise ArgumentErrorCallback(self.format_help())
+ 
      def error(self, message):
-         raise Exception('error: {}\n'.format(message))
+         raise ArgumentErrorCallback(message, 'error')
+ 
+     def exit(self, status, message=None):
+         if message is not None:
+             raise ArgumentErrorCallback(message)
  
  
  class Module(object):
      cmd = ''
      description = ''
      args = []
      authors = []
      output = []
  
      def __init__(self):
-         self.parser = ArgumentParser(prog=self.cmd, description=self.description, add_help=False)
+         self.parser = ArgumentParser(prog=self.cmd, description=self.description)
-         self.parser.add_argument('-h', '--help', action='store_true', help='show this help message')
  
      def set_args(self, args):
          self.args = args
  
      def log(self, event_type, event_data):
          self.output.append(dict(
              type=event_type,
              data=event_data
          ))
  
      def usage(self):
          self.log('', self.parser.format_usage())
  
      def help(self):
          self.log('', self.parser.format_help())
  
-     def run(self, *args):
+     def run(self):
          self.parsed_args = None
+         if len(self.args) == 0:
-         try:
-             self.parsed_args = self.parser.parse_args(self.args)
-             if self.parsed_args.help:
-                 self.help()
-                 self.parsed_args = None
-         except Exception as e:
              self.usage()
+         else:
+             try:
+                 self.parsed_args = self.parser.parse_args(self.args)
+             except ArgumentErrorCallback as e:
-             self.log('', e)
+                 self.log(*e.get())
  
Improve the error handling, better use of ArgumentParser.
## Code Before:

import argparse


class ArgumentParser(argparse.ArgumentParser):

    def error(self, message):
        raise Exception('error: {}\n'.format(message))


class Module(object):
    cmd = ''
    description = ''
    args = []
    authors = []
    output = []

    def __init__(self):
        self.parser = ArgumentParser(prog=self.cmd, description=self.description, add_help=False)
        self.parser.add_argument('-h', '--help', action='store_true', help='show this help message')

    def set_args(self, args):
        self.args = args

    def log(self, event_type, event_data):
        self.output.append(dict(
            type=event_type,
            data=event_data
        ))

    def usage(self):
        self.log('', self.parser.format_usage())

    def help(self):
        self.log('', self.parser.format_help())

    def run(self, *args):
        self.parsed_args = None
        try:
            self.parsed_args = self.parser.parse_args(self.args)
            if self.parsed_args.help:
                self.help()
                self.parsed_args = None
        except Exception as e:
            self.usage()
            self.log('', e)

## Instruction:
Improve the error handling, better use of ArgumentParser.
## Code After:

import argparse


class ArgumentErrorCallback(Exception):

    def __init__(self, message, level=''):
        self.message = message.strip() + '\n'
        self.level = level.strip()

    def __str__(self):
        return '{}: {}'.format(self.level, self.message)

    def get(self):
        return self.level, self.message


class ArgumentParser(argparse.ArgumentParser):

    def print_usage(self):
        raise ArgumentErrorCallback(self.format_usage())

    def print_help(self):
        raise ArgumentErrorCallback(self.format_help())

    def error(self, message):
        raise ArgumentErrorCallback(message, 'error')

    def exit(self, status, message=None):
        if message is not None:
            raise ArgumentErrorCallback(message)


class Module(object):
    cmd = ''
    description = ''
    args = []
    authors = []
    output = []

    def __init__(self):
        self.parser = ArgumentParser(prog=self.cmd, description=self.description)

    def set_args(self, args):
        self.args = args

    def log(self, event_type, event_data):
        self.output.append(dict(
            type=event_type,
            data=event_data
        ))

    def usage(self):
        self.log('', self.parser.format_usage())

    def help(self):
        self.log('', self.parser.format_help())

    def run(self):
        self.parsed_args = None
        if len(self.args) == 0:
            self.usage()
        else:
            try:
                self.parsed_args = self.parser.parse_args(self.args)
            except ArgumentErrorCallback as e:
                self.log(*e.get())
