Processing of command line options

Source: Internet
Author: User
Tags strtok

The processing of command line options is sometimes a headache. First, you need to identify the command line options from the input, and use the combination of options to find the specific processing process to be called. In the unix world, the command line options are common and very different. If you manually process our methods, we will first perform word segmentation and then perform lexical analysis. The complexity of this process increases with the increase in the number of parameters.

    int main(int argc, char **argv)    {        return 0;    }

Argc records the number of parameters. argv is the input string. Split argv for use:

      #include <string.h>       char *strtok(char *str, const char *delim);

Note that strtok will destroy the memory of the original string and you need to copy the 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 find function of std: string in c ++ for processing. If you need more powerful features, use boost: algorithm: split in the boost library. 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);  }}

You can also directly use boost: tokenizer, which has an iterator for boost: tokenizer: iterator to break down parameters.

template <        class TokenizerFunc = char_delimiters_separator<char>,         class Iterator = std::string::const_iterator,        class Type = std::string  >  class tokenizer

We need to deal with these processes. However, we are lucky to have getopt complete the functions we need. In bash, getopt is an external program that supports long and short options. Use-o and-l to process short and long options.

short_options="b:r"long_options="back:,restore:"opts=`getopt -o $short_options --long $long_options`

In C, unistd. h and getopt. h provide support for short and long options. 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 *longindex);

Use:

result = getopt(argc, argv, "a:b:");

If the failure result is-1, otherwise it is the option character, optarg is the option content, and optopt is the option.

The third parameter is the option:

  1. Single Character, indicating options,
  2. A single character followed by a colon indicates that this option must be followed by a parameter. Parameters are immediately followed by options or separated by spaces. The pointer of this parameter is assigned to optarg.

Most languages have built-in support for getopt, and the usage methods are similar, such as python.

import getopt,sysopts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])  

Now we can easily implement the entry part of a program. But if we need an interactive CLI interface, we need the readline library to support it. It provides functions including command completion, history, and shell shortcut keys.

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.