CS-239: Linux Command Line Arguments

JamesMadisonUniversity
The unix/linux tradition is to specify program flags using single letters in command line flag arguments. The following rules specify how command line arguments are interpreted.
  1. A flag argument must begin with a dash (minus sign) ('-').
  2. Each letter after the initial dash in an flag argument specifies a flag (except the special argument -- which just signifies the next argument (if any) is a data argument).
  3. Flags may be simple or parameterized. A parameterized flag is specified with a parameter. A parameterized flag's letter must be specified last in an flag argument and its parameter is specified in the next argument on the command line.
  4. Flag arguments must precede data arguments. Any argument after the first data argument is a data argument, even if it begins with a dash.
  5. The first data argument is either (1) the first argument that is not an flag parameter and that does not begin with a dash or (2) the next argument after the special argument --.

The following examples illustrate the rules above. Some of the examples have multiple command lines listed, all the listed command lines for an example are equivalent.

cp -p filename -newname This example uses the linux cp (copy) program. The cp program's p (preserve) flag is a simple flag. So -p is a flag argument, and the filename and -newname arguments are data arguments (specifying a source and destination file). The program creates a copy of file filename in file -newname, preserving the source file's permissions in the destination file.
ls -w 80 -l ls -lw 80 ls -l -w 80 This example uses the linux ls (list) program. The ls program's l (long) flag is a simple flag and its w (width) flag is a parameterized flag. So -w is a flag argument with 80 being its parameter argument and the -l is also a flag argument. There are no data arguments. The program output a long listing of the current directory formatted for an output width of 80 columns.
sort -nrbT /tmp grades sort -T /tmp -nrb grades sort -n -r -b -T /tmp grades sort -T /tmp -n -r -b -- grades sort -nrb -T /tmp grades This example uses the linux sort program. The sort program's n (numeric), r (reverse), and b (ignore leading blanks) flags are simple flags. The T (temporary directory) flag is a parameterized flag. So -nrbT is a flag argument, /tmp is a parameter argument for the T flag, and grades is a data argument. The program outputs to standard output the file grades, sorted in reverse order using numeric sorting and ignoring leading blanks, using the directory /tmp for any temporary files it must create.
sort -nrTb /tmp grades This call is erroneous because the parameterized flag T is not the last flag specified in the argument -nrTb.
rm -iv -- -file rm -i -v -- -file rm -v -i -- -file This example uses the linux rm (remove) program. The rm program's i (interactive) and v (verbose) flags are simple flags. So -iv is a flag argument and the -file is a data argument. The program removes the file -file, prompting before removing (interactive) and outputting a message each time a file is removed (verbose).
mkdir -vf dir1 dir2 This example uses the linux mkdir (make directory) program. The argument -vf is considered a flag argument but the command line is erroneous because the f flag is not defined for the mkdir program. If the flag argument had been -v instead of -vf, the program would create, verbosely, directories named dir1 and dir2.
mkdir This call is erroneous because the mkdir program expects one or more data arguments and this call specifies no data arguments (or flag arguments).

Note that Java programs run in the Java Virtual Machine. Command lines that execute java programs must precede the program name argument with a java argument. So if the linux cp program had been written in Java, its command-line would look something like this java cp -p filename -newname instead of cp -p filename -newname.

The command line arguments after the program name are passed into a Java program via the String array parameter of the Main method. In the above example, there would be three arguments in the array: the zero'th element in the array would be "-p", the first element would be "filename", and the second element would be "-newname". The first data argument would be the argument with index value 1.