Having problems parsing numerical command-line arguments
<p>I am making a super simple program that strips even or odd numbers from a string given on the command line. For example:</p>

<pre><code>$ test.py 1 1234
13
$ test.py 2 1234
24
</code></pre>

<p>The problem is I can't get it to work. It prints the usage instead of the expected numbers.</p>

<pre><code>$ test.py 1 1234  
Usage: test.py [option] [number], etc..
</code></pre>

<p>Also, this should print out the usage, but instead does nothing:</p>

<pre><code>$ python2.7 test.py 1  
$
</code></pre>

<p>Why doesn't the program output the usage? <code>len(sys.argv)</code> is <code>&lt; 2</code> so it should print usage, correct?</p>

<pre><code>def oddtarray(num):
    tlist = map(int, str(num))
    tnum = [x for x in tlist if (x % 2) != 0]
    return tnum
def eventarray(num):
    tlist = map(int, str(num))
    tnum = [x for x in tlist if (x % 2) == 0]
    return tnum
def transform():
    try:
        odd = int(''.join(map(str, eventarray(sys.argv[2]))))
        even = int(''.join(map(str, oddtarray(sys.argv[2]))))
            if len(sys.argv) == 3:
                if sys.argv[1] == 1:
                    print odd
                elif sys.argv[1] == 2:
                    print even
                else:
                    usage()
            else:
                usage()
    except IndexError:
        if len(sys.argv) &lt; 2:
            usage()
def usage():
    print 'Usage: test.py [option] [number]'
    print 'Options: \n[1] - Remove even numbers\n[2] - Remove odd numbers'

transform()
</code></pre>

<p><code>sys.argv[1] == 1</code> is not possible. You mean: <code>sys.argv[1] == "1"</code>. Those are strings, not numbers.</p>

<p>Your argument parsing would require some cleaning. No need for all those checks and exceptions to do such a simple task.</p>

<p>Allow me to "code review" &amp; fix your code (I focused on the arg parsing part, protecting the integer parsing by a try/except <code>ValueError</code> printing usage and testing the number of arguments):</p>

<pre><code>import sys
def oddtarray(num):
    tlist = map(int, str(num))
    tnum = [x for x in tlist if (x % 2) != 0]
    return tnum
def eventarray(num):
    tlist = map(int, str(num))
    tnum = [x for x in tlist if (x % 2) == 0]
    return tnum

def transform():
    if len(sys.argv) &lt; 3:
        # not enough arguments: print usage and that's it!
        usage()
    else:
        try:
          command = int(sys.argv[1])  # convert to integer
          odd = int(''.join(map(str, eventarray(sys.argv[2]))))
          even = int(''.join(map(str, oddtarray(sys.argv[2]))))

          if command == 1:
              print(odd)
          elif command == 2:
              print(even)
          else:
              usage()
        except ValueError as e:
           # some string to integer went wrong: display detailed exception
           # just in case, then usage
           print(str(e))
           usage()
def usage():
    print('Usage: test.py [option] [number]')
    print('Options: \n[1] - Remove even numbers\n[2] - Remove odd numbers')

transform()
</code></pre>

<p>now it's really "super simple", and as a bonus, it works :)</p>

python