command line arguments processing: an ugly lpr wrapper

morden morden at shadows.net
Sun Mar 16 19:50:19 EST 2003


I want the users of the script identify themselves with -B option
(for logging) and pass the remaining arguments to lpr.
What I came up with looks rather ugly:


by="?"
next = 0
new_arg = []
for arg in sys.argv:
	if next:
		by = arg
		next = 0
	else:
		if arg == "-B":
			next = 1
		else:
			new_arg.append(arg)
...
# log "by" someplace
...
os.execv("/usr/bin/lpr", new_arg)

# should I exit(os.execv("/usr/bin/lpr", new_arg)) instead?

So, my question is:
how to make the code a bit less igly
and make it take arguments in format -Bfoo in addition to -B foo?





More information about the Python-list mailing list