-
command/program options
Hi,
I have a c++ program that handles serial communications. At this stage, serial settings (ports, baud rate, settings) are hard-coded into the program.
I'd like to re-build the program so that it will take in options/flags to control each of the settings, such as
Code:
c:\user\command.exe -p 5 -r 115200
the command above will listen on COM5 at rate 115200
I've been reading about command line arguments, and I've tried to play with it like this:
Code:
int main(int argc, char **argv)
{
switch (argv[1])
.......
}
butswitch statement doesn't allow checking char data type.
any insight will be so helpful!!
thanks!!!
-
switch allows switching on a char. What it doesn't necessarily like is switching on a char*. If all your arguments are one letter long, then that letter will be argv[x][1] (where x is the argument number, and argv[x][0] is supposed to be the hyphen that precedes it). Note also that you can increment argv to the next argument....
-
nice!!!
thanks!! I solved the problem!
-
In my opinion the best way to handle command line arguments is the Boost.ProgramOptions library.
-
thanks!
actually, i ran into problem
that is an array of characters. yet, i need to get com port and baud rate, which has been declared as INT16. Is there any way to get around this?
All of my serial-com functions has been written based on INT3 baud rate and com-port.
many thansk!
-
Do you mean something other than "use atoi or strtol"?
-
yeah. I tried that but the compiler won't let me
Code:
atol: cannot convert parameter 1 from char` to `const car`
-
Looks like you're trying to pass a single character to a function expecting a string.
-
based on my main()
Code:
int main(int arc, char *arv[])
arv[] is char pointer. I'd like to passed something like
Code:
command.exe 5 filename.txt
"5" will be parsed into a serial-com function to open corresponding COM ports.
'filename.txt" will be parsed into another serial-com funciton to stream output from serial line into file.txt
The problem is that serial-com open-port functions is expecting an integer; whereas, "5" is a char.
thanks!!
-
You were complaining about atol just now, not main, correct? atol expects not a single character (like argv[1][1]), but a string (like argv[1]).
-
I see, so "atol" doesn't get a single character.
yes, i was trying to get around "char to int" conversion error, and you brought up "atol" so i tried out and got the error above.
Thanks!!