Solved

C++/linux command line parsing

Posted on 2002-05-29
13
266 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
ID: 7042599
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
ID: 7042601
If this is not what you're looking for, please give more details description.
0
 

Author Comment

by:slyph
ID: 7042619
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 30

Expert Comment

by:Axter
ID: 7042648
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
ID: 7042667
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
ID: 7042675
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
 

Author Comment

by:slyph
ID: 7042787
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
ID: 7043469
>> 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
ID: 7043663
>>  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
ID: 7047793
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
ID: 7047899
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
ID: 7178863
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
ID: 7199846
Force accepted

** Mindphaser - Community Support Moderator **

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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 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