Ismael
Ismael

Reputation: 197

How to code for optional command line parameters in C

My program has to accept optional command line parameters where there will be either 1 or 3 parameters (executable, number of rows, number of columns). If argc is anything other than 3, we have to create a board that is 10x10 (I've already made a function to create the board).

I'm not sure how to account for this. One restriction on main.c is that we're only allowed to declare/assign variables and call functions, so I'm stuck on how I'm supposed to make this work. My initial idea was to write a function correctNumOfArgs to check if argc is 3, and then return true or false. I would then use that result in a different function to determine how to create the board, but that would still require me to pass argv[1] and argv[2] as parameters in the function and they might not even exist. How do I get around this? Note: I looked at other questions regarding optional command line parameters and they suggested solutions more advanced than what we have learned. Is there a more basic solution to this?

Upvotes: 1

Views: 356

Answers (3)

David C. Rankin
David C. Rankin

Reputation: 84632

You can do it quite easily with variable assignment using a ternary operator to check if argc > 2 and if it is, convert the supplied arguments to rows and cols, otherwise, use the default 10 for each, e.g.

errno = 0;

int rows = argc > 2 ? (int)strtol (argv[1], NULL, 10) : 10,
    cols = argc > 2 ? (int)strtol (argv[2], NULL, 10) : 10;

if (errno) { /* handle error in strtol conversion    */
             /* (you should also check for overflow) */ }

(note: always check for error in the conversion before continuing -- you can move this to a function and fully validate the strtol conversion by checking nptr against endptr to insure characters were converted, and then saving the value to a temporary long value, you can insure the values are between INT_MIN and INT_MAX (and reasonable) before assigning to rows, cols)

If you must use a function, just pass a pointer to rows and cols as parameters and then set the values stored at each respective address within the function. That way you have a simple function call that can return 0/1 for success/failure and update the values for rows, cols through the pointers passed as parameters.

Upvotes: 0

NetMage
NetMage

Reputation: 26926

You can pass both argc and argv to a function and have it return rows and cols.

int main(int argc, char **argv) {
    int rows = 10, cols = 10;
    check_args(argc, argv, &rows, &cols);
    return 0;
}

void check_args(int argc, char **argv, int *rows, int *cols) {
    if (argc == 3) {
        *rows = atoi(argv[1]);
        *cols = atoi(argv[2]);
    }
}

Upvotes: 1

SoronelHaetir
SoronelHaetir

Reputation: 15172

It doesn't matter if the arguments don't exist for a code path you don't take.

What I mean:

if(3==argc)
make_board(argv[1], argv[2]);
else if(1==argc)
make_board("10", "10");
else
{
/*error*/
}

If numArgs is not 3 then that path is not taken so the non-existent arguments aren't used and are never evaluated.

Upvotes: 0

Related Questions