command/program options

This is a discussion on command/program options within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a c++ program that handles serial communications. At this stage, serial settings (ports, baud rate, settings) are ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    43

    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!!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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....

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    43
    nice!!!
    thanks!! I solved the problem!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In my opinion the best way to handle command line arguments is the Boost.ProgramOptions library.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    43
    thanks!
    actually, i ran into problem
    Code:
    char* argv[]
    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!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you mean something other than "use atoi or strtol"?

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    43
    yeah. I tried that but the compiler won't let me
    Code:
    atol: cannot convert parameter 1 from char` to `const car`

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Looks like you're trying to pass a single character to a function expecting a string.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    43
    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!!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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]).

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    43
    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!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 02:24 PM
  2. What's the best way to handle many program options?
    By stickmangumby in forum C Programming
    Replies: 19
    Last Post: 06-06-2007, 05:06 PM
  3. Options Dialog
    By Morgul in forum Game Programming
    Replies: 3
    Last Post: 11-16-2005, 12:15 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 08:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21