Solved

C++/linux command line parsing

Posted on 2002-05-29
13
273 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
[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
  • 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
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

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cross compile release version of c++ program for linux 2 139
Getting IP address 8 104
How to convert MFC::CString to UTF8 wchar_t* 10 388
FMX TCameraComponent Problem 2 93
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

730 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