earthbound19 / _ebDev

Various tools, mostly custom-made for art development.
2 stars 2 forks source link

correct sys.argv length check in python scripts #105

Closed earthbound19 closed 4 years ago

earthbound19 commented 4 years ago

some Python scripts that use positional switches may be checking the length of sys.argv (to indicate the existence of positional switches) incorrectly.

Examine them re this:

wut.py

import sys
print('len of sys.argv is', len(sys.argv))
print('sys.argv[0] is', sys.argv[0])

if len(sys.argv) > 1:     # checking for FIRST parameter, but it makes length of sys.argv 2!
    print('sys.argv[1]: ', sys.argv[1])

if len(sys.argv) > 2:     # checking for SECOND parameter, but it makes length of sys.argv 3!
    print('sys.argv[2]: ', sys.argv[2])

If you pass one parameter to a script, the length of sys.argv becomes two, because it uses zero-based indexing (and the index at 0 is the name of the script).

earthbound19 commented 4 years ago

(Done a while ago and forgot to close)