Boost: Library program_options -- Article 1

Source: Internet
Author: User
Program concurrent row processing function: boost program options (1/n)

Generally, a program needs to be a little larger, or be more efficient. Generally, when the program is running, it needs to retrieve some external data from the external database, to set the internal value. Generally speaking, the common method is to directly pass the command line to the lower limit, or, you can retrieve the preset value (for example, INI token) through the iterator.

In the past, although the main function and main () of C ++ have two numbers: argc and argv, you can use the processing command to prompt the number of incoming records for the character, but in reality, you need to handle it by yourself, in fact, when there are many users, it is still quite troublesome. The Program Options function provided by boost C ++ libraries (official file ), basically, it is a function that can be more structured and more systematically processed.

To use boost program options, you must include the following header values:

#include <boost/program_options.hpp>

All the related functions and objects are in the namespace boost: program_options. The basic usage is as follows:

#include <iostream>#include <string>using namespace std;
# Include <boost/program_options.hpp> namespace BPO = boost: program_options;
int main( int argc, char** argv ){  int iValInt;
// Setup program options description BPO: options_description boptions ("test options"); boptions. add_options () ("help", "produce help message") ("Vint", BPO: value <int> (), "int value "); // parse Program Options BPO: variables_map mvmap; BPO: store (BPO: parse_command_line (argc, argv, boptions), mvmap); BPO: Notify (mvmap ); // output help message if required if (mvmap. count ("help") {cout <boptions <Endl; return 1;} // process other option if (mvmap. count ("Vint") {ivalint = mvmap ["Vint"]. as <int> (); cout <"int:" <ivalint <Endl ;}
}

In this example, you can accept the "-- Help" and "-- Vint" numbers during the next row, the end of "-- Vint" must be an integer. The following is an example of a dynamic row:

C:\test.exe --vint 10Int: 10

However, "-- Help" will print the parameter data statement generated by boost program options. The following is the statement output after test.exe -- help:

Test Options:  --help                Produce help message  --vint arg            int value

Next, let's take a look at the program tracing section.

In the above program, we can see that there are four sections added with background coloring. The front-end scheme is the pre-step scheme using boost program options, which basically includes the necessary header scheme, boost/program_options.hpp.

However, because all of its related functions are in the long namespace of Boost: program_options, therefore, this method passes through the namespace alias method (msdn) and converts it into BPO. If you do not want to add namespace at all, you can also use "using namespace boost: program_options;.

The next three sections are the three main steps for using boost Program Options:

  1. Options decription
  2. The root cause is the source (argc and argv) of the root cause command line for analysis, and the result is stored in the map.
  3. Processing and analysis are completed, and there are resources in the map.

The following describes the following parts:

Set the description of the selection.

Boost program options is used to determine the number of parameters, mainly options_description (an official file ).

The options_description object can be written through ostream. It will compile the preset selection description and compile the layout, this is also a great advantage for using boost program options! Therefore, when the options_description object is created (this condition is boptions), you can add a string if necessary, as the most basic description of this selection, Here heresy gives him "test options 」.

When you want to add a new selection definition, you basically use the add_options () function to generate a callable object (official file) of options_description_easy_init ), it is used to quickly set the description of a call number. Through this object, operator () can be used ()), to make quick handling.

In this example, heresy adds two options after calling add_options () and uses two different settings, the difference is help And Vint. However, it should be noted that, in fact, there is no end-to-end splitting (;) for the same line of commands (;), the entire instruction is completed only when all numeric definitions are set.

bOptions.add_options()  ( "help",    "Produce help message" )  ( "vint",    BPO::value<int>(), "int value" );

First, the help option is very simple, that is, two strings. The first string is the selected name, that is, when the command is run, the string to be given ("--" must be added before), and the second string is the description of this parameter. When options_description is written, for example, to explain the meaning and usage of this parameter, you need to explain it here.

When selecting the second Vint, you can see that, compared with the help, there is a "BPO: value <int> ()", value <> () this template function is used to specify the type of the value required for the -- Vint option, where it is specified as int, that is to say, the end of a Vint must be followed by an integer. However, the specified method can be left blank and directly given a value. You can also use "=" to specify it. That is, either of the following methods is acceptable:

--vint 10--vint=10

However, it should be noted that if the form of the given value is not correct (for example, giving it a string) or if there is no value given, boost Program Options generates an exception. If there is no special handling, the program will be disconnected.

In this case, it is basically two different options. In reality, if value <> is used to specify the type, there are more and more convenient functions! But let's wait for the next article ~

Analysis, memory storage and selection

After setting the description of all the options, the next step is to analyze the source of the root metrics ~ The program example is as follows:

BPO::variables_map mVMap;BPO::store( BPO::parse_command_line( argc, argv, bOptions ), mVMap );BPO::notify( mVMap );

Boost Program Options provides different parsers, which can be used to perform analysis based on different data sources, is the limit number of command line, so first use the simplest, parse_command_line () function. Its usage is very simple, as long as the main () argc and argv, as well as the selection description and boptions cited in the previous definition can be used for this function ~ After the analysis is complete, a basic_parsed_options object is returned, Which is the result after the analysis.

However, in this example, we do not need to directly use this object, but first pass the store () function to get the result of the analysis, save objects to a type other than variables_map (mvmap), and then perform the operation.

Finally, remember to pass through policy () again to let the choices in mvmap do the final processing.

Processing and Analysis

After the analysis is complete, the result of the analysis should be handled. All the objects used for access here are the objects (mvmap) that are previously handled and whose type is not variables_map (official file ). Basically, it is a class that inherits from STL map, so the architecture is a key and corresponds to a value. In this case, STD :: all functions of map can also be used.

Generally, the count () function is used to check whether the selected value exists; the help in the preceding example is the following statement:

if( mVMap.count( "help" ) ){  cout << bOptions << endl;  return 1;}

In practice, this sort method is used to determine the number of keys. When the number of help keys in mvmap is not equal to 0 (that is, at least one key ), the boptions are passed through the cout outputs, and the usage instructions are provided to the user.

What if we want to get the value? You can see the section of Vint:

if( mVMap.count( "vint" ) ){  iValInt = mVMap["vint"].as<int>();  cout << "Int: " << iValInt << endl;}

In this case, check count () to confirm the value of the selected Vint, and then extract it through operator; however, the retrieved object is a type other than variable_value (an official file), so you still need to go through the AS <> template function, we need to convert it into the type we need to use. For example, this is because it is an integer, so it is as <int> ~

The basic usage of boost program options is probably here. In fact, he still has many more convenient functions to use! For example, each selection can be set to allow many settings or even preset values, and the description of the selection can also be processed by classification... But when it comes to space issues, we will try again later ~

 

Original: http://viml.nchc.org.tw/blog/paper_info.php? Class_id = 1 & sub_id = 1 & paper_id = 361

Boost: Library program_options -- Article 1

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.