command line handling using getopt (), Getopt_long (), Getopt_long_only ()

Source: Internet
Author: User
Tags web services

Introduction

In early UNIX, its command-line environment (the only user interface at the time) contained dozens of small text-processing tools. These tools are very small and usually do a good job. These tools are chained together through longer command lines, and the previous program passes its output to the next program as input, and the entire process is controlled by various command-line options and parameters.

It is this aspect of UNIX that makes it an extremely powerful environment for processing data based on this article, which is one of its original uses in the corporate environment. Enter some text at one end of the command pipe, and then retrieve the processed output at the other end.

command-line options and parameters control UNIX programs, telling them how to act. As a developer, you are responsible for discovering the user's intent from the command line that is passed to your program's main () function. This article demonstrates how to use the standard getopt () and Getopt_long () functions to simplify command-line processing and discuss a technique for tracking command-line options.

Before you start

The sample code included in this article (see download) is written in Eclipse 3.1 using the C development tool (c Development tooling,cdt); the Getopt_demo and Getopt_long_demo projects are Managed make projects, are built using the CDT's program generation rules. There is no Makefile in the project, and if you need to compile the code outside of Eclipse, you can easily build one yourself.

If you have not tried Eclipse (see Resources), you should really try-this is an excellent integrated development environment (integrated development environment,ide), with a significant increase in each new version. This is the work of "hard-line" EMACS and Makefile developers.

Command line

One of the first hurdles to writing a new program is how to handle command-line arguments that control its behavior. This includes an integer count (usually named ARGC) and an array of pointers to the string (usually named argv) that is passed from the command line to your program's main () function. You can declare the callout main () function in two essential ways, as shown in Listing 1.

Listing 1. Two ways to declare the main () function

int main (int argc, char *argv[]);

int main (int argc, char **argv);

The first way to use a pointer to a char pointer array, which now seems very popular, is slightly clearer than the second (whose pointer points to multiple pointers to char). For some reason, I spent more time in the second way, which may have been the result of my hard learning of the C pointer in high school. Both methods are the same for all purposes and purposes, so you can use one of your favorite ways.

The command line has been processed when the program startup code for the C Run-time Library calls your main (). The ARGC parameter contains the count value of the parameter, and argv contains an array of pointers to those parameters. For the C Run-time Library, arguments is the name of the program, and any content after the program name should be delimited with a space.

For example, if you use the parameter-V bar www.ibm.com to run a program named Foo, your argc will be set to 4,ARGV as shown in Listing 2.

Listing 2. The content of argv

Argv[0]-Foo

ARGV[1]---V

ARGV[2]-Bar

ARGV[3]-www.ibm.com

A program has only one set of command-line arguments, so I want to store this information in the global structure of the logging options and settings. Anything that makes sense to the program can be logged into this structure, and I'll use the structure to help reduce the number of global variables. As I mentioned in the Web Services design article (see Resources), global variables are very inappropriate for threading programming, so use them sparingly.

The sample code demonstrates the command-line processing of a hypothetical doc2html program. The doc2html program converts some type of document to HTML, which is controlled by the command-line options specified by the user. It supports the following options:

-i--does not create a keyword index.

-L lang--is converted to the language specified using language code lang.

-O outfile.html--writes converted documents to outfile.html instead of printing to standard output.

-v--provides detailed information when converting, and can be specified multiple times to increase the diagnostic level.

Other file names will be used as input documents.

You will also support-H and-? To print a help message to indicate the purpose of each option.

Simple command line processing: getopt ()

The getopt () function is located in the Unistd.h system header file, and its prototype is shown in Listing 3:

Listing 3. Getopt () prototype

int getopt (int argc, char *const argv[], const char *optstring);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.