void Job::parsePropertyTree(bpo::variables_map& vm, bpt::ptree& tree) { Info defaults = globalDefaults; if( vm.count( "verbosity" ) > 0 ) { // if --verbosity N is specified, use it. defaults.verbosity = vm["verbosity"].as<int>(); } else defaults.verbosity = Logger::getDefaultLevel(); if( vm.count( "name" ) > 0 ) { defaults.name = vm["name"].as<string>(); } if( vm.count( "log-file" ) ) { vector<string> logfiles = vm["log-file"].as<vector<string>>(); for( auto & filename : logfiles ) { // get first non-empty logfile if( filename.empty() ) continue; defaults.logFile = filename; break; } } else if( !defaults.name.empty() ) { defaults.logFile = defaults.name + ".log"; } info.timeout = tree.get<uint32_t>("TIMEOUT", defaults.timeout); info.priority = tree.get<uint8_t>("PRIORITY", defaults.priority); info.verbosity = tree.get<uint8_t>("VERBOSITY", defaults.verbosity); info.maxThreads = tree.get<uint16_t>("MAX_THREADS", defaults.maxThreads); info.maxPartRetries = tree.get<uint8_t>("MAX_PART_RETRIES", defaults.maxPartRetries); info.logFile = tree.get<string>("LOGFILE", defaults.logFile); info.outputDir = tree.get<string>( "OUTPUT_DIR", defaults.outputDir ); if( info.outputDir.empty() ) info.outputDir = bfs::current_path().string(); info.name = tree.get<string>("NAME", defaults.name); }
/* Function used to check that 'opt1' and 'opt2' are not specified at the same time. */ void conflicting_options(const bpo::variables_map& vm, const char* opt1, const char* opt2) { if (vm.count(opt1) && !vm[opt1].defaulted() && vm.count(opt2) && !vm[opt2].defaulted()) throw std::logic_error(std::string("Conflicting options '") + opt1 + "' and '" + opt2 + "'."); }
/* Function used to check that of 'for_what' is specified, then 'required_option' is specified too. */ void option_dependency(const bpo::variables_map& vm, const char* for_what, const char* required_option) { if (vm.count(for_what) && !vm[for_what].defaulted()) if (vm.count(required_option) == 0 || vm[required_option].defaulted()) throw std::logic_error(std::string("Option '") + for_what + "' requires option '" + required_option + "'."); }
auto readOptions( int argc, char** argv, bpo::variables_map& vm ) { auto desc = bpo::options_description{ "img2term options" }; // register_methods(); desc.add_options() ( "help,h", "Print help" ) ( "method,m", bpo::value<std::string>()->default_value( "color" ), "Method for converting image to term representation." ) ( "step-x,x", bpo::value<int>()->default_value( 10 ), "Width of patches to be represented by char (sequence)." ) ( "step-y,y", bpo::value<int>()->default_value( 20 ), "Height of patches to be represented by char (sequence)." ) ; auto desc_visible = desc; for ( const auto& o : mapping ) desc_visible.add( o.second.second ); ; // positional options, not visible to user desc.add_options() ( "filename", bpo::value< std::vector<std::string> >()->required(), "Input files" ); ; bpo::positional_options_description pos; pos.add( "filename", -1 ); auto print_help = [&desc_visible,&pos] ( auto& stream ) { stream << "\nimg2term [options] filename [filename...]\n" << std::endl << desc_visible << std::endl; }; try { bpo::store( bpo::command_line_parser( argc, argv ) .options( desc ) .allow_unregistered() .positional( pos ) .run() , vm ); if ( vm.count("help") ) { print_help( std::cout ); throw HelpRequested("Asked for help!"); } std::string method = vm["method"].as<std::string>(); const auto& mp = std::find_if( begin( mapping ), end( mapping ), [&method] ( const auto& s ) { return s.first == method; } ); if( mp == end( mapping ) ) { throw bpo::error( "'" + method + "'" + " not a legal choice for --method." ); } else { bpo::store( bpo::command_line_parser( argc, argv ) .options( desc.add( mp->second.second ) ) .positional( pos ) .run() , vm ); } bpo::notify( vm ); } catch ( const HelpRequested& h ) { throw h; } catch ( const bpo::error& e ) { print_help( std::cerr ); std::cerr << e.what() << std::endl; throw e; } return desc; }