Friday, January 25, 2013

Command Line Arguments

The ability to accept command line arguments is useful if a program is to have a developer mode or debug mode that wouldn't be suitable in a separate configuration file or in an options menu. By inserting command line arguments in a Window's shortcuts, each shortcut can specify different options, while still pointing to the same executable.

A program ran with a -h or /? argument can provide a standard of assistance to users.

In order for a program to access its command line arguments, the main or winmain function will need a parameter list other than the usual main ( void ).


Parsing Command Line Arguments


To gain access ot the command line arguments the main function will need to accept the paramters passed in by the operating system. The first parameter, generally int argc, is the number of arguments passed. The second parameter, char* argv[ ] or char** argv, is the argument. The argc and argv are the conventional names, though they can be any other valid variable name.

There will always be at least one argument count. The first argument is the path\filename, similar to C:\MyFolder\MyProgram.exe.

Reading Command Line Arguments
main.cpp
//===================================INCLUDES==================================
#include <iostream>
using namespace std;

//==================================MAIN ENTRY=================================
int main ( int argc, char* argv[] )
{
    //Output the total number of command line arguments, argc.
    cout << "There are " << argc << " arguments." << endl;

    //For each argument, argc.
    for ( int i = 0; i < argc; i++ )
    {
        //Output the argument.
        cout << "Argument " << i << ": " << argv[i] << endl;
    }

    return ( 0 );
}

The code above outputs the argument count and the arguments present. For information on running an executable with arguments, see Setting Command Line Arguments, below. If we set the command line argument to -doSomething then we should get the following output from the Reading Command Line Arguments example.




Having the argument parameters at run time, the program can react depending on the arguments passed in.
Parse Command Line Arguments
main.cpp
//===================================INCLUDES==================================
#include <iostream>
#include <string>

using namespace std;

//==================================MAIN ENTRY=================================
int main ( int argc, char* argv[] )
{
    //Test if argument count is more than the path/filename.
    if ( argc > 1 )
    {
        //For each argument entered, after the first.
        for ( int i = 1; i < argc; i++ )
        {

            //Test a certain condition.
            if ( string( argv[i] ) == "-dev" )
            {
                cout << "Developer Mode Activated!" << endl;
            }

            //Test another condition.
            if ( string( argv[i] ) == "-cheats" )
            {
                cout << "Cheats activated!" << endl;
            }

        }
    }

    //Output standard program behavior.
    cout << "Program finished" << endl;

    return ( 0 );
}


The above's output will differ depending on the command line argument passed in. No matter what the program will output the "Program finished" message. If the program is ran with -dev then it will output "Developer Mode Activated!". If the program is ran with -cheats then it will output "Cheats activated!". If both, -dev and -cheats, are added then both of the conditions will occur.

Now, this program is just an example so it doesn't test for variations in upper or lowercase nor does it inform the usue when an argument was attempted but matches none of the conditions such as if -DEV, instead of -dev, nothing happens and the user is not informed that -DEV is not a valid parameter.

Obviously that is a relatively easy fix by saving the argv[ ] to a string and lower-casing it before comparison.

In addition, the program could be made to output a help message if an argument is attempted but is not one of the conditions by changing the ifs structure to an if else or a switch.

Setting Command Line Arguments

Adding Argument to A Shortcut
Since handling a command line argument is only useful if you know how to run a program with one we should discuss how to add the desired argument. Typically the argument is entered after a space in the command to run a program. This can be done in Windows via a shortcut by right clicking on the shortcut, selecting it's properties, and appending the argument to the target field.


So the shortcut's target field would be the filepath\filename argument. For example, the target field might be: C:\CmdLineArguments\Debug\CmdLineArguments.exe MyArgument or C:\CmdLineArguments\Debug\CmdLineArguments.exe -h.

Adding Argument to Visual Studio
It is also useful to add command line arguments to a program that is being actively developed. This will add the special argument each time the program is ran from inside Visual Studio. For Visual Studio 2010, this is done by right-clicking on the Project, selecting properties » Configuration Properties » Debugging » Command Arguments field, where you can enter arguments to be added each run.


1 comment:

  1. Citizen Promaster Titanium Darts
    The online gaming ceramic vs titanium curling iron giant's mission is to deliver best titanium flat iron superior gaming experiences across various mobile devices, including smartphones, tablets,  Rating: titanium cross necklace 4.6 · titanium nipple jewelry ‎56 rainbow titanium reviews

    ReplyDelete