Add support for setting default options in config file

Added --save-config and --reset-config options
This commit is contained in:
Sude 2014-02-03 19:28:48 +02:00
parent 44220211d5
commit 3b4def2ebe
2 changed files with 107 additions and 16 deletions

View File

@ -33,6 +33,8 @@ class Config
bool bVerifyPeer; bool bVerifyPeer;
bool bCheckStatus; bool bCheckStatus;
bool bDuplicateHandler; bool bDuplicateHandler;
bool bSaveConfig;
bool bResetConfig;
std::string sGameRegex; std::string sGameRegex;
std::string sDirectory; std::string sDirectory;
std::string sXMLFile; std::string sXMLFile;

121
main.cpp
View File

@ -94,8 +94,11 @@ int main(int argc, char *argv[])
std::string check_orphans_text = "Check for orphaned files (files found on local filesystem that are not found on GOG servers). Sets regular expression filter (Perl syntax) for files to check. If no argument is given then the regex defaults to '" + orphans_regex_default + "'"; std::string check_orphans_text = "Check for orphaned files (files found on local filesystem that are not found on GOG servers). Sets regular expression filter (Perl syntax) for files to check. If no argument is given then the regex defaults to '" + orphans_regex_default + "'";
bpo::variables_map vm; bpo::variables_map vm;
bpo::options_description desc("Options"); bpo::options_description options_cli_all("Options");
bpo::options_description config_file_options("Configuration"); bpo::options_description options_cli_no_cfg;
bpo::options_description options_cli_cfg;
bpo::options_description options_cfg_only;
bpo::options_description options_cfg_all("Configuration");
try try
{ {
bool bInsecure = false; bool bInsecure = false;
@ -108,7 +111,8 @@ int main(int argc, char *argv[])
bool bNoPatches = false; bool bNoPatches = false;
bool bNoLanguagePacks = false; bool bNoLanguagePacks = false;
bool bNoRemoteXML = false; bool bNoRemoteXML = false;
desc.add_options() // Commandline options (no config file)
options_cli_no_cfg.add_options()
("help,h", "Print help message") ("help,h", "Print help message")
("login", bpo::value<bool>(&config.bLogin)->zero_tokens()->default_value(false), "Login") ("login", bpo::value<bool>(&config.bLogin)->zero_tokens()->default_value(false), "Login")
("list", bpo::value<bool>(&config.bList)->zero_tokens()->default_value(false), "List games") ("list", bpo::value<bool>(&config.bList)->zero_tokens()->default_value(false), "List games")
@ -116,12 +120,19 @@ int main(int argc, char *argv[])
("download", bpo::value<bool>(&config.bDownload)->zero_tokens()->default_value(false), "Download") ("download", bpo::value<bool>(&config.bDownload)->zero_tokens()->default_value(false), "Download")
("repair", bpo::value<bool>(&config.bRepair)->zero_tokens()->default_value(false), "Repair downloaded files\nUse --repair --download to redownload files when filesizes don't match (possibly different version). Redownload will delete the old file") ("repair", bpo::value<bool>(&config.bRepair)->zero_tokens()->default_value(false), "Repair downloaded files\nUse --repair --download to redownload files when filesizes don't match (possibly different version). Redownload will delete the old file")
("game", bpo::value<std::string>(&config.sGameRegex)->default_value(""), "Set regular expression filter\nfor download/list/repair (Perl syntax)\nAliases: \"all\", \"free\"") ("game", bpo::value<std::string>(&config.sGameRegex)->default_value(""), "Set regular expression filter\nfor download/list/repair (Perl syntax)\nAliases: \"all\", \"free\"")
("create-xml", bpo::value<std::string>(&config.sXMLFile)->default_value(""), "Create GOG XML for file\n\"automatic\" to enable automatic XML creation")
("update-check", bpo::value<bool>(&config.bUpdateCheck)->zero_tokens()->default_value(false), "Check for update notifications")
("check-orphans", bpo::value<std::string>(&config.sOrphanRegex)->implicit_value(""), check_orphans_text.c_str())
("status", bpo::value<bool>(&config.bCheckStatus)->zero_tokens()->default_value(false), "Show status of files\n\nOutput format:\nstatuscode gamename filename filesize filehash\n\nStatus codes:\nOK - File is OK\nND - File is not downloaded\nMD5 - MD5 mismatch, different version")
("save-config", bpo::value<bool>(&config.bSaveConfig)->zero_tokens()->default_value(false), "Create config file with current settings")
("reset-config", bpo::value<bool>(&config.bResetConfig)->zero_tokens()->default_value(false), "Reset config settings to default")
;
// Commandline options (config file)
options_cli_cfg.add_options()
("directory", bpo::value<std::string>(&config.sDirectory)->default_value(""), "Set download directory") ("directory", bpo::value<std::string>(&config.sDirectory)->default_value(""), "Set download directory")
("limit-rate", bpo::value<curl_off_t>(&config.iDownloadRate)->default_value(0), "Limit download rate to value in kB\n0 = unlimited") ("limit-rate", bpo::value<curl_off_t>(&config.iDownloadRate)->default_value(0), "Limit download rate to value in kB\n0 = unlimited")
("create-xml", bpo::value<std::string>(&config.sXMLFile)->default_value(""), "Create GOG XML for file\n\"automatic\" to enable automatic XML creation")
("xml-directory", bpo::value<std::string>(&config.sXMLDirectory), "Set directory for GOG XML files") ("xml-directory", bpo::value<std::string>(&config.sXMLDirectory), "Set directory for GOG XML files")
("chunk-size", bpo::value<size_t>(&config.iChunkSize)->default_value(10), "Chunk size (in MB) when creating XML") ("chunk-size", bpo::value<size_t>(&config.iChunkSize)->default_value(10), "Chunk size (in MB) when creating XML")
("update-check", bpo::value<bool>(&config.bUpdateCheck)->zero_tokens()->default_value(false), "Check for update notifications")
("platform", bpo::value<unsigned int>(&config.iInstallerType)->default_value(GlobalConstants::PLATFORM_WINDOWS), platform_text.c_str()) ("platform", bpo::value<unsigned int>(&config.iInstallerType)->default_value(GlobalConstants::PLATFORM_WINDOWS), platform_text.c_str())
("language", bpo::value<unsigned int>(&config.iInstallerLanguage)->default_value(GlobalConstants::LANGUAGE_EN), language_text.c_str()) ("language", bpo::value<unsigned int>(&config.iInstallerLanguage)->default_value(GlobalConstants::LANGUAGE_EN), language_text.c_str())
("no-installers", bpo::value<bool>(&bNoInstallers)->zero_tokens()->default_value(false), "Don't download/list/repair installers") ("no-installers", bpo::value<bool>(&bNoInstallers)->zero_tokens()->default_value(false), "Don't download/list/repair installers")
@ -136,19 +147,19 @@ int main(int argc, char *argv[])
("verbose", bpo::value<bool>(&config.bVerbose)->zero_tokens()->default_value(false), "Print lots of information") ("verbose", bpo::value<bool>(&config.bVerbose)->zero_tokens()->default_value(false), "Print lots of information")
("insecure", bpo::value<bool>(&bInsecure)->zero_tokens()->default_value(false), "Don't verify authenticity of SSL certificates") ("insecure", bpo::value<bool>(&bInsecure)->zero_tokens()->default_value(false), "Don't verify authenticity of SSL certificates")
("timeout", bpo::value<long int>(&config.iTimeout)->default_value(10), "Set timeout for connection\nMaximum time in seconds that connection phase is allowed to take") ("timeout", bpo::value<long int>(&config.iTimeout)->default_value(10), "Set timeout for connection\nMaximum time in seconds that connection phase is allowed to take")
("check-orphans", bpo::value<std::string>(&config.sOrphanRegex)->implicit_value(""), check_orphans_text.c_str())
("status", bpo::value<bool>(&config.bCheckStatus)->zero_tokens()->default_value(false), "Show status of files\n\nOutput format:\nstatuscode gamename filename filesize filehash\n\nStatus codes:\nOK - File is OK\nND - File is not downloaded\nMD5 - MD5 mismatch, different version")
; ;
// Options read from config file
bpo::store(bpo::parse_command_line(argc, argv, desc), vm); options_cfg_only.add_options()
bpo::notify(vm);
// Read token and secret from config file
config_file_options.add_options()
("token", bpo::value<std::string>(&config.sToken)->default_value(""), "oauth token") ("token", bpo::value<std::string>(&config.sToken)->default_value(""), "oauth token")
("secret", bpo::value<std::string>(&config.sSecret)->default_value(""), "oauth secret") ("secret", bpo::value<std::string>(&config.sSecret)->default_value(""), "oauth secret")
; ;
options_cli_all.add(options_cli_no_cfg).add(options_cli_cfg);
options_cfg_all.add(options_cfg_only).add(options_cli_cfg);
bpo::store(bpo::parse_command_line(argc, argv, options_cli_all), vm);
bpo::notify(vm);
if (boost::filesystem::exists(config.sConfigFilePath)) if (boost::filesystem::exists(config.sConfigFilePath))
{ {
std::ifstream ifs(config.sConfigFilePath.c_str()); std::ifstream ifs(config.sConfigFilePath.c_str());
@ -159,7 +170,7 @@ int main(int argc, char *argv[])
} }
else else
{ {
bpo::store(bpo::parse_config_file(ifs, config_file_options), vm); bpo::store(bpo::parse_config_file(ifs, options_cfg_all), vm);
bpo::notify(vm); bpo::notify(vm);
ifs.close(); ifs.close();
} }
@ -168,7 +179,7 @@ int main(int argc, char *argv[])
if (vm.count("help")) if (vm.count("help"))
{ {
std::cout << config.sVersionString << std::endl std::cout << config.sVersionString << std::endl
<< desc << std::endl; << options_cli_all << std::endl;
return 0; return 0;
} }
@ -240,6 +251,84 @@ int main(int argc, char *argv[])
if (config.bLogin) if (config.bLogin)
return result; return result;
else if (config.bSaveConfig)
{
std::ofstream ofs(config.sConfigFilePath.c_str());
if (ofs)
{
std::cout << "Saving config: " << config.sConfigFilePath << std::endl;
for (bpo::variables_map::iterator it = vm.begin(); it != vm.end(); ++it)
{
std::string option = it->first;
std::string option_value_string;
const bpo::variable_value& option_value = it->second;
try
{
if (options_cfg_all.find(option, false).long_name() == option)
{
if (!option_value.empty())
{
const std::type_info& type = option_value.value().type() ;
if ( type == typeid(std::string) )
option_value_string = option_value.as<std::string>();
else if ( type == typeid(int) )
option_value_string = std::to_string(option_value.as<int>());
else if ( type == typeid(size_t) )
option_value_string = std::to_string(option_value.as<size_t>());
else if ( type == typeid(unsigned int) )
option_value_string = std::to_string(option_value.as<unsigned int>());
else if ( type == typeid(long int) )
option_value_string = std::to_string(option_value.as<long int>());
else if ( type == typeid(bool) )
{
if (option_value.as<bool>() == true)
option_value_string = "true";
else
option_value_string = "false";
}
}
}
}
catch (...)
{
continue;
}
if (!option_value_string.empty())
{
std::cout << option << " = " << option_value_string << std::endl;
ofs << option << " = " << option_value_string << std::endl;
}
}
ofs.close();
return 0;
}
else
{
std::cout << "Failed to create config: " << config.sConfigFilePath << std::endl;
return 1;
}
}
else if (config.bResetConfig)
{
std::ofstream ofs(config.sConfigFilePath.c_str());
if (ofs)
{
if (!config.sToken.empty() && !config.sSecret.empty())
{
ofs << "token = " << config.sToken << std::endl;
ofs << "secret = " << config.sSecret << std::endl;
}
ofs.close();
return 0;
}
else
{
std::cout << "Failed to create config: " << config.sConfigFilePath << std::endl;
return 1;
}
}
else if (config.bUpdateCheck) // Update check has priority over download and list else if (config.bUpdateCheck) // Update check has priority over download and list
downloader.updateCheck(); downloader.updateCheck();
else if (config.bRepair) // Repair file else if (config.bRepair) // Repair file
@ -255,7 +344,7 @@ int main(int argc, char *argv[])
else else
{ // Show help message { // Show help message
std::cout << config.sVersionString << std::endl std::cout << config.sVersionString << std::endl
<< desc << std::endl; << options_cli_all << std::endl;
} }
return 0; return 0;