I. Command Line Parsing
Sample Code for parsing command line parameters in tprogram_options:
[CPP]View plaincopy
- # Include <iostream>
- Using namespace STD;
- # Include <boost/program_options.hpp>
- Namespace po = boost: program_options;
- Int main (INT argc, char * argv [])
- {
- // Int level;
- Po: options_description DESC ("allowed options ");
- Desc. add_options ()
- ("Help", "produce help message ")
- // ("Help, H", "produce help message ")
- ("Compression", Po: value <int> (), "set compression level ");
- // ("Compression", Po: value <int> (& level)-> default_value (1), "set compression level ");
- Po: variables_map VM;
- Po: store (PO: parse_command_line (argc, argv, DESC), Vm );
- Po: Notify (VM );
- If (VM. Count ("help "))
- {
- Cout <DESC <Endl;
- Return 1;
- }
- If (VM. Count ("compression "))
- {
- Cout <"Compression level was set to" <VM ["compression"]. As <int> () <"." <Endl;
- // Cout <"Compression level:" <level <Endl;
- }
- Else
- {
- Cout <"Compression level was not set." <Endl;
- }
- Return 0;
- }
Running result:
Input parameter: -- Help
Input parameter: -- compression 10
2. Read configuration files (for Linux and Windows)
Code 2.1
[CPP]View plaincopy
- # Include <fstream>
- # Include <map>
- Using namespace STD;
- # Include <boost/program_options.hpp>
- Using namespace boost;
- Namespace po = boost: program_options;
- # Ifdef Win32
- # Include "C: \ Users \ gwy8868 \ Desktop \ fast_dr302 \ fast_dr302 \ Global \ xtokens. H"
- # Else
- # Include "/opt/guowenyan/fast_dr302/global/xtokens. H"
- # Endif
- STD: pair <STD: String, STD: String> at_option_parser (STD: String const & S)
- {
- If ('@' = s [0])
- {
- Return make_pair (STD: string ("Config"), S. substr (1 ));
- }
- Else
- {
- Return STD: pair <STD: String, STD: String> ();
- }
- }
- Int main (INT argc, char * argv [])
- {
- //
- String host_ip;
- Short host_port;
- String server_ip;
- Short server_port;
- //
- Po: options_description hostoptions ("host options ");
- Hostoptions. add_options ()
- ("Host_ip, H", Po: value <string> (& host_ip), "set db_host ")
- ("Host_port, P", Po: value <short> (& host_port)-> default_value (3306), "set db_port ");
- Po: options_description General ("general options ");
- General. add_options ()
- ("Help, H", "produce help message ")
- ("Server_ip, s", Po: value <string> (& server_ip), "set the http_server's IP. e.g. '2017. 106.0.20 '")
- ("Server_port, P", Po: value <short> (& server_port)-> default_value (80), "set the http_server's port. Default: 80 ");
- String config_file;
- Po: options_description config ("Config options ");
- Config. add_options ()
- ("Config", Po: value <string> (& config_file)-> default_value ("config. conf "),
- "Set config file, specified with '@ name' too ");
- Po: options_description all ("all options ");
- All. Add (hostoptions). Add (general). Add (config );
- Po: variables_map VM;
- Po: store (PO: command_line_parser (argc, argv). Options (all). extra_parser (: at_option_parser). Run (), Vm );
- If (VM. Count ("help "))
- {
- Cout
- Cout <general <Endl;
- Cout <config <Endl;
- Return false;
- }
- If (VM. Count ("Config "))
- {
- String conf_name = VM ["Config"]. As <string> ();
- Ifstream ifs_config (conf_name.c_str ());
- If (! Ifs_config)
- {
- Cerr <"cocould not open the configure file" <Endl;
- Return false;
- }
- Stringstream ss_config;
- Ss_config <ifs_config.rdbuf ();
- Global: strings_t ARGs;
- Global: separate_tokens (ss_config.str (), argS, "\ r \ n ");
- Po: store (PO: command_line_parser (ARGs). Options (all). Run (), Vm );
- }
- Po: Notify (VM );
- //
- Cout <"host_ip:"
- Cout <"host_port:"
- Cout <"server_ip:" <server_ip <Endl;
- Cout <"server_port:" <server_port <Endl;
- Return 0;
- }
2.2 configuration file
Config. conf:
Config2.conf:
2.3 output results