Optional flag in commandline

This is a discussion on Optional flag in commandline within the C Programming forums, part of the General Programming Boards category; Hi, I've been googling through the internet trying to find articles and sample on the optional flag in commandline argument. ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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

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