Advertisement
oster_man

Possible proto-implementation boost::program_options

Jul 7th, 2025
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | Software | 0 0
  1. #include "sys/error_print.h"
  2. #include "sys/error_code.h"
  3. #include <boost/program_options.hpp>
  4. #include <vector>
  5. #include <expected>
  6.  
  7. namespace parse{
  8.     namespace po = boost::program_options;
  9.  
  10.     template<typename DerivedParser>
  11.     class BaseParser{
  12.         protected:
  13.         po::options_description descriptor_;
  14.         std::vector<std::string> unique_values_;
  15.         BaseParser(const std::string& description):descriptor_(description){
  16.             __init__();
  17.         }
  18.         ~BaseParser() = default;
  19.         virtual void __init__() = 0 noexcept;
  20.  
  21.         virtual ErrorCode __parse__(const std::vector<std::string>& args) noexcept = 0;
  22.  
  23.         public:
  24.  
  25.         static ServerAction& instance(){
  26.             static DerivedParser inst;
  27.             return inst;
  28.         }
  29.  
  30.         const po::options_description& descriptor(){
  31.             return descriptor_;
  32.         }
  33.         virtual ErrorCode parse(const std::vector<std::string>& args) noexcept = 0;
  34.  
  35.         void define_uniques() const{
  36.             unique_values_ = [this](){
  37.                 std::vector<std::string> result;
  38.                 for(auto option:descriptor_.options())
  39.                     result.push_back(option->long_name());
  40.                 return result;
  41.             }();
  42.         }
  43.     };
  44.  
  45.  
  46.     std::expected<po::parsed_options,ErrorCode> try_parse(const po::options_description& opt_desc,const std::vector<std::string>& args){
  47.         try{
  48.             po::parsed_options parsed = po::command_line_parser(args).options(opt_desc).allow_unregistered().run();
  49.             return parsed;
  50.         }
  51.         catch(const po::multiple_occurrences& err){
  52.             return std::unexpected(ErrorPrint::print_error(ErrorCode::TO_MANY_ARGUMENTS,err.what(),AT_ERROR_ACTION::CONTINUE));
  53.         }
  54.         catch(const po::ambiguous_option& err){
  55.             return std::unexpected(ErrorPrint::print_error(ErrorCode::COMMAND_INPUT_X1_ERROR,err.what(),AT_ERROR_ACTION::CONTINUE,err.get_option_name()));
  56.         }
  57.         catch(const po::required_option& err){
  58.             return std::unexpected(ErrorPrint::print_error(ErrorCode::TO_FEW_ARGUMENTS,err.what(),AT_ERROR_ACTION::CONTINUE));
  59.         }
  60.         catch(const po::unknown_option& err){
  61.             return std::unexpected(ErrorPrint::print_error(ErrorCode::COMMAND_INPUT_X1_ERROR,err.what(),AT_ERROR_ACTION::CONTINUE,err.get_option_name()));
  62.         }
  63.         catch(const po::error& err){
  64.             return std::unexpected(ErrorPrint::print_error(ErrorCode::COMMAND_INPUT_X1_ERROR,err.what(),AT_ERROR_ACTION::CONTINUE));
  65.         }
  66.     }
  67.  
  68.     ErrorCode try_notify(po::variables_map& vm){
  69.         try{
  70.             po::notify(vm);
  71.             return ErrorCode::NONE;
  72.         }
  73.         catch(const po::multiple_occurrences& err){
  74.             return ErrorPrint::print_error(ErrorCode::TO_MANY_ARGUMENTS,err.what(),AT_ERROR_ACTION::CONTINUE);
  75.         }
  76.         catch(const po::ambiguous_option& err){
  77.             return ErrorPrint::print_error(ErrorCode::COMMAND_INPUT_X1_ERROR,err.what(),AT_ERROR_ACTION::CONTINUE,err.get_option_name());
  78.         }
  79.         catch(const po::required_option& err){
  80.             return ErrorPrint::print_error(ErrorCode::TO_FEW_ARGUMENTS,err.what(),AT_ERROR_ACTION::CONTINUE);
  81.         }
  82.         catch(const po::unknown_option& err){
  83.             return ErrorPrint::print_error(ErrorCode::COMMAND_INPUT_X1_ERROR,err.what(),AT_ERROR_ACTION::CONTINUE,err.get_option_name());
  84.         }
  85.         catch(const po::error& err){
  86.             return ErrorPrint::print_error(ErrorCode::COMMAND_INPUT_X1_ERROR,err.what(),AT_ERROR_ACTION::CONTINUE);
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement