and from the python's prompt will yield different results:
c:\Temp>c:\Python27\python Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import test >>> test.main(['arg1', 'arg2']) {} ['arg2'] >>> test.main(['arg_that_nobody_cares_of', 'arg1', 'arg2']) {} ['arg1', 'arg2']
My proposal is the following: ------------------------------------------------------
if __name__ == "__main__": sys.exit(main(sys.argv[1:])) # (3)
------------------------------------------------------ (1) opts_args defaults to [], because calling parser.parse_args(None) will actually parse sys.argv[1:], and you don't want this if calling main() from another script. (2) if you want to use the script name in the help message, __file__ should do (3) no more sys.argv parsing past this point