title: python argh/argparse: How can I pass a list as a command-line argument?
body: <p>I'm trying to pass a list of arguments to a python script using the argh library.  Something that can take inputs like these:</p>
  
  <pre><code>./my_script.py my-func --argA blah --argB 1 2 3 4
  ./my_script.py my-func --argA blah --argB 1
  ./my_script.py my-func --argA blah --argB 
  </code></pre>
  
  <p>My internal code looks like this:</p>
  
  <pre><code>import argh
  
  @argh.arg('--argA', default="bleh", help='My first arg')
  @argh.arg('--argB', default=[], help='A list-type arg--except it\'s not!')
  def my_func(args):
      "A function that does something"
  
      print args.argA
      print args.argB
  
      for b in args.argB:
          print int(b)*int(b)  #Print the square of each number in the list
      print sum([int(b) for b in args.argB])  #Print the sum of the list
  
  p = argh.ArghParser()
  p.add_commands([my_func])
  p.dispatch()
  </code></pre>
  
  <p>And here's how it behaves:</p>
  
  <pre><code>$ python temp.py my-func --argA blooh --argB 1
  blooh
  ['1']
  1
  1
  
  $ python temp.py my-func --argA blooh --argB 10
  blooh
  ['1', '0']
  1
  0
  1
  
  $ python temp.py my-func --argA blooh --argB 1 2 3
  usage: temp.py [-h] {my-func} ...
  temp.py: error: unrecognized arguments: 2 3
  </code></pre>
  
  <p>The problem seems pretty straightforward: argh is only accepting the first argument, and treating it as a string.  How do I make it "expect" a list of integers instead?</p>
  
  <p>I see <a href="https://stackoverflow.com/questions/2086556/specifying-a-list-as-a-command-line-argument-in-python">how this is done in optparse</a>, but what about the (not-deprecated) argparse?  Or using argh's much nicer decorated syntax?  These seem much more pythonic.</p>