David --
It might be helpful if you could post a repro. case on a pastebin or
>> something. Also, what happens if you take your code, wrap it in a trivial
>> function, and then at the bottom, call the function as follows:
>> if __name__ == '__main__':
>> foo()
>> As far as I know, that's the more idiomatic way of making command-line
>> scripts in Python.
>>
>
A slight modification to Rohit's suggestion (who is correct that checking
for __main__ is the usual way of hiding script argument reading and other
setup). Suggest keeping your argument reading separate from the rest of
your script that does the actual work. That is, move everything except the
command line reading into a function with parameters, and read the command
line as a separate step (which can just be under your __main__ test if it's
not too messy). That way, you can unit test the rest of the script.
"""
This is some help text.
"""
def work(thing):
print "Got thing = %s" % thing
# Hide use of sys.argv
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
sys.exit("Usage: %s thing" % sys.argv[0])
# Call whatever does the work.
work(sys.argv[1])
Now you can unit test work(thing). ;-) Just sayin'...
-- Pat