[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


C++/linux command line parsing

Posted on 2002-05-29
Medium Priority
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.
Question by:slyph
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
  • 4
  • 3
  • 2
  • +4
LVL 30

Accepted Solution

Axter earned 132 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.


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

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

Expert Comment

ID: 7042601
If this is not what you're looking for, please give more details description.

Author Comment

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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 30

Expert Comment

ID: 7042648
What ever function you use in C, you can use in C++.

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

Author Comment

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.
LVL 30

Expert Comment

ID: 7042675
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.

Author Comment

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.
LVL 49

Expert Comment

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

Expert Comment

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.


Expert Comment

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

Expert Comment

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.


-- Dan
LVL 11

Expert Comment

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/


Expert Comment

ID: 7199846
Force accepted

** Mindphaser - Community Support Moderator **

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

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

650 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