Linux Today: Linux News On Internet Time.

More on LinuxToday

LinuxProgramming: Hello Universe 2: Mastering the Command Line

Jun 20, 2000, 13:53 (0 Talkback[s])
(Other stories by Lou Grinzo)


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

[ Thanks to Kevin Reichard for this link. ]

"In the abstract, this "ignore command-line options" approach is appealing--after all, what's the point of festooning our programs with all those widgets, except to give the user complete control over the program? But once we move from fuzzy abstracts like "all GUI applications" to specific programs with real needs, the scene changes radically, and before you can say "user request" you're faced with a slew of minor issues, some of which can best be solved with good old-fashioned command-line processing."

"To no one's surprise, there are several ways to tiptoe through command-line options under Linux. You can manually process the argc/argv variable passed to your main() function, if you like, but that's a pain, it's another opportunity to add obscure bugs to your program, and it can lead to code maintenance hassles when you (inevitably) add new features to your program. A more programmer-friendly approach is to use Linux's getopt_long() function, which is what I did in this installment of Hello Universe to give the user some control over the font and colors the program uses."

"There are essentially two kinds of options you need to worry about, and options can either take an argument (as opposed to causing one) or not, for a total of four combinations. I can't begin to provide a comprehensive treatment of command line processing here, so I'll present just a basic technique that requires an argument for each option, but lets you use either a single dash and letter ("-h") or a double dash and a complete word ("--help") for the options."

Complete Story

Related Stories: