Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

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
0
Yankeelandy
Asked:
Yankeelandy
2 Solutions
 
ExceterCommented:
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
0
 
akshayxxCommented:
if u r on linux then most probably are working with GNU-libc
then have a look at this..with examples

http://ldeniau.home.cern.ch/ldeniau/docs/getopts.pdf

or do
man 3 getopts
0
 
snewoCommented:

Yes there is, I just wrote a program using them.

The premade function is getopt and it is available in uninst.h.   Here is more info:

http://www.opengroup.org/onlinepubs/007904975/functions/getopt.html


0
 
jmcgOwnerCommented:
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now