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. [Cpp] int main (int argc, char ** argv) {return 0;} argc records the number of parameters. argv is the input string. Split argv for use: [cpp] # 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. Prototype: [cpp] namespace boost {namespace algorithm {template <typename schedule, typename RangeT, typename PredicateT> SequenceSequenceT & split (SequenceSequenceT &, RangeT &, PredicateT, token_compress_mode_type = token_compress_off) ;}} or use boost: tokenizer directly. He has an iterator for boost: tokenizer: iterator to break down parameters. [Cpp] template <class TokenizerFunc = char_delimiters_separator <char>, class Iterator = std: string: const_iterator, class Type = std :: string> class tokenizer all of these processes need to be processed. 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. [Plain] short_options = "B: r" long_options = "back:, restore:" opts = 'getopt-o $ short_options -- long $ long_option' unistd in C. h and getopt. h provides support for short and long options. The function prototype is as follows: [cpp] # 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, www.2cto.com 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 the following: [cpp] 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 option: a single character, indicating option, followed by a colon, indicating that the 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. For example, python [python] import getopt, sys opts, 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.