i've never knew how to use command line arguments since the first source code i used to edit(and from i learned basics of c++) was using a config file to 'read' user input data on program startup and not command line arguments.

now my question, what's the difference between using command line arguments and creating a special designed class to read a config file (config.cfg) looking like this:

path = C:\ProgramFiles:\MyProg
logfile = log.txt
savegamepath = C:\ProgramFiles:\MyProg\save
// etc..

i know that on the first thought, the cfg option is far more complicated and timeconsuming, but almost every program needs a config file to read the default values for the data it needs

i ask this cuz i'm not sure if it's worth learning to use command line arguments or not

Recommended Answers

All 12 Replies

i ask this cuz i'm not sure if it's worth learning to use command line arguments or not

Command line arguments are very simple and worth knowing. For example:

#include <iostream>
#include <string>
#include <vector>

int main(int argc, char *argv[])
{
    // Store the arguments in a more convenient data structure
    std::vector<std::string> args(argv, argv + argc);

    ...
}

The arguments correspond exactly to what's typed into the console, which means there's a caveat that args[0] will be the program name in some form[*]. That's really all there is to it. You can quibble about the nuances of the argc and argv parameters, but there's little point in doing so, in my opinion. ;)


[*] "Some form" means it could be an empty string if the program name isn't available, or it could be a full path, or a relative path, or even just the executable name (to list some common results).

Learning to use command-line arguments is definitely part of the basic things you just need to know, even if you have other practices yourself.

As for command-line vs config-file, why not use both? See the Boost.Program-Options library.

Usually command line arguments are meant to pass data into the program, such as
- a file name
- switches that modify execution in some way
- specifying the exact data to be worked on

Config files 'define' the environment for the program. Such as changing the program's default values to user defined values that are set each time the program runs. For example
- folders there data can be found. Define your own folders rather than using the folders the programmer set up.
- Set default parameters to your defaults rather than the programmer's
Things like that.

thx for posting all
could someone tell me how to change the command line arguments? or post a link/tutorial?
thx in advance

You don't generally "change" the command-line arguments. You either specify them or not, as needed and as dictated by the program. If you're running your program by double-clicking on a desktop icon, you don't have a command-line, so you don't get to specify any arguments. But more modern user-interactions methods, such as dragging a text-file-icon onto an editor-application-icon to run the editor program with the text-file pre-loaded for editing is a fancy version of specifying the (optional) text-file path on the command-line when starting the editor.

As far as which to use (assuming you're running from a command-line, such as the "Command Prompt" in Windows or a shell in cygwin or an equivalent terminal window on Mac, Linux, etc.), both have their uses.

In addition to specifying start-up environment/state for a program, a config-file can be made read/write and can store user preferences made while the program is running -- this is more-or-less how all the various apps' info gets saved in the iPhone's "Settings" tool. While the underlying implementation details may differ, you can think of it as reading and writing a config file for each installed app, and then the app also reads its config file on start-up.

The command-line is better suited for information specific to that specific run of the program, but can also be used to override configuration values on an as-needed basis -- faster than editing the config-file and then changing it back later.

As far as the specific arguments to use with a given program, most will provide a usage-statement if you give it something it doesn't understand, or use a standard argument to ask for the usage. DOS applications often use forward-slash to indicate an argument: "dir /w" provides a grid-layout of files in the current directory, while "dir" provides more details and one file per line. "dir /?" displays at least the most common options available. Older Unix utilities often use a single dash followed by one or more option-letters, the newer and more preferred usage is more-readable option strings, each preceded by a double-dash: "ls -l" will provide a long-form directory listing, while "ls --help" will display usage information.

In addition to handling the command-line arguments passed into your program, it's a good idea to understand current common practice for specifying options, and to include a usage statement. There are some APIs for helping with the rather boring task of defining the command-line arguments of a program, and then parsing those arguments at run-time, but I've generally found it simple enough to loop over the arguments and handle each situation correctly.

Some very C-like code I just scrounged up as an example:

static void usage ( const char * progname )
{
    fprintf (
        stderr,
        "usage: %s [options]\n"
        "where options can be any of:\n"
        "    -center <x> <y>\n"
        "    -zoom <zoom>\n"
        "    -maxiters <iters>\n",
        progname
    );
}

int main ( int argc, char * argv[] )
{
    int a;
    double x = 0.0, y = 0.0, z = 1.0;
    unsigned long i = 10;
    for ( a = 1;  a < argc;  ++a )
    {
        const char * arg = argv[a];
        if ( ! strncmp ( arg, "-h", 2 )  ||  ! strncmp ( arg, "-u", 2 ) )
        {
            usage ( argv[0] );
            return 0;
        }
        else if ( ! strncmp ( arg, "-c", 2 ) ) {
            x = atof ( argv[++a] );
            y = atof ( argv[++a] );
        }
        else if ( ! strncmp ( arg, "-z", 2 ) ) {
            z = atof ( argv[++a] );
        }
        else if ( ! strncmp ( arg, "-m", 2 ) ) {
            i = atoi ( argv[++a] );
        }
        else {
            usage ( argv[0] );
            return 1;
        }
    }

    // rest of program goes here

    return 0;
}

i understand how to use command line arguments, i only wanted to know how to specify them;
in that tutorial it says somewhere "Now run the program, adding command line parameters:"
how exactlty you do that?

in that tutorial it says somewhere "Now run the program, adding command line parameters:"
how exactlty you do that?

Do you know how to use the command line? When you type the "command", that's the program name. Any whitespace separated strings after that (using double quotes for embedded whitespace) are subsequent arguments. For example:

$ echo "boogity boogity boo!"

echo is the program (the first argument), and "boogity boogity boo!" is the second argument. Here are a few more examples with the arguments extracted as the C runtime would handle it:

$ ls

argc: 1
argv[0]: "ls"(approximate)
argv[1]: NULL

$ ls -l

argc: 2
argv[0]: "ls"(approximate)
argv[1]: "-l"
argv[2]: NULL

$ ls -R /

argc: 3
argv[0]: "ls"(approximate)
argv[1]: "-R"
argv[2]: "/"
argv[3]: NULL

Most IDEs will support providing command line arguments as a debugging option, but it's best to understand using the command line directly first.

i figured it out thx to you Narue, i appreciate your effort, now, one more last thing, is there any other way to specify the command line arguments than by running the program through the cmd followed by "$ ....."?

is there any other way to specify the command line arguments than by running the program through the cmd followed by "$ ....."?

As my previous post stated, most IDEs will support sending command line arguments to the debugger, but how you do that varies between them. So the answer is yes, but I can't be specific without knowing what compiler and perhaps IDE you use.

i figured it out thx to you Narue, i appreciate your effort, now, one more last thing, is there any other way to specify the command line arguments than by running the program through the cmd followed by "$ ....."?

As mentioned, you should be able to do it through the IDE's execution configuration. If you're dealing with the compiled program through, for example the Windows GUI (don't know what O/S you're on), you can also add your Command Line Arguments to the shortcut's "target" field.

thx, for answers, i got it now, no need for other explanations
ps: i am using windows 7 / MSVC 2010

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.