pgsql2sqlite: better error output, limit 'table' param to flag option rather than positional arg so the two do not conflict

This commit is contained in:
Dane Springmeyer 2011-04-04 03:48:39 +00:00
parent 7d6ed703f4
commit a73bb9d2cd

View File

@ -35,78 +35,89 @@
//stl
#include <iostream>
#include <fstream>
#include <exception>
int main ( int argc, char** argv)
{
namespace po = boost::program_options;
po::options_description desc("Postgresql/PostGIS to SQLite3 converter\n Options");
desc.add_options()
("help,?","Display this help screen.")
("host,h",po::value<std::string>(),"Allows you to specify connection to a database on a machine other than the default.")
("port,p",po::value<std::string>(),"Allows you to specify a database port other than the default.")
("user,u",po::value<std::string>(),"Connect to the database as the specified user.")
("dbname,d",po::value<std::string>(),"postgresql database name")
("password,P",po::value<std::string>(),"Connect to the database with the specified password.")
("query,q",po::value<std::string>(),"Name of the table/or query to pass to postmaster")
("table,t",po::value<std::string>(),"Name of the table to create")
("file,f",po::value<std::string>(),"Use this option to specify the name of the file to create.")
;
po::positional_options_description p;
p.add("table",1);
po::variables_map vm;
std::string usage = "usage: pgsql2sqlite --dbname db --table planet_osm_line --file osm.sqlite --query \"select * from planet_osm_line\"";
try
{
po::store(po::command_line_parser(argc,argv).options(desc).positional(p).run(),vm);
desc.add_options()
("help,?","Display this help screen.")
("host,h",po::value<std::string>(),"Allows you to specify connection to a database on a machine other than the default.")
("port,p",po::value<std::string>(),"Allows you to specify a database port other than the default.")
("user,u",po::value<std::string>(),"Connect to the database as the specified user.")
("dbname,d",po::value<std::string>(),"postgresql database name")
("password,P",po::value<std::string>(),"Connect to the database with the specified password.")
("query,q",po::value<std::string>(),"Name of the table/or query to pass to postmaster")
("table,t",po::value<std::string>(),"Name of the output table to create (default: table in query)")
("file,f",po::value<std::string>(),"Use this option to specify the name of the file to create.")
;
//po::positional_options_description p;
//p.add("table",1);
po::variables_map vm;
//positional(p)
po::store(po::command_line_parser(argc,argv).options(desc).run(),vm);
po::notify(vm);
if (vm.count("help") || !vm.count("file") || !vm.count("query"))
if (vm.count("help"))
{
std::cout << desc << "\n";
std::cout << usage << "\n";
return EXIT_SUCCESS;
}
}
catch (...)
{
std::cout << desc << "\n";
return EXIT_FAILURE;
}
else if ( !vm.count("dbname") || !vm.count("file") || !vm.count("query") )
{
std::cout << desc << "\n";
std::cout << usage << "\n";
std::cout << "Both --dbname, --file and, --query are required\n";
return EXIT_FAILURE;
}
boost::optional<std::string> host;
boost::optional<std::string> port ;
boost::optional<std::string> dbname;
boost::optional<std::string> user;
boost::optional<std::string> password;
boost::optional<std::string> connect_timeout("4");
boost::optional<std::string> host;
boost::optional<std::string> port ;
boost::optional<std::string> dbname;
boost::optional<std::string> user;
boost::optional<std::string> password;
boost::optional<std::string> connect_timeout("4");
if (vm.count("host")) host = vm["host"].as<std::string>();
if (vm.count("port")) port = vm["port"].as<std::string>();
if (vm.count("dbname")) dbname = vm["dbname"].as<std::string>();
if (vm.count("user")) user = vm["user"].as<std::string>();
if (vm.count("password")) password = vm["password"].as<std::string>();
ConnectionCreator<Connection> creator(host,port,dbname,user,password,connect_timeout);
try
{
boost::shared_ptr<Connection> conn(creator());
if (vm.count("host")) host = vm["host"].as<std::string>();
if (vm.count("port")) port = vm["port"].as<std::string>();
if (vm.count("dbname")) dbname = vm["dbname"].as<std::string>();
if (vm.count("user")) user = vm["user"].as<std::string>();
if (vm.count("password")) password = vm["password"].as<std::string>();
ConnectionCreator<Connection> creator(host,port,dbname,user,password,connect_timeout);
try
{
boost::shared_ptr<Connection> conn(creator());
std::string query = vm["query"].as<std::string>();
std::string output_table_name = vm.count("table") ? vm["table"].as<std::string>() : mapnik::table_from_sql(query);
std::string output_file = vm["file"].as<std::string>();
std::cout << "output_table : " << output_table_name << "\n";
mapnik::pgsql2sqlite(conn,query,output_table_name,output_file);
}
catch (mapnik::datasource_exception & ex)
{
std::cerr << ex.what() << "\n";
}
std::string query = vm["query"].as<std::string>();
std::string output_table_name = vm.count("table") ? vm["table"].as<std::string>() : mapnik::table_from_sql(query);
std::string output_file = vm["file"].as<std::string>();
std::cout << "output_table : " << output_table_name << "\n";
mapnik::pgsql2sqlite(conn,query,output_table_name,output_file);
}
catch (mapnik::datasource_exception & ex)
{
std::cerr << ex.what() << "\n";
catch(std::exception& e) {
std::cerr << desc << "\n";
std::cout << usage << "\n";
std::cerr << e.what() << "\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;