Command-Line Arguments

Summary: In this lab we practice using command-line arguments.

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?

Using command-line arguments

  1. Write a program named sum.c that adds up its command-line arguments, which are assumed to be integers.

    The following input:
    ./sum 8 24 62
    
    should produce the following output:
    Total = 94

    Note: You'll need to convert each command-line argument from string form to integer form. Review the reading from today if you don't remember how to do that.

  2. Write a program, named student-roster.c, which checks a series of strings to see which correspond to the names of students on a roster. Use the following student roster in your code:

    char *students[] = {"Connor", "Luke", "Sam", "Will","Allison", "Maya", "Kelly"};
    
    Your program should consider each command-line argument, comparing it with the strings in the students array. Your program should be flexible in ignoring case. For example, the user might prompt from the command line:
    ./student-roster luke ALlison lucas Connor
    
    should display whether or not each string corresponds to a student in the class:
    luke is in our class 
    allison is in our class
    lucas is not in our class
    connor is in our class
    
    Section 23.5 in our textbook goes over the ctype header, which may be helpful in this problem.

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 clang 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 flags with programs we write.

  1. Implement the following flags for your program from problem 5. Your implementation should have a precondition that the flag arguments come before the command arguments.
    • -c: do not ignore the case of the characters in the input string
    • -r: reverse the order in which the command-line operations are executed
    • -h: print help information describing the flag and command options

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

  2. 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.

    Modify your code from the previous problem 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.

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