tags:

views:

527

answers:

8

How do you pass options to an executable? Is there an easier way than making the options boolean arguments?

EDIT: The last two answers have suggested using arguments. I know I can code a workable solution like that, but I'd rather have them be options.

EDIT2: Per requests for clarification, I'll use this simple example: It's fairly easy to handle arguments because they automatically get parsed into an array.

./printfile file.txt 1000

If I want to know what the name of the file the user wants to print, I access it via argv[1].

Now about how this situation:

./printfile file.txt 1000 --nolinebreaks

The user wants to print the file with no line breaks. This is not required for the program to be able to run (as the filename and number of lines to print are), but the user has the option of using if if s/he would like. Now I could do this using:

./printfile file.txt 1000 true

The usage prompt would inform the user that the third argument is used to determine whether to print the file with line breaks or not. However, this seems rather clumsy.

+2  A: 

I use two methods for passing information:

1/ The use of command line arguments, which are made easier to handle with specific libraries such as getargs.

2/ As environment variables, using getenv.

paxdiablo
+11  A: 

Command-line arguments is the way to go. You may want to consider using Boost.ProgramOptions to simplify this task.

Martin Cote
Definitely like this cross platform suggestion. Thanks!
Scott Saad
A: 

The question isn't blazingly clear as to the context and just what you are trying to do - you mean running an executable from within a C++ program? There are several standard C library functions with names like execl(), execv(), execve(), ... that take the options as strings or pointer to an array of strings. There's also system() which takes a string containing whatever you'd be typing at a bash prompt, options and all.

DarenW
+1  A: 

Pax has the right idea here.

If you need more thorough two-way communication, open the process with pipes and send stuff to stdin/listen on stdout.

Paul Betts
A: 

You can put options in a .ini file and use the GetPrivateProfileXXX API's to create a class that can read the type of program options you're looking for from the .ini.

You can also create an interactive shell for your app to change certain settings real-time.

EDIT: From your edits, can't you just parse each option looking for special keywords associated with that option that are "optional"?

PiNoYBoY82
The GetPrivateProfile APIs are Windows only.
Ferruccio
Yeah, I was thinking I could simply treat some arguments as optional. However, I have to believe there is a more elegant way to do it?
Tim Sally
+1  A: 

You can also use Window's PostMessage() function. This is very handy if the executable you want to send the options to is already running. I can post some example code if you are interested in this technique.

Adam Pierce
+4  A: 

You seem to think that there is some fundamental difference between "options" that start with "--" and "arguments" that don't. The only difference is in how you parse them.

It might be worth your time to look at GNU's getopt()/getopt_long() option parser. It supports passing arguments with options such as --number-of-line-breaks 47.

bk1e
I don't think there is some fundamental difference, I just knew there was a more elegant way to treat them than using boolean arguments or looking for arguments that begin with - or --. getopt() was what i was looking for. Thanks!
Tim Sally
A: 

I like the popt library. It is C, but works fine from C++ as well.

It doesn't appear to be cross-platform though. I found that out when I had to hack out my own API-compatible version of it for a Windows port of some Linux software.

Zan Lynx