How to get getopt.getopt in python -


my python script this:

def main(argv):     print argv     try:         opts, args = getopt.getopt(argv,"h",["help"])         print opts         print args         opt, arg in opts:            print opt 

but when run it, see opts empty.

 ./myscript.py debug.lst -s 0 -e 1 ['debug.lst', '-s', '0', '-e', '1'] [] ['debug.lst', '-s', '0', '-e', '1'] 

do have idea how work?

as the docs say:

note unlike gnu getopt(), after non-option argument, further arguments considered non-options. similar way non-gnu unix systems work.

so, should expect.

if want gnu-style parsing instead of traditional unix-style parsing, use gnu_getopt.

or, better, use argparse instead of getopt. reasons ever use getopt (a) know getopt of hand , don't want learn new, or (b) you're porting or maintaining code (possibly c or language) uses getopt. neither of these applies here. docs explicitly, right @ top of getopt documentation.


also, note once fix problem, 1 way or another, you're going errors unrecognized option -s.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -