Topic: Command-Line Arguments in C Learning Outcomes: Understand the usefullness of command-line arguments Interpret command line arguments Design test cases for applications of command-line arguments Apply command-line arguments specified in a fixed order Apply command-line arguments specified in an arbitrary order using flags Example: Random ASCII Maze ./amazing1 8 4 @ Command-line arguments can be used to specify parameter values that modify the output of a program. In this example, the three arguments specified the number of rows, number of columns, and wall character used to produce an ASCII art maze. Create a new C program args-example.c to explore how we can gain access to command-line arguments. #include int main(int argc, char *argv[]) { printf("argc is %d\n", argc); return 0; } Exercise 1. Run args-example by typing and entering the following commands: a. ./args-example b. ./args-example 1 2 3 4 c. ./args-example a b c d. ./args-example "a b c" e. ./args-example `ls` f. ./args-example *a g. ./args-example hello goodbye good day bye What seems to be the general pattern for argc? Exercise 2. Modify your program from exercise 1 so it also displays argv[0]. Run all of the calls from exercise 1 again. What is the pattern for argv[0]? Exercise 3. 3. Now modify your program so that it displays every element in argv. You should be able to use argc to help you display all of them with a loop. Run all of the calls from exercise 1 one last time. What is the pattern for argv? Exercise 4. Download amazing1.c Read the introductory header comments. a. Work in pairs to fully enumerate all possible valid ways that arguments can be specified for this program. For each test case, specify an example list of argument values and what values we would expect to have assigned for the program's four parameters num rows, num cols, wall character, and background color code. b. In exercises 2 and 3 we simply printed the string values of the argv[i] elements. Consider again the initial example used to run the maze program. ./amazing1 8 4 @ What must we also do in order to later use the 8 and 4 arguments as integers? Modify your args-example.c once more to this time convert the second and third arguments into integers and printf their values using %d to demonstrate that you have successfully read the maze dimensions as integers. c. Work in pairs to complete the parseArgs function. d. Test your program against the test cases and compare to the expected results. Exercise 5. Download amazing2.c Read the introductory header comments. a. Work in pairs to enumerate several distinct ways that arguments can be specified for this program. For each test case, specify an example list of argument values and what values we would expect to have assigned for the program's four parameters num rows, num cols, wall character, and background color code. Assume that the program will ignore any arguments with unknown flag characters. b. I deliberately omitted specifications for one input scenario where a user could give four valid argument flags, but end up assigning a value to only one of the four maze parameters. What is an example of that situation? How should we clarify the specifications in order to cover this oversight? c. In order to read the values we will need to separate flag portion of a string from the value. Example. -r10 --> After we recognize that we have hyphen and letter r, then we must get the string "10" skipping over "-r" How might we do this? d. Modify arg-example.c to quickly test out your solution ./arg-example -r10 Can you get a string that is only "10"? e. Work in pairs to complete the parseArgs function of amazing2.c f. Test your program against the test cases and compare to the expected results. Optional - Exercise 6. If you finish early, modify amazing1.c or amazing2.c so that the usage function prints a more informative help that describes the meaning of the arguments and allowed range of row/column values and color values. Optional - Exercise 7. If you finish early, modify amazing2.c so that it allows for a fifth command-line argument flag -e that can be used to specify the character symbol used to print the space.