Thread: Optional flag in commandline

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    Optional flag in commandline

    Hi, I've been googling through the internet trying to find articles and sample on the optional flag in commandline argument. I was hoping someone could point me to the right direction for the example. I found one but it has very little information.

    http://kevindumpscore.com/docs/csoun...mandflags.html

    -d, --nodisplays

    Suppress all displays.
    I've been trying to write a program to show different types of output whether the flag is there or not but it doesn't work.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Explain how it should work, post the code, and any problems or errors you have so far; we can help you more that way.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    I can't really post any codes, from what I understand from the quote, I tried using printf, fprintf with or without the flag, it still prints both strings.

    E.G.
    Code:
     ./sample -d
    All I did was a simple program to test which string will be printed.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You have to parse the command line options yourself

    Code:
    int suppress = 0;
    int main ( int argc, char *argv[] ) {
      if ( strcmp( argv[1], "-d") == 0 ) {
        suppress = 1;
      }
      // more code
      if ( ! suppress ) {
        printf( "A message\n" );
      }
      return 0;
    }
    There are no standard flags which affect all C programs. There are however a few standard conventions amongst certain groups of related programs.

    If you're using Unix/Linux, then the getopt() function can make parsing command line arguments a bit easier.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    I see..thanks Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. need help on error handling.
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 06-19-2008, 11:55 AM
  3. Checking for flag input through argv
    By cisokay in forum C Programming
    Replies: 6
    Last Post: 05-11-2005, 10:51 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. my own commandline parser function
    By scrappy in forum C Programming
    Replies: 7
    Last Post: 08-22-2003, 02:57 AM