Wednesday, May 1, 2019

Command Line Arguments Revamped

Revisiting the Command Line Argument article, there are some more enhancements can be made to the longevity of the code. For example, we can make a typedef for a return code so that if we want to change the type, we can do it in one location and have all uses of it change as well.

We can also offload the parsing of the command line into its own function, away from cluttering up the main. The trade off is that adding a new recognized argument must take into account the struct CommandLineArgs_t, having an associated Boolean flag.

Reading Command Line Arguments
main.cpp
//----------------------------------INCLUDES-----------------------------------
#include <iostream>
#include <string>

using namespace std;
//----------------------------------TYPEDEFS-----------------------------------
//return_code explicitly for program and associated return codes

typedef int returnCode_t;

//argumentsCount defined for argc
typedef unsigned int argsCount_t;

//command line argument flags
struct CommandLineArgs_t {
    bool bDev;
    bool bAdmin;
};

//-----------------------------FUNCTION PROTOTYPES-----------------------------
void ParseCommandLineArguments(argsCount_t argc, char** argv, CommandLineArgs_t &cla);

//----------------------------------MAIN ENTRY---------------------------------
returnCode_t main( argsCount_t argc, char** argv ) {
    //return code set to one and explicitly changed just prior to normal exit
    returnCode_t isError = 1;
    //used for pausing just prior to exit
    char pause;

    //check command line arguments
    CommandLineArgs_t commandLineArgs;
    ParseCommandLineArguments( argc, argv, commandLineArgs );

    //report command line arguments to user
    if ( commandLineArgs.bDev ) {
        cout << "DEV_MODE is ON\n";
    }
    if ( commandLineArgs.bAdmin ) {
        cout << "ADMIN is ON\n";
    }

    //normal operations

    if ( commandLineArgs.bDev ) {
        cout << "All mode output.\n";
    }

    cout << "Enter any character to exit: ";
    cin >> pause;

    //set isError to zero just prior to normal exit
    isError = 0;
    return isError;
}
//=============================FUNCTION DEFINITIONS============================
//-----------------------------------------------------------------------------
//Function: ParseCommandLineArguments
//Description: Parses command line arguments, argv, of count, argc. Values
//    are set in the passed CommandLineArgs_t, which can then be
//    read from outside this function to determine if options
//    such as dev or admin mode are enabled.
//-----------------------------------------------------------------------------
void ParseCommandLineArguments( argsCount_t argc, char** argv, CommandLineArgs_t &cla ) {
    //argc is the count of arguments passed
    //argv is each of the arguments
    //initialize default values to zero
    cla.bDev = false;
    cla.bAdmin = false

    //loop through all arguments passed and apply it to cla's members, as applicable
    for( argsCount_t i = 0 ; i < argc ; ++i ) {
        if ( argv[i] == "-dev" || "dev" ) {
            cla.bDev = true;
        }
        if ( argv[i] == "-admin" || "admin" ) {
            cla.bAdmin = true;
        }
    }
    if ( cla.bDev == true ) {
        //0th element of argv is program's path and name (or null if OS moody)
        if ( argv[0][0] != NULL ) {
            cout << "Path reported: " << argv[0] << '\n';
        }
    cout << "Total arguments passed:" << argc << '\n';
    }
}


No comments:

Post a Comment