Boost program_options library, parse command line parameters, read configuration files

Source: Internet
Author: User

I. Command Line Parsing

Sample Code for parsing command line parameters in tprogram_options:

 

[CPP]View plaincopy
  1. # Include <iostream>
  2. Using namespace STD;
  3. # Include <boost/program_options.hpp>
  4. Namespace po = boost: program_options;
  5. Int main (INT argc, char * argv [])
  6. {
  7. // Int level;
  8. Po: options_description DESC ("allowed options ");
  9. Desc. add_options ()
  10. ("Help", "produce help message ")
  11. // ("Help, H", "produce help message ")
  12. ("Compression", Po: value <int> (), "set compression level ");
  13. // ("Compression", Po: value <int> (& level)-> default_value (1), "set compression level ");
  14. Po: variables_map VM;
  15. Po: store (PO: parse_command_line (argc, argv, DESC), Vm );
  16. Po: Notify (VM );
  17. If (VM. Count ("help "))
  18. {
  19. Cout <DESC <Endl;
  20. Return 1;
  21. }
  22. If (VM. Count ("compression "))
  23. {
  24. Cout <"Compression level was set to" <VM ["compression"]. As <int> () <"." <Endl;
  25. // Cout <"Compression level:" <level <Endl;
  26. }
  27. Else
  28. {
  29. Cout <"Compression level was not set." <Endl;
  30. }
  31. Return 0;
  32. }



 

Running result:

 

Input parameter: -- Help

 

Input parameter: -- compression 10

 

2. Read configuration files (for Linux and Windows)

Code 2.1

 

[CPP]View plaincopy
  1. # Include <fstream>
  2. # Include <map>
  3. Using namespace STD;
  4. # Include <boost/program_options.hpp>
  5. Using namespace boost;
  6. Namespace po = boost: program_options;
  7. # Ifdef Win32
  8. # Include "C: \ Users \ gwy8868 \ Desktop \ fast_dr302 \ fast_dr302 \ Global \ xtokens. H"
  9. # Else
  10. # Include "/opt/guowenyan/fast_dr302/global/xtokens. H"
  11. # Endif
  12. STD: pair <STD: String, STD: String> at_option_parser (STD: String const & S)
  13. {
  14. If ('@' = s [0])
  15. {
  16. Return make_pair (STD: string ("Config"), S. substr (1 ));
  17. }
  18. Else
  19. {
  20. Return STD: pair <STD: String, STD: String> ();
  21. }
  22. }
  23. Int main (INT argc, char * argv [])
  24. {
  25. //
  26. String host_ip;
  27. Short host_port;
  28. String server_ip;
  29. Short server_port;
  30. //
  31. Po: options_description hostoptions ("host options ");
  32. Hostoptions. add_options ()
  33. ("Host_ip, H", Po: value <string> (& host_ip), "set db_host ")
  34. ("Host_port, P", Po: value <short> (& host_port)-> default_value (3306), "set db_port ");
  35. Po: options_description General ("general options ");
  36. General. add_options ()
  37. ("Help, H", "produce help message ")
  38. ("Server_ip, s", Po: value <string> (& server_ip), "set the http_server's IP. e.g. '2017. 106.0.20 '")
  39. ("Server_port, P", Po: value <short> (& server_port)-> default_value (80), "set the http_server's port. Default: 80 ");
  40. String config_file;
  41. Po: options_description config ("Config options ");
  42. Config. add_options ()
  43. ("Config", Po: value <string> (& config_file)-> default_value ("config. conf "),
  44. "Set config file, specified with '@ name' too ");
  45. Po: options_description all ("all options ");
  46. All. Add (hostoptions). Add (general). Add (config );
  47. Po: variables_map VM;
  48. Po: store (PO: command_line_parser (argc, argv). Options (all). extra_parser (: at_option_parser). Run (), Vm );
  49. If (VM. Count ("help "))
  50. {
  51. Cout
  52. Cout <general <Endl;
  53. Cout <config <Endl;
  54. Return false;
  55. }
  56. If (VM. Count ("Config "))
  57. {
  58. String conf_name = VM ["Config"]. As <string> ();
  59. Ifstream ifs_config (conf_name.c_str ());
  60. If (! Ifs_config)
  61. {
  62. Cerr <"cocould not open the configure file" <Endl;
  63. Return false;
  64. }
  65. Stringstream ss_config;
  66. Ss_config <ifs_config.rdbuf ();
  67. Global: strings_t ARGs;
  68. Global: separate_tokens (ss_config.str (), argS, "\ r \ n ");
  69. Po: store (PO: command_line_parser (ARGs). Options (all). Run (), Vm );
  70. }
  71. Po: Notify (VM );
  72. //
  73. Cout <"host_ip:"
  74. Cout <"host_port:"
  75. Cout <"server_ip:" <server_ip <Endl;
  76. Cout <"server_port:" <server_port <Endl;
  77. Return 0;
  78. }

2.2 configuration file

 

Config. conf:

Config2.conf:

2.3 output results

 

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.