title: python argparse -- customizing error messages
body: <p>I want to generate custom error messages for particular usage errors in my command-line program that uses the <code>argparse</code> library.  I know I can override the general presentation of the error by subclassing <code>argparse.ArgumentParser</code>:</p>
  
  <pre><code>class HelpParser(argparse.ArgumentParser):
      def error(self, message):
          sys.stderr.write('error: %s\n' % message)
          sys.exit(2)
  
  parser = HelpParser(... ...)
  args = parser.parse_args()
  </code></pre>
  
  <p>But when my <code>error</code> method is called, <code>message</code> has already been formatted by the library.  For example,</p>
  
  <pre><code>&gt; python prog.py old stuff
  
  usage: prog [-h] {hot,cold,rain,snow} ...
  prog: error: argument subparser: invalid choice: 'old' (choose from u'hot', u'cold', u'rain', u'snow')
  </code></pre>
  
  <p>How can I change how the stuff after <code>error:</code> is presented, for instance to</p>
  
  <pre><code>usage: prog [-h] {hot,cold,rain,snow} ...
  error: 'old' is not a valid option. select from 'hot', 'cold', 'rain', 'snow'
  </code></pre>
  
  <p>?</p>