Handling of command line options

Source: Internet
Author: User
Tags function prototype string find strtok

Handling the options on the command line is sometimes a problem that is more troubling. First you need to identify the command-line options from the input, and use the combination of options to specify the process that needs to be invoked. command line options in the Unix world are common and vary widely. If the manual to deal with our method will generally first participle, after the lexical analysis. The complexity of the process increases as the number of parameters increases.

int main (int argc, char **argv)
{return
    0;
}
ARGC records the number of parameters, argv is the input string. To argv the use of a split:

      #include <string.h>
       Char *strtok (char *str, const char *delim);
Note that strtok destroys the memory of the original string and requires a copy of Str. After each split, the strtok processing node moves down to determine whether STR is NULL to end the loop. Of course, you can also use the C + + std::string find function to deal with, if you need more powerful boost library boost:: Algorithm::split deal with it. The prototype is as follows:

Namespace boost {
  namespace algorithm {
    template<typename Sequencesequencet, TypeName Ranget, TypeName Predicatet> 
      Sequencesequencet & 
      Split (Sequencesequencet, Ranget, Predicatet, Token_ 
            Compress_mode_type = Token_compress_off);
  }

or directly with Boost::tokenizer, he has a boost::tokenizer::iterator iterator that can decompose the parameters.

Template <
        class Tokenizerfunc = Char_delimiters_separator<char>, 
        class iterator = std::string:: Const_iterator,
        class Type = std::string
  >
  class Tokenizer

All this requires us to deal with these processes. But we are fortunate that getopt can accomplish the functions we need. Getopt in Bash is an external program that provides support for the length of the option. Implements the processing of short and long options through-O and-L.

short_options= "B:r"
long_options= "Back:,restore: opts=

' getopt-o $short _options--long $long _options '

Unistd.h and Getopt.h in C provide support for short and long options, respectively. The function prototype is as follows:

       #include <unistd.h>

       int getopt (int argc, char * const argv[], 
const char *optstring);
       extern char *optarg;
       extern int Optind, opterr, optopt;

       #include <getopt.h>

       int getopt_long (int argc, char * const argv[],
                  const char *optstring,
                  const struct Option *longopts, int *longindex);

       int getopt_long_only (int argc, char * const argv[],
                  const char *optstring,
                  const struct option *longopts, int *l Ongindex);

Use the following:

result = Getopt (argc, argv, "A:B:");
If the failure result is-1, otherwise the option character, Optarg is the option content, optopt is the option.

The third parameter is an option:
A single character, representing an option, followed by a colon after a single character: This option must be followed by an argument. Parameters are immediately followed by an option or separated by a space. The pointer to the parameter is assigned to Optarg.

Most languages are built with getopt support, with similar methods, such as Python

Import Getopt,sys
opts, args = Getopt.getopt (sys.argv[1:], "ho:", ["Help", "output="])  

Now it's easy for us to implement the entry section of a program. But if we need an interactive CLI interface, it needs to be supported by the ReadLine library. He will provide related features including command completion, history, and Shell accelerator support.

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.