3.2. Option Arguments

As mentioned in Section 2.1, arguments are passed to a command to extend its usefulness. The arguments presented in the last chapter were words, such as foo, bar and foo.c. Standards exist under UNIX for programs to accept command-line option arguments. Option arguments (as the name suggests) are optional. There are two standards, short options and long options. Short options are characters that represent commands, whereas long options contain the entire option name.

Consider the following output of the CATALOG command from ProDOS:


/DEV
NAME        TYPE BLOCKS MODIFIED        CREATED       ENDFILE

FINDER.DATA $C9       1 21-OCT-91 22:38 14-APR-90 18:24   260 
FINDER.ROOT $C9       1 22-OCT-91 17:12  6-OCT-91 15:40    82 
GENESYS     DIR       1 21-OCT-91 23:37 25-APR-91 15:46   512 
GSBUG       DIR       1 21-OCT-91 23:38 19-JUL-90 16:48   512 
MERLIN      DIR       2 22-OCT-91  2:50 30-APR-91 20:21  1024 
LIFEGUARD   $B3      73  4-SEP-87  4:51 25-DEC-89 20:22 36608 
ORCA        DIR       2 22-OCT-91 17:12 14-SEP-89 18:27  1024 
GNO         DIR       2 22-OCT-91 17:12 13-AUG-91 16:36  1024 
FAST.ANIM   DIR       2 21-OCT-91 23:44 11-MAY-91 10:50  1024 
MICOL       DIR       2 22-OCT-91  3:10 14-JAN-90  2:46  1024 
SRC         DIR       1 21-OCT-91 23:44  7-AUG-91 20:30   512 
NIFTYLIST   DIR       2 21-OCT-91 23:44 29-JUL-91  4:04  1024 
MCSRC       DIR       1 21-OCT-91 23:45  7-AUG-91 20:34   512 

BLOCKS FREE:43923     BLOCKS USED:21185    TOTAL BLOCKS:65108

It is impossible to get any variation in the format of this output. While the GNO/ME utility ls serves the same purpose as the command CATALOG from Applesoft BASIC, it has a wide number of options which can tailor the output to specific needs. Here is how ls can be used to give similar output to the CATALOG command:


gno%  ls -l
:dev
total 45k
drw--rd 0000 dir         512 Oct 21 23:45 1991 MCSrc
drw--rd 0000 dir        1024 Oct 21 23:44 1991 NiftyList
drw--rd 0000 dir        1024 Oct 21 23:44 1991 fast.anim
drw--rd 0000 dir         512 Oct 21 23:37 1991 genesys
drw--rd 0000 dir        1024 Oct 22 17:29 1991 gno
drw--rd 0000 dir         512 Oct 21 23:38 1991 gsbug
drw--rd 0000 dir        1024 Oct 22 02:50 1991 merlin
drw--rd 0000 dir        1024 Oct 22 03:10 1991 micol
drw--rd 0100 dir        1024 Oct 22 17:28 1991 orca
drw--rd 0000 dir         512 Oct 21 23:44 1991 src

The -l short option argument tells ls to format the output in long format. ls supports only short options. If ls did support long options, the above command could be changed to ls +format-long. This is clearly more descriptive of what function ls will perform. For users to new to the UNIX environment, long format options are more user-friendly. However, advanced UNIX users prefer short options because of their brevity.

As indicated above, ls has a wide number of options available to format the output. Use the command "ls -?" to get a short list of these options. It is left as an exercise for the user to discover how these options affect the output of ls. For a complete description of the ls command and its options use the command man ls.

As an example of the usage and importance of long options, the following is the result of the +help option given to the coff utility. Note the use of both short and long options:


coff [-OPTIONS] filename [segment..] [loadsegment..]

  OPTIONS        DESCRIPTION
  -v [+version]  display coff's version number
  -D [+default]  disable default options
  -d [+asm]      dump segment body in 65816-format disassembly
  -T [+tool]     interpret Toolbox, GS/OS, ProDOS, ROM calls
  -x [+hex]      dump segment body in hex (can be used with '+asm')
  -l [+label]    print expressions using labels (default is offsets)
  -t [+infix]    display expressions in infix form
  -p [+postfix]  display expressions in postfix form (default)
  -m [+merlin]   format of '+asm' to use merlin opcodes (default)
  -o [+orca]     format of '+asm' to use orca/m opcodes
  -a [+shorta]   assume 8-bit accumulator for disassembly
  -i [+shorti]   assume 8-bit index registers for disassembly
  -s [+header]   dump segment headers only
  -n [+noheader] do not print segment headers
  -f [+nooffset] do not print offset into file
  -h [+help]     print this information, then quit
  filename       name of file to dump
  [segment]      names of segments in file to dump
  [loadsegment]  names of load segments in file to dump

The long options are much more descriptive, and provide a very easy way to remember options of programs. If an option passed to a shell utility program is not understood by that program, you will generally receive an error message stating that the option is not understood. If the program is user-friendly, a brief list of supported options will also be displayed.