Command line how to handle optional command.

This is a discussion on Command line how to handle optional command. within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I am trying to write a simple command line parser. My question is what is the better way ...

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

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, 02:12 PM
  3. Command Interface : Black Box testing
    By GlowinCelica in forum C Programming
    Replies: 1
    Last Post: 03-10-2003, 09: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

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