Link to home
Start Free TrialLog in
Avatar of Yankeelandy
Yankeelandy

asked on

Method for reading in command line options

Just wondering if there is a tried and true method for reading in command line options/flags.  Are there funcitons out there for searching argv? I could write one myself, but I was just wondering if any documentation existed on this topic.


Thanks,

Jake
Avatar of Exceter
Exceter
Flag of United States of America image

None that I am aware of, but that does not mean much. :-)

Try this,

if( argc > 1 )
{
     int x, r;
     for( x = 1; x < argc; x++ )
     {
          r = options( argv[x] );
          if( !r )
               printf("\nUnrecognized switch.\n\n");
     }
}

...

int options( char* args )
{
     int r = 0;

     if( strlen( args ) > 2 && args[0] == '-' )
          return r;

     switch( tolower( args[1] ) )
     {
          case 'd':
                        //insert code here
               r = 1;
               break;
          default:
               break;
     }

     return r;
}

Simply add more case statements to add additional flags.

Exceter
ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nothing has happened on this question in more than 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between akshayxx and snewo.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer