getopts confusion

Rob Brown-Bayliss rob at ZOOstation.cc
Thu Jun 28 06:57:16 CEST 2001


Hi,  I have a problem with getopt on a linux system.

I have two python progs, one works and one does not.  Both import:
string, sys, getopt


This one WORKS:

	global verbose, log, upload, download, MasterServer
	
	try:
		opts, args = getopt.getopt(sys.argv[1:], "hvlud", ["help", "verbose",
"log", "upload", "download"])
	except getopt.error:
		# print help information and exit:
		usage()
		sys.exit(2)
	for o, a in opts:
		if o in ("-h", "--help"):
			usage()
			sys.exit()
		if o in ("-v", "--verbose"):
			verbose = 1
		if o in ("-l", "--log"):
			log = 1
			openlog("Syncro",LOG_NDELAY,LOG_DAEMON)
			syslog(LOG_NOTICE,"Starting Syncronisation run")
		if o in ("-u", "--upload"):
			upload = 1
		if o in ("-d", "--download"):
			download = 1


This one FAILS with: 

Error on option -s: unknown option.
Run './gearbox.py --help' to see a full list of available command line
options.

The code is:

	global wTree, GearBox, Status, showsum
	try:
		opts, args = getopt.getopt(sys.argv[1:], "hsz", ["help", "summary",
"zoo"])
		print getopt.getopt(sys.argv[1:], "hsz", ["help", "summary", "zoo"])
	except Error, msg:
		# print help information and exit:
		usage()
		sys.exit(2)
	for o, a in opts:
		if o in ("-h", "--help"):
			usage()
			sys.exit()
		if o in ("-s", "--summary"):
			showsum = 1
			print "ssss"
		if o in ("-z", "--zoo"):
			zoo = 1


The one that fails fais for all opts, but if I give args rather than
options it passes ok (as in I can print the args out)


Any one know whats going on?  Any clues are welcome

Thanks

-- 

  Rob Brown-Bayliss
 ---======o======---





More information about the Python-list mailing list