?
Solved

Method for reading in command line options

Posted on 2003-03-03
5
Medium Priority
?
188 Views
Last Modified: 2010-04-15
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
Comment
Question by:Yankeelandy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8061328
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
 
LVL 8

Accepted Solution

by:
akshayxx earned 100 total points
ID: 8061763
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
 
LVL 4

Assisted Solution

by:snewo
snewo earned 100 total points
ID: 8072739

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
 
LVL 20

Expert Comment

by:jmcg
ID: 10058610
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question