CSC 161 Grinnell College Fall, 2011 Imperative Problem Solving and Data Structures

# Laboratory Exercise on Command-Line Arguments

### Introduction

Command-line arguments are a great way to give a program input right as the program begins executing. The convention to do this is to give your main function two arguments, argc and argv. argc is an integer related to the number of arguments passed in to the program, and argv is an array of strings related to those arguments, and thus is declared as char * argv [ ]. Let's explore how to use these.

### argc

1. Write a program, args-example.c, in which the main function is in the form: int main (int argc, char *argv[]). The program should simply print out argc.

Compile and make the following calls:

1. ./args-example
2. ./args-example 1 2 3 4
3. ./args-example a b c
4. ./args-example "a b c"
5. ./args-example `ls`
6. ./args-example *a
7. ./args-example hello goodbye good day bye

What seems to be the general pattern for argc?

### argv[0]

You may have noticed that argv is a char* array, or in other words, an array of strings. Let's now look at the first element in that array.

1. Modify your program from exercise 1 so it also displays argv[0]. Then run all of the calls from exercise 1 again. What is the pattern for argv[0]?

### argv[i]

1. 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?

### Commanding the Robot

1. Look over the program skeleton command-robot.c. Make sure you understand what the program is doing. This is just the skeleton to a program you will complete. Now complete the program by filling in the question marks so that it supports the following commands:

• 'l' = turn left
• 'r' = turn right
• 'f' = move forward
• 'b' = move backward
• 's' = change speed

in the following format: cd, where c is a command character and d is a double. Commands will be in the format l3 b0.5 s.2, r1, b.7. It should support the four motion commands and also the 's' command to modify the speed at which the robot will move.

2. Test your program with the following commands and watch the robot to make sure it is working as intended:

1. ./command-robot f3 b1 l.5 s.5 r1 f1
2. ./command-robot s.1 f1 s.3 f1 s.5 f1 s.7 b1 s1 b1
3. ./command-robot f1 r2 b3 l3 s.6 b1 f1

### Commanding with Flags

As you have surely discovered by now, most Linux utilities have "flag" options, where you give the utility a flag when you run it. One example you use whenever you compile is the -o flag, which you use with gcc when compiling a c program if you don't want your executable file to be named "a.out". Such flags are generally in the format -c, where c is the character of a particular flag. It would be useful to implement this with the robot command program.

1. Implement the following five flags for your command-robot program. Your implementation should have a precondition that the flag arguments come before the command arguments.

1. -r — This will reverse the order in which the inputted commands are executed.

2. -s — This will set the the forwardness of the robot to "scribbler-forward".

3. -f — This will set the the forwardness of the robot to "fluke-forward".

4. -h — This will print out help information describing what all of the flag and command options are.

5. -z — This will force the speed to stay at 1 throughout the entire run. The s command (not to be confused with the -s flag) will be unable to change the speed of the robot.

2. Test your program with various inputs and flags to make sure everything works properly.

### For those with extra time

In real utilities, the order and location of flags generally does not matter, and you can even combine flags, so that instead of -c -d, where c and d are two different flags, you can do -cd, or -dc, and the functionality will be the same.

1. Modify your program so that the order and location of flags does not matter. One common method of doing this is going through all of your arguments first to set the flags, and then executing the command arguments.

2. Modify your program so that you can combine flags.

## Reminder: Complete Evaluation Form

When you have finished this lab, be sure to fill out its evaluation form in the "Lab Evaluation" section for CSC 161 on Pioneer Web.