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>Unlike the OP and one other answer so far, I don't see why you would compute both the odd and even lists knowing you'll only need one of them -- it's short distance from "simple" to "simply inefficient".  How about we delay the "hard work" of thinning the lists until we've finished the "easy work" of decoding the arguments:</p>

<pre><code>import sys

def oddtarray(digits):
    tlist = map(int, digits)
    tnum = [x for x in tlist if (x % 2) != 0]
    return int(''.join(map(str, tnum)))

def eventarray(digits):
    tlist = map(int, digits)
    tnum = [x for x in tlist if (x % 2) == 0]
    return int(''.join(map(str, tnum)))

def transform():
    if len(sys.argv) == 3:

        try:
            if sys.argv[1] == "1":
                print oddtarray(sys.argv[2])
            elif sys.argv[1] == "2":
                print eventarray(sys.argv[2])
            else:
                usage()
        except ValueError:
            usage()
    else:
        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>Specific issues with the original program: the <code>odd</code> and <code>even</code> variables are reversed; command line arguments are being treated as numbers instead of as strings that look like numbers; the validity of the length of the command line argument list is being tested <strong>after</strong> items have already been extracted from it -- it should be tested before; the possibility of an IndexError exception is more likely due to programmer error than user error -- a ValueError exception is more likely due to user error (entering non-digits).</p>

python