Solved

C++/linux command line parsing

Posted on 2002-05-29
13
263 Views
Last Modified: 2012-05-04
What is the best/easiest way to grab command-line arguments in C++ on a linux box?  I have used getopt with C programs, and I have heard rumors of pure C++ equivalents that return container objects and such.  Does anyone know of a pure C++ function/object/library that I can use to parse my command-line arguments, preferably both the long and short versions (gnu standards).  Something fairly standard would also be nice.
0
Comment
Question by:slyph
  • 4
  • 3
  • 2
  • +4
13 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 33 total points
Comment Utility
If you mean splitting up the command line option that is passed to the program, this is the default behavior in main.

Example:

int main(int argc, char * argv[])
{
}

In above main, argv recieves the command line option parsed.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
If this is not what you're looking for, please give more details description.
0
 

Author Comment

by:slyph
Comment Utility
I am looking for something to parse the inputs so that I don't have to do it manually.  I understand how C++ gets the arguments from the command line through argc and argv, however I don't want to have to write all the code to loop through each of the arguments and determine if it has a parameter and if it exists and so forth.  getopt is a C equivalent of what I want to do:  you just pass it a list of the possible flags and it tells you if one is there and what the resulting argument was (if any).  I am looking for a similar C++ function/class, where I can pass it argc and argv and it will do all of the parsing for me and give me back something nice to handle, like a container class.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
What ever function you use in C, you can use in C++.

Why don't you use getopt for your C++ program?
0
 

Author Comment

by:slyph
Comment Utility
I am just looking for a pure C++ function.  I understand that I can use getopt, but I would like to know if there is a pure C++ equivalent ... I have been on a pure C++ kick lately, none of this C crap for me :)

Anyway, I am asking because I have read about similar classes that provide command line parsing under windows into MFC style CStrings and such, and it sounded a little more useful that getopt.  getopt is great, I am just trying to simplify my life even more.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
FYI:
getopt is not part of the C standards, so it's not a C specific function.  It's an OS/compiler specific function, which can be used on C or C++.

There is no C++ function that will parse data automatically.
But it's not hard to create a small function that can do it.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:slyph
Comment Utility
I know that getopt is a separate GNU function and not a C standard per se, but I have heard enough people use it that I figure that it is a pretty standard part of the linux/C programming community and is located in a commonly installed library, unistd.  I am looking for something like it in C++, which I know exists under Windows for C++ and MFC, but I am asking about linux software.
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
>> I am just looking for a pure C++ function

C is the purest form of C++
Why would you want to mangle clean, tight code such as getopt?  If you have an irresistible urge, you could write a wrapper for it.

You mention CString, so perhaps you will be using MFC.  If so, you might look into CWinApp::ParseCommandLine() and the associated CCommandLineInfo object.  It does not do waht you want, but it is handy for handling all options and parameters in one place.

-- Dan
0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
>>  I would like to know if there is a pure C++ equivalent

slyph , for one thing such a class is not in STL, and this means that you are not going to find it in the standard, why dont you write such a class yourself, you can model the CCommandLineInfo class that MFC provides.

Maybe you write a class that uses getopt internally and provides a C++ style ineterface.

>> I have read about similar classes that provide command line parsing under windows into MFC style CStrings and such

Those classes are not a standard for windows, they are part of MFC and surely anyone working with Borland or anyother Win32 C compiler would either have to suffice with any framework that comes with the compiler ,write themselves or get from any other source.

0
 
LVL 3

Expert Comment

by:CoolBreeze
Comment Utility
he never did say he wanted a standard one, he just said he want a pure C++ equivalent.

according to my understanding, pure != standard
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
There is a little-known trick that can be used for converting filthy C code into pure C++ code:

1) Add this to the top of the file
    //-------------- the following code is PURE C++

2) Rename the file so that it has a filename extension of CPP

3) Stick a fork in yourself, because you are done.

<bows>

-- Dan
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
Dear slyph

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to split between

     "Axter, ambience and DanRollins"

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **

ambience and DanRollins, there will be a separate question with points for your help.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now