Пример #1
0
bool CommandLineArgument::ParseCommandLine(int argc, char** argv) {
	boost::program_options::options_description options_desc("Parsing Building Facade Tools Options");
	options_desc.add_options()
		("help", "print help information.")
		("workplace", boost::program_options::value<std::string>(), "working folder.")
		;

	boost::program_options::variables_map variables_map;
	try
	{
		boost::program_options::store(boost::program_options::parse_command_line(argc, argv, options_desc), variables_map);
		boost::program_options::notify(variables_map);

		if(variables_map.count("help") || variables_map.size() == 0) {
			std::cout << options_desc << std::endl;
			return false;
		}
		if(variables_map.count("workplace")) {
			working_folder = variables_map["workplace"].as<std::string>();
		}
		else
		{
			return false;
		}
	}
	catch(boost::program_options::error& e) 
	{
		std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
		std::cerr << options_desc << std::endl;
		return false;
	}
	return true;
}
Пример #2
0
int mainEntryClickHouseExtractFromConfig(int argc, char ** argv)
{
    bool print_stacktrace = false;
    bool process_zk_includes = false;
    bool try_get = false;
    std::string log_level;
    std::string config_path;
    std::string key;

    namespace po = boost::program_options;

    po::options_description options_desc("Allowed options");
    options_desc.add_options()
        ("help", "produce this help message")
        ("stacktrace", po::bool_switch(&print_stacktrace), "print stack traces of exceptions")
        ("process-zk-includes", po::bool_switch(&process_zk_includes),
         "if there are from_zk elements in config, connect to ZooKeeper and process them")
        ("try", po::bool_switch(&try_get), "Do not warn about missing keys")
        ("log-level", po::value<std::string>(&log_level)->default_value("error"), "log level")
        ("config-file,c", po::value<std::string>(&config_path)->required(), "path to config file")
        ("key,k", po::value<std::string>(&key)->required(), "key to get value for");

    po::positional_options_description positional_desc;
    positional_desc.add("config-file", 1);
    positional_desc.add("key", 1);

    try
    {
        po::variables_map options;
        po::store(po::command_line_parser(argc, argv).options(options_desc).positional(positional_desc).run(), options);

        if (options.count("help"))
        {
            std::cerr << "Preprocess config file and extract value of the given key." << std::endl
                << std::endl;
            std::cerr << "Usage: clickhouse extract-from-config [options]" << std::endl
                << std::endl;
            std::cerr << options_desc << std::endl;
            return 0;
        }

        po::notify(options);

        setupLogging(log_level);
        std::cout << extractFromConfig(config_path, key, process_zk_includes, try_get) << std::endl;
    }
    catch (...)
    {
        std::cerr << DB::getCurrentExceptionMessage(print_stacktrace, true) << std::endl;
        return DB::getCurrentExceptionCode();
    }

    return 0;
}
Пример #3
0
int main(int argc, const char *const *argv) {
	bool is_server = false;
	bool is_client = false;
	std::string ip_str;
	int port;
	// Parse command options
	po::options_description options_desc("Allowed Options");
	options_desc.add_options()
		("help", "produce help message")
		("listen_ip", po::value<std::string>(), "if starts like a server,need a listen ip and port")
		("listen_port", po::value<int>(), "if starts like a server,need a listen port and ip")
		("conn_ip", po::value<std::string>(), "connect server ip")
		("conn_port", po::value<int>(), "connect server port");

	po::variables_map vm;
	po::store(po::parse_command_line(argc, argv, options_desc), vm);
	po::notify(vm);

	if (argc <= 1 || vm.count("help")) {
		usage();
		std::cout << options_desc << std::endl;
		return OK;
	}

	if (vm.count("listen_ip")) {
		ip_str = vm["listen_ip"].as<std::string>();
		if (!check_ip(ip_str)) {
			std::cout << "IP ERROR" << std::endl;
			return -1;
		}
		is_server = true;
	}

	if (vm.count("listen_port")) {
		port = vm["listen_port"].as<int>();
		if (!check_port(port)) {
			std::cout << "PORT ERROR" << std::endl;
			return -1;
		}

		if (vm.count("conn_ip")) {
			is_client = true;
		}

		if (!is_server) {
			std::cout << "HAS NO LISTEN IP!" << std::endl;
		}
	}

	// starts a server
	boost::shared_ptr<CTcpServer> server_ptr = NULL;
	boost::shared_ptr<CUdpServer> udp_server_ptr = NULL;
	if (is_server) {
		boost::asio::ip::address ip_addr = boost::asio::ip::make_address(ip_str);
		server_ptr = boost::make_shared<CTcpServer>(ip_addr, static_cast<unsigned short>(port));
		server_ptr->run();

		udp_server_ptr = boost::make_shared<CUdpServer>(ip_addr, static_cast<unsigned short>(port));
		udp_server_ptr->run();
	}

	// starts a client
	boost::shared_ptr<CTcpClient> client_ptr = NULL;
	if (is_client) {
		std::string client_ip_str(vm["conn_ip"].as<std::string>());
		client_ptr = boost::make_shared<CTcpClient>(client_ip_str, vm["conn_port"].as<int>());
		client_ptr->async_connect();
	}

	common_io_context.run();

	return OK;
}