Command-Line Arguments
Summary: In this lab we practice using command-line arguments.
Argument Basics
-
Write a program
args-example.cwith amainfunction of the form:int main (int argc, char * argv[]) { ... }This program should simply print the value of
argc.-
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
-
What seems to be the general pattern for
argc? Summarize your observations.
-
Compile your program and and use it to make the
following sequence of calls:
- You may have noticed that
argvis achar*array, or in other words, an array of strings. Let's look at the first element in that array.-
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]?
-
Modify your program from exercise 1 so it also
displays
- Now modify your program so that it displays every element
in
argv.-
You should be able to use
argcto 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?
-
You should be able to use
Using command-line arguments
-
Write a program named
The following input:sum.cthat adds up its command-line arguments, which are assumed to be integers../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.
-
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 thestudentsarray. 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 thectypeheader, 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.
-
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.
-
-
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, wherecanddare 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.
- Modify your program so that you can combine flags.