[cs631apue] handling multiple command options

Jan Schaumann jschauma at stevens.edu
Sun Oct 9 15:41:44 EDT 2016


mseaton <mseaton at stevens.edu> wrote:

> I am using getopt, works well for single command options which I have 
> implemented. However I am having a lot of difficulty handling multiple 
> options like ls -la for example. How to combine these two options 
> together to produce the desired output?

getopt(3) will process them one by one and do the right thing regardless
of whether '-la' or '-l -a' was given.  The manual page for getopt(3)
contains the canonical example for how to process command-line flags by
iterating over whatever getopt(3) gives you and setting internal
variables accordingly.

> another question I have in regard to getopt is how does it handle file 
> names?

getopt(3) does not handle file names.  It only processes flags.

> I have separately written a function which handles all 
> combinations of file name operands without command options.

I'm not sure I understand.  The ls(1) command you're implementing does
not have any flags that accept a filename as a specific argument for
just that flag.  After all command-line options are processed, the
remaining arguments are to be treated as pathnames, which need to be
processed.

-Jan


More information about the cs631apue mailing list