[getopt-sig] More about commands on the command line

Matthias Urlichs smurf@noris.de
Fri, 8 Mar 2002 14:16:07 +0100


Hi,

I wrote:
> 	if not sys.argv[1].startswith('-'):
> 		sys.argv[1] = '-' + sys.argv[1]
> 
> (NOTE: This test doesn't handle corner cases like 'no arguments' or
>        'empty argument'.)
> 
... and completely forgot that this doesn't work for "tar" at all.

But, given an option package which allows introspection, writing a generic
code snipped which rips this apart is no problem at all. Untested code:

 	if not sys.argv[1].startswith('-'):
		rargs=sys.argv[2:]
		args=[]
		for c in sys.argv[1]:
			args.append("-"+c)
			if opts.option(c).wants_an_argument():
				args.append(rargs[0])
				del rargs[0:1]
		sys.argv=sys.argv[0]+args+rargs

so I think that "tar" is a rather noninteresting example.

-- 
Matthias Urlichs     |     noris network AG     |     http://smurf.noris.de/