Ace_get_opt parsing the command line

Source: Internet
Author: User

Ace_get_opt is an iterator that parses command-line parameter options.

1: Construction method

Ace_get_opt needs to refer to the header file, #include "Ace/get_opt.h".

ace_get_opt (int  argc,               * *argv               ,const Ace_tchar *optstring = Ace_text ( "" ),               int1,               int0,               int ordering = Permute_args,               int0);

  

argc and argv are the parameters of the program entry function Main method, which is the command line of the program.

optstring is a specified parameter option, and is a character as an option, consisting mainly of three forms

      • A simple character option, such as S, that indicates that an option cannot be added after this option
      • A character option followed by a colon, such as S:, indicates that there will be a parameter after this option
      • A character followed by two colons, such as S::, indicating that this option can have parameters or no parameters

Skip_args means starting with the first element of argv, the default is 1, and generally argv[0] is the path of the program

report_errors Whether an error is indicated when an unrecognized parameter is encountered

long_only Indicates whether to include only the option parameters of the string.

The following explains the character options and string options, that is, short option and long option.

    • Short option starts with the character '-', such as-s
    • long option starts with two characters '-', than into--server

This is in the case of the default, which is Long_only = 0. And when long_only is not equal to 0, you can use-server to represent long option.

2: Operation

Traversing the command line to take out a parameter item depends on 2 basic operations

   /*    * Scan elements of @a argv (whose length is @a argc) for short option   * characters given in @a optstring or L Ong options (with no short   * option equivalents).    */   int operator () (void);    /*    * For communication from @c operator () to the caller.  When   * @c operator () finds a option that takes a argument, the argument   * value is returned from this method, Otherwise it returns 0.    */ *opt_arg (voidconst;   

int operator () (void), used to traverse a parameter item

The opt_arg is used to remove the value of the parameter entry.

3: Sample Program

Command-line arguments: ACE_TEST.EXE-E 2000-i 1000

#include"ace/get_opt.h"#include"ace/os_ns_unistd.h"#include"ace/os_ns_sys_time.h"#include<iostream>ConstAce_time_value Max_interval ( -* -);intMainintargcChar**argv) {Ace_time_value Expiration=Ace_os::gettimeofday ();    Ace_time_value interval; Ace_get_opt Opt (ARGC,ARGV,"e:i:");  for(intc = (c = opt ())! =-1;) {        Switch(c) { Case 'e': Expiration + =Ace_time_value (Atoi (Opt.opt_arg ()));  Break;  Case 'I': Interval =Ace_time_value (Atoi (Opt.opt_arg ()));  Break; }    }    if(Interval >max_interval) {cout<<"interval must is less than"<<max_interval.sec () <<Endl; }    Else if(Expiration > (Ace_time_value::max_time-interval)) {cout<<"expiration + interval must is less than"<<ace_time_value::max_time.sec () <<Endl; }    return 0; }

Partially reproduced from http://www.cnblogs.com/hbccdf/p/use_acegetopt_parse_commandline.html.

Ace_get_opt parsing the command line

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.