title: Use argparse with Setuptools entry_points
body: <p>I'm writing a script which I want to distribute using Setuptools. I have added this script to the <code>entry_points</code> section in my <code>setup.py</code>.</p>
  
  <p>From the setuptools docs:</p>
  
  <blockquote>
    <p>The functions you specify are called with no arguments, and their return value is passed to sys.exit(), so you can <strong>return an errorlevel or message to print to stderr</strong>.</p>
  </blockquote>
  
  <p>Since the method will return instead of exit it becomes more testable. For testability purposes I accept arguments in the method defaulting to <code>sys.argv</code>. So far so good.</p>
  
  <p>The problem arises when argparse is added to the mix. When argparse fails to parse args it calls <code>sys.exit</code>. Now I would really prefer that argparse doesn't do this as this is handled by the setuptools wrapper. The first thing I could think of to fix this is to override the <code>argparse.ArgumentParser</code> but then I saw this:</p>
  
  <pre><code># ===============
  # Exiting methods
  # ===============
  def exit(self, status=0, message=None):
      if message:
          self._print_message(message, _sys.stderr)
      _sys.exit(status)
  
  def error(self, message):
      """error(message: string)
  
      Prints a usage message incorporating the message to stderr and
      exits.
  
      If you override this in a subclass, it should not return -- it
      should either exit or raise an exception.
      """
      self.print_usage(_sys.stderr)
      self.exit(2, _('%s: error: %s\n') % (self.prog, message))
  </code></pre>
  
  <p>So the docstring states I should not return and stick with raising an exception. How should I solve this?</p>
  
  <p>The main method if I didn't explain it thoroughly enough:</p>
  
  <pre><code>def main(args=sys.argv):
      parser = ArgumentParser(prog='spam')
  
      # parser is configured here
  
      parsed = parser.parse_args(args)
  
      # Parsed args are used here
  </code></pre>