[techtalk] switch function in C (or how to read commandline args?)

Conor Daly conor.daly at oceanfree.net
Sat Jun 30 21:32:38 EST 2001


I want to read a bunch of commandline args or config options from file 
in a C program.  Now, in bash, I can use a switch structure to do stuff
based on the *string* presented.  AFAIR, in C, you can only switch on an
*int*.

So, how do I use a switch to process my config options? Or do I have to
build an "if; elseif; else" ladder instead?  I have options in a config
file that look like

<opt> <arg>
location Belmullet
name "Joe Bloggs"

and commandline switches like

--force
-C /path/to/config.file

I can read these and put the values into "opt" and "arg" but what I need
to do next is the C equivalent to the following (bash)

case $opt in
	name)
		NAME=$arg
	;;
	location)
		LOCATION=$arg
	;;
	--force)
		FORCE=TRUE
	;;
	-C)
		CONFIG_FILE=$arg
		readconfig($CONFIG_FILE)
	;;
	*)
		echo "AARRRGGGHHHH!!!!!"
	;;
esac

I even tried 

case (! strcmp(opt,"config-file")):

to try and return an integer for the case statement but it 
seems to need a constant rather than the return value from 
function to work properly.

TIA
Conor
-- 
Conor Daly <conor.daly at oceanfree.net>

Domestic Sysadmin :-)
---------------------
Faenor.cod.ie
  9:15pm  up 15 days, 21:32,  0 users,  load average: 0.08, 0.02, 0.01
Hobbiton.cod.ie
  9:21pm  up 15 days, 21:38,  2 users,  load average: 0.00, 0.00, 0.00




More information about the Techtalk mailing list