Required and Optional Arguments Using Boost Library Program Options

后端 未结 4 988
情话喂你
情话喂你 2020-12-12 15:07

I\'m using Boost Program Options Library to parse the command line arguments.

I have the following requirements:

  1. Once \"help\" is provided, all the oth
4条回答
  •  暖寄归人
    2020-12-12 15:11

    Here is complete program as per rcollyer and Tim, whom the credits go to:

    #include 
    #include 
    #include 
    namespace po = boost::program_options;
    
    bool process_command_line(int argc, char** argv,
                              std::string& host,
                              std::string& port,
                              std::string& configDir)
    {
        int iport;
    
        try
        {
            po::options_description desc("Program Usage", 1024, 512);
            desc.add_options()
              ("help",     "produce help message")
              ("host,h",   po::value(&host)->required(),      "set the host server")
              ("port,p",   po::value(&iport)->required(),             "set the server port")
              ("config,c", po::value(&configDir)->required(), "set the config path")
            ;
    
            po::variables_map vm;
            po::store(po::parse_command_line(argc, argv, desc), vm);
    
            if (vm.count("help"))
            {
                std::cout << desc << "\n";
                return false;
            }
    
            // There must be an easy way to handle the relationship between the
            // option "help" and "host"-"port"-"config"
            // Yes, the magic is putting the po::notify after "help" option check
            po::notify(vm);
        }
        catch(std::exception& e)
        {
            std::cerr << "Error: " << e.what() << "\n";
            return false;
        }
        catch(...)
        {
            std::cerr << "Unknown error!" << "\n";
            return false;
        }
    
        std::stringstream ss;
        ss << iport;
        port = ss.str();
    
        return true;
    }
    
    int main(int argc, char** argv)
    {
      std::string host;
      std::string port;
      std::string configDir;
    
      bool result = process_command_line(argc, argv, host, port, configDir);
      if (!result)
          return 1;
    
      // else
      std::cout << "host:\t"   << host      << "\n";
      std::cout << "port:\t"   << port      << "\n";
      std::cout << "config:\t" << configDir << "\n";
    
      // Do the main routine here
    }
    
    /* Sample output:
    
    C:\Debug>boost.exe --help
    Program Usage:
      --help                produce help message
      -h [ --host ] arg     set the host server
      -p [ --port ] arg     set the server port
      -c [ --config ] arg   set the config path
    
    
    C:\Debug>boost.exe
    Error: missing required option config
    
    C:\Debug>boost.exe --host localhost
    Error: missing required option config
    
    C:\Debug>boost.exe --config .
    Error: missing required option host
    
    C:\Debug>boost.exe --config . --help
    Program Usage:
      --help                produce help message
      -h [ --host ] arg     set the host server
      -p [ --port ] arg     set the server port
      -c [ --config ] arg   set the config path
    
    
    C:\Debug>boost.exe --host 127.0.0.1 --port 31528 --config .
    host:   127.0.0.1
    port:   31528
    config: .
    
    C:\Debug>boost.exe -h 127.0.0.1 -p 31528 -c .
    host:   127.0.0.1
    port:   31528
    config: .
    */
    

提交回复
热议问题