[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

C++/linux command line parsing

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
slyph
Asked:
slyph
  • 4
  • 3
  • 2
  • +4
1 Solution
 
AxterCommented:
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
 
AxterCommented:
If this is not what you're looking for, please give more details description.
0
 
slyphAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Why don't you use getopt for your C++ program?
0
 
slyphAuthor Commented:
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
 
AxterCommented:
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
 
slyphAuthor Commented:
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
 
DanRollinsCommented:
>> 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
 
ambienceCommented:
>>  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
 
CoolBreezeCommented:
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
 
DanRollinsCommented:
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
 
griesshCommented:
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
 
MindphaserCommented:
Force accepted

** Mindphaser - Community Support Moderator **

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now