Thread: Command line how to handle optional command.

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    Command line how to handle optional command.

    Hi guys, I am trying to write a simple command line parser.
    My question is what is the better way to handle optional command.
    for example
    like shell command in linux
    (1)ls -a -l
    (2)ls -l
    (3)ls
    (4)ls -a
    (5)ls -a -l -s
    are all valid command

    When I confront this, I always stuck into multiple if ,else if ,else if conditions
    Code:
    if() {
    ...
    } else if (){
    ...
    } else if (){
    ...
    } else if (){
    
    }
    And each block have very similar codes.
    And so many selections code just because of the order of command can exchange.


    Given a specific eg.
    assume
    print a b c d is a valid command.
    and order doesn't matter.
    a b c d is optional
    what I will write will become
    Code:
    if (a) {
       if(b) {
           if (c) {
                if(d)   //   print a b c d
                else  // print a b d 
           }
           else{
                if (d)  // print a b d
                else  // print a b
           }
       }
        else {
             if (c)
             .....
       }
    }
    else {
    
    }

    A really stupid solution... because if there is n optional command.
    I have to at most handle 2^n selections code.

    Could anyone suggest a better idea?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First, read this getopt(3): Parse options - Linux man page

    Second, have a number of state variables which get updated.
    So for example, for the -a option, you would have
    int showDotFiles; // == 1 if -a was specified.

    So for example, for the -l option, you would have
    int showDetails; // == 1 if -l was specified.



    In the program code, you get your list of filenames using say opendir/readdir.

    Then say
    if ( filename[0] == '.' && showDotFiles ) displayResults(showDetails);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with "touch" command in c program
    By Moony in forum C Programming
    Replies: 10
    Last Post: 08-01-2006, 09:56 AM
  2. in program command console
    By howzer in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2006, 03:12 PM
  3. Command Interface : Black Box testing
    By GlowinCelica in forum C Programming
    Replies: 1
    Last Post: 03-10-2003, 10:21 PM
  4. a simple C question...
    By DramaKing in forum C Programming
    Replies: 10
    Last Post: 07-28-2002, 02:04 PM
  5. API Reading Files, handle is always -1
    By Xei in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2002, 10:16 PM