Thread: Detect flags arguments

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Detect flags arguments

    Hi everyone. I did a program in C++ and in my program in need to detect some flags that are given by arguments. I did it in a very inelegant way and i wasted more than 100 lines to do that. So i'm asking if someone know some algorithm or function or have some examples how to do this ?

    Thank you very much

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That would depend a lot on:
    1. What environment? OS, compiler, etc?
    2. What style and parameters are the flags/options?
    3. What is the consequence of each flag/option? Do the flags have arguments to (e.g -x=3 or -outfile=foo.txt)?

    Give some examples of what a command line for the program would look like, and a list of all options [or at least a list of a few typical ones]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    For example this line:

    ./program -n 34 56 -i -f

    I'm programming in Linux

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So presumably you would walk through argv; if argv[i][0] is a hyphen, do a switch on argv[i][1] and process that character. You'd have to use atoi or strtol to get numbers out of "34" and "56", of course.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may also want to look at "man getopt" - it's a function written specifically to support option parsing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matsp View Post
    You may also want to look at "man getopt" - it's a function written specifically to support option parsing.

    --
    Mats
    You don't want to look at my google search history for the last five minutes because I couldn't think of the name. Thanks.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And there's Boost.ProgramOptions, which is IMO far superior to getopt.
    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

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I'll second CornedBee!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  4. Bit Flags
    By Padawan in forum C Programming
    Replies: 15
    Last Post: 03-30-2004, 10:38 PM
  5. Replies: 5
    Last Post: 11-20-2003, 01:27 AM