GIDNetwork > Command Line Arguments, Part 2
Register
« Beginning Python Tutorial (Part 4) Command Line Arguments, Part 3 »

Command Line Arguments, Part 2

by: WaltP - Sep 05, 2005

Single Characters as Command Line Parameters

A single character as a parameter, although not extremely useful, will give us a basis for later in this topic.

Remember that each parameter is a char*. So to check for a single character, here's a program that will display messages based on a single character as a parameter:

C/CPP/C++ Code Example:

/**********************************************************/
/*  Name: CHR.C                                           */
/*  Implementation: Using the command line parameters,    */
/*           display messages base on characters          */
/*  Syntax:  CHR c1 c2 ... cN                             */
/*  Notes:   This program contains limited error checking */
/*           Additional error checking is left to you     */
/**********************************************************/

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int  param;         // Command line parameter index
    int  ch;            // Character from the command line
    
    if (argc > 1)       // need at least two parameters
    {
        for (param=1; param < argc; param++)
        {  
            ch = argv[param][0];
            switch(ch)
            {
              case '0':
              case '1':
              case '2':
              case '3':
              case '4':
              case '5':
              case '6':
              case '7':
              case '8':
              case '9':
                printf("Parameter %d is the number %c \n", param, ch);
                break;
              case 'A':
              case 'a':
                printf("A is for Apple \n");
                break;
              case 'b':
                printf("Bakers make Pies \n");
                break;
              case 'B':
                printf("Bees make Honey \n");
                break;
            }
        }
    }
    else
    {
        printf("Syntax:  \n");
        printf("    CHR c1 c2 ... cN \n");
    }
    return 0;
}

My output:

Generic Code Example:

D:\GIDNetwork>chr a B 1 2 3 b A
A is for Apple
Bees make Honey
Parameter 3 is the number 1
Parameter 4 is the number 2
Parameter 5 is the number 3
Bakers make Pies
A is for Apple

D:\GIDNetwork>

Notice the use of argv[param][0]. This gets the first character [0] of the current parameter string argv[param]. You can modify the code to process a parameter that is more than 1 character, allowing the parameters to be combined:

Generic Code Example:

D:\GIDNetwork\>chr aB 123 bA
A is for Apple
Bees make Honey
Parameter 3 is the number 1
Parameter 4 is the number 2
Parameter 5 is the number 3
Bakers make Pies
A is for Apple

D:\GIDNetwork>

I leave that exercise to you.

Numbers as Command Line Parameters

This is pretty simple, you've probably figured it out already. Let's create another program to add numbers together:

C/CPP/C++ Code Example:

/**********************************************************/
/*  Name: NUM.C                                           */
/*  Implementation: Using the command line parameters,    */
/*           add a series of numbers together             */
/*  Syntax:  NUM num1 num2 ... numN                       */
/*  Notes:   This program contains limited error checking */
/*           Additional error checking is left to you     */
/**********************************************************/

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int  ans = 0;       // Start the answer at 0
    int  num;           // Command line number
    int  param;         // Command line parameter index
    
    if (argc > 1)       // need at least two parameters
    {
        // start after the program name to the last parameter 
        for (param=1; param < argc; param++)
        {  
            num = atoi(argv[param]);    // convert char* to int
            ans += num;                 // add to the answer
        }
        printf("    The sum is %d \n", ans);
    }
    else
    {
        printf("Syntax:  \n");
        printf("    ADD num1 num2 ... numN \n");
    }
    return ans;
}

My execution:

Generic Code Example:

D:\GIDNetwork>num
Syntax:
    ADD num1 num2 ... numN

D:\GIDNetwork>num 2
    The sum is 2

D:\GIDNetwork>num 2 3 4
    The sum is 9

D:\GIDNetwork>num 2 -3 4
    The sum is 3

D:\GIDNetwork>num 2 -3
    The sum is -1

D:\GIDNetwork>

Of course you can use atol, atof, strtol, or sscanf() or any other functions to do your dirty work.

Putting them together

You can make a simple calculator program from the two techniques above:

C/CPP/C++ Code Example:

/*************************************************************/
/*  Name: EQ.C                                               */
/*  Implementation: Using the command line parameters,       */
/*           add a series of numbers together                */
/*  Syntax:  EQ num1 op1 num2 ... opN numN                   */
/*  Notes:   This program contains limited error checking    */
/*           Additional error checking is left to you        */
/*                                                           */
/*           Spaces must surround each part of the equation. */
/*           The equation is evaluated left to right only.   */
/*           The equation must begin with a number.          */
/*           If two operators are adjacent, only the second  */
/*              is used in the equation.                     */
/*           If two numbers are adjacent, both numbers are   */
/*              used with the previous operator in the       */
/*              equation.                                    */
/*************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
    int  ans = 0;       // Start the answer at 0
    int  num;           // Command line number
    int  param;         // Command line parameter index
    char opr;           // The operator seen
    
    if (argc > 1)       // need at least two parameters
    {
        ans = atoi(argv[1]);                // Get the first number
        for (param=2; param < argc; param++)
        {  
            if (isdigit(argv[param][0]))    // Is this parameter a number?
            {
                num = atoi(argv[param]);    // convert char* to int
                switch(opr)                 // Test the operator
                {
                  case '+':
                    ans += num;             // add to the answer
                    break;
                  case '-':
                    ans -= num;             // sub from the answer
                    break;
                  case '*':
                    ans *= num;             // mult to the answer
                    break;
                  case '/':
                    ans /= num;             // divide into the answer
                    break;
                }
            }
            else
            {                               // This parameter is an operator
                opr = argv[param][0];       // Save it...
            }
        }
        printf("    The answer is %d \n", ans);
    }
    else
    {
        printf("Syntax:  \n");
        printf("    EQ num1 op1 num2 ... opN numN \n");
    }
    return ans;
}

My output:

Generic Code Example:

D:\GIDNetwork>eq 2 + 3 - 1
    The answer is 4

D:\GIDNetwork>eq 2 + 3 * 2
    The answer is 10

D:\GIDNetwork>eq 2 + 3 / 2
    The answer is 2

D:\GIDNetwork>eq 2 + 3 6 / * 2
    The answer is 22

D:\GIDNetwork>

Other operators and functions can of course be added. You can also read each parameter and concatenate them together to make one long string. Then write a parser to process the data, like a full-blown calculator.

Next we look at parameters as switches.

Would you like to comment? This story has been viewed 18,813 times.
« Beginning Python Tutorial (Part 4) Command Line Arguments, Part 3 »

__top__

Copyright © GIDNetwork™ 2001 - 2024

Another website by J de Silva

Page generated in : 0.00850 sec.