title: how to parse a boolean argument in a script?
body: <p>The following function and usage examples illustrate exactly what I need with this usage:</p>
  
  <p><code>test([{True | False}])</code>:</p>
  
  <pre><code>&gt;&gt;&gt; def test(arg=True):
  ...     if arg:
  ...         print "argument is true"
  ...     else:
  ...         print "argument is false"
  ...
  &gt;&gt;&gt; test()
  argument is true
  &gt;&gt;&gt; test(True)
  argument is true
  &gt;&gt;&gt; test(False)
  argument is false
  &gt;&gt;&gt; test(1)
  argument is true
  &gt;&gt;&gt; test(0)
  argument is false
  &gt;&gt;&gt; test("foo")
  argument is true
  &gt;&gt;&gt; test("")
  argument is false
  &gt;&gt;&gt;
  </code></pre>
  
  <p>Now I want exactly the same usage and behaviour but with command-line parsing, i.e. with this usage:</p>
  
  <p><code>python test [{True | False}]</code></p>
  
  <p>So I am trying to sort out how to do it with something like this:</p>
  
  <pre><code>import argparse
  parser = argparse.ArgumentParser()
  parser.add_argument("-arg",
      help="I want the usage to be [{True | False}] (defaults to True)")
  arg = parser.parse_args().arg
  
  if arg:
      print "argument is true"
  else:
      print "argument is false"
  </code></pre>
  
  <p>But I can't figure it out. I tried all sorts of options and combinations of options among which <code>action="store_true"</code>, <code>default=True</code>, <code>choices=[True, False]</code>, <code>type=bool</code> but nothing works as I would like, e.g.:</p>
  
  <pre><code>$ python test.py -h
  usage: test.py [-h] [-arg ARG]
  
  optional arguments:
    -h, --help  show this help message and exit
    -arg ARG    I want the usage to be [{True | False}] (defaults to True)
  
  $ python test.py
  argument is true
  
  $ python test.py True
  usage: test.py [-h] [-arg ARG]
  test.py: error: unrecognized arguments: True
  
  etc.
  </code></pre>
  
  <p>Thanks for any help.</p>