Click to See Complete Forum and Search --> : programming with switches: need a HOWTO


Cerf
01-16-2005, 10:41 PM
Hey,

I was just wondering if someone could refer me to some literature on how to decode switches. For example, "ls -la". "l" and "a" are the switches. What makes it complicated is the many combinations it could be in.

ls -l -a
ls -a -l
ls -la
ls -al

The number of combinations is P(3, 3) = 3! = 3 x 2 x 1.

Now I'm sure that in a simple case like this everything could me accomplished in a nice loop, but what about a command such as tar where there is the switchs and strings in the command line

So to get back to my origional point, does anyone know where I can get a HOWTO on this or any related lituratre?

bwkaz
01-16-2005, 11:06 PM
man 3 getopt

:p

(That's what the ls binary uses to parse its command line options, at least. So I know you can get equivalent behavior by using it, if you set up the option string the same way. Long options (getopt_long()) are sometimes a trick, but if you keep to the short options only, it should be easier.)

the.spike
01-18-2005, 08:44 AM
I've used getopt in perl, c and various different shells.. You can parse single flags as in

<prog> -a -b -c

or multiple flags

<prog> -abc

or options with parameters

<prog> -a option_1 -b option_2

or a combination

<prog> -a option_1 -bc -d -e option_2

All do the same job but the syntax differs from language to language..

Check out the manuals (websites etc) for the language you are using.. (I've just googled "perl getopt" and the first hit looked like a winner)

spike..

Cerf
01-18-2005, 08:47 AM
Originally posted by bwkaz
man 3 getopt

:p

(That's what the ls binary uses to parse its command line options, at least. So I know you can get equivalent behavior by using it, if you set up the option string the same way. Long options (getopt_long()) are sometimes a trick, but if you keep to the short options only, it should be easier.)

Thanks, it works well. (lol - Now I need to get a windows port for it but I can take care of it)