Command-Line Arguments

Summary: We practice using command-line arguments and use them to drive the robot.

Argument Basics

  1. Write a program args-example.c with a main function of the form:
    int
    main (int argc, char * argv[])
    {
        ...
    }

    This program should simply print the value of argc.

    1. Compile your program and and use it to make the following sequence of calls:
      ./args-example
      ./args-example 1 2 3 4
      ./args-example a b c
      ./args-example "a b c"
      ./args-example `ls`
      ./args-example a*
      ./args-example hello goodbye good day bye
    2. What seems to be the general pattern for argc? Summarize your observations.

  2. You may have noticed that argv is a char* array, or in other words, an array of strings. Let's look at the first element in that array.
    1. Modify your program from exercise 1 so it also displays argv[0].
    2. Run all of the calls from exercise 1 again. What is the pattern for argv[0]?

  3. Now modify your program so that it displays every element in argv.
    1. You should be able to use argc to help you display all of them with a loop.
    2. Run all of the calls from exercise 1 one last time. What is the pattern for argv?

Commanding the Robot

  1. Copy the program skeleton command-robot.c to your account. This is just the skeleton to a program you will complete to support the commands
    • l: turn left
    • r: turn right
    • f: move forward
    • b: move backward
    • s: change speed

    of the form cd, where c is a command character and d is a double. For example, a command invocation might be

    ./command-robot l3 b0.5 s.2 r1 b.7
    1. Inspect the skeleton program, making sure you understand what the program does (so far).
    2. Replace the portions marked ??? to complete the program. The resulting program should support the four motion commands listed above and also the s command to modify the speed at which the robot will move.

    Hint: C's standard library (<stdlib.h>) contains functions atoi and atof that convert strings to integers and doubles, respectively. Use the man utility in a terminal for details.

  2. Test your program with the following commands and watch the robot to make sure it is working as intended:
    ./command-robot f3 b1 l.5 s.5 r1 f1
    ./command-robot s.1 f1 s.3 f1 s.5 f1 s.7 b1 s1 b1
    ./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.
    • -r: reverse the order in which the input commands are executed
    • -s: set the the forwardness of the robot to "scribbler-forward"
    • -f: set the the forwardness of the robot to "fluke-forward"
    • -h: print help information describing the flag and command options
    • -z: force the speed to stay at 1 throughout the entire run; subsequent s commands (not to be confused with the -s flag) have no effect
  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 type either -cd, or -dc, and the functionality will be the same.

  1. Modify your command-robot.c 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.