Use common input file parameter for gnucash-cli --add-price-quotes

As gnucash and most gnucash-cli commands will work on an input file
it makes sense to use the common positional input-file parameter everywhere.

It's still optional for a normal gnucash run though. It will fall back to
to last-used file as before.

A --add-price-quotes run on the other hand will bail out with an error
message. As this command is typically run unattended in a cron script
it's safer to explicitly request a file to work on.
This commit is contained in:
Geert Janssens
2020-05-29 18:26:52 +02:00
parent 4af7ea0f0a
commit 1ea284d868
4 changed files with 40 additions and 25 deletions

View File

@@ -55,7 +55,7 @@ namespace Gnucash {
private: private:
void configure_program_options (void); void configure_program_options (void);
std::string m_quotes_file; bool m_add_quotes;
}; };
} }
@@ -70,12 +70,11 @@ Gnucash::GnucashCli::parse_command_line (int argc, char **argv)
{ {
Gnucash::CoreApp::parse_command_line (argc, argv); Gnucash::CoreApp::parse_command_line (argc, argv);
m_add_quotes = m_opt_map["add-price-quotes"].as<bool>();
if (m_opt_map.count ("namespace")) if (m_opt_map.count ("namespace"))
gnc_prefs_set_namespace_regexp(m_opt_map["namespace"]. gnc_prefs_set_namespace_regexp(m_opt_map["namespace"].
as<std::string>().c_str()); as<std::string>().c_str());
if (m_opt_map.count ("add-price-quotes"))
m_quotes_file = m_opt_map["add-price-quotes"].as<std::string>();
} }
// Define command line options specific to gnucash-cli. // Define command line options specific to gnucash-cli.
@@ -84,7 +83,7 @@ Gnucash::GnucashCli::configure_program_options (void)
{ {
bpo::options_description quotes_options(_("Price Retrieval Options")); bpo::options_description quotes_options(_("Price Retrieval Options"));
quotes_options.add_options() quotes_options.add_options()
("add-price-quotes", bpo::value<std::string>(), ("add-price-quotes", bpo::bool_switch(),
N_("Add price quotes to given GnuCash datafile.\n")) N_("Add price quotes to given GnuCash datafile.\n"))
("namespace", bpo::value<std::string>(), ("namespace", bpo::value<std::string>(),
N_("Regular expression determining which namespace commodities will be retrieved")); N_("Regular expression determining which namespace commodities will be retrieved"));
@@ -97,10 +96,20 @@ Gnucash::GnucashCli::start ([[maybe_unused]] int argc, [[maybe_unused]] char **a
{ {
Gnucash::CoreApp::start(); Gnucash::CoreApp::start();
if (m_quotes_file.empty()) if (m_add_quotes)
return 1; {
if (m_file_to_load.empty())
{
std::cerr << bl::translate("Missing data file parameter") << "\n\n"
<< *m_opt_desc.get();
return 1;
}
else
return Gnucash::add_quotes (m_file_to_load);
}
return Gnucash::add_quotes (m_quotes_file);
return 1;
} }
int int

View File

@@ -622,6 +622,9 @@ Gnucash::CoreApp::parse_command_line (int argc, char **argv)
m_log_to_filename = m_opt_map["logto"]. m_log_to_filename = m_opt_map["logto"].
as<std::string>().c_str(); as<std::string>().c_str();
if (m_opt_map.count ("input-file"))
m_file_to_load = m_opt_map["input-file"].as<std::string>();
if (args_remaining) if (args_remaining)
file_to_load = args_remaining[0]; file_to_load = args_remaining[0];
} }
@@ -645,7 +648,11 @@ Gnucash::CoreApp::add_common_program_options (void)
("logto", bpo::value<std::string>(), ("logto", bpo::value<std::string>(),
N_("File to log into; defaults to \"/tmp/gnucash.trace\"; can be \"stderr\" or \"stdout\".")) N_("File to log into; defaults to \"/tmp/gnucash.trace\"; can be \"stderr\" or \"stdout\"."))
("gsettings-prefix", bpo::value<std::string>(), ("gsettings-prefix", bpo::value<std::string>(),
N_("Set the prefix for gsettings schemas for gsettings queries. This can be useful to have a different settings tree while debugging.")); N_("Set the prefix for gsettings schemas for gsettings queries. This can be useful to have a different settings tree while debugging."))
("input-file", bpo::value<std::string>(),
N_("[datafile]"));
m_pos_opt_desc.add("input-file", -1);
m_opt_desc->add (common_options); m_opt_desc->add (common_options);
} }

View File

@@ -45,6 +45,7 @@ protected:
int gtk_show_help = 0; int gtk_show_help = 0;
std::string m_app_name; std::string m_app_name;
std::string tagline; std::string tagline;
std::string m_file_to_load;
std::unique_ptr<bpo::options_description> m_opt_desc; std::unique_ptr<bpo::options_description> m_opt_desc;
bpo::variables_map m_opt_map; bpo::variables_map m_opt_map;

View File

@@ -304,8 +304,7 @@ namespace Gnucash {
void configure_program_options (void); void configure_program_options (void);
std::string m_gtk_help_msg; std::string m_gtk_help_msg;
std::string m_file_to_load; bool m_add_quotes; // Deprecated will be removed in gnucash 5.0
std::string m_quotes_file; // Deprecated will be removed in gnucash 5.0
}; };
} }
@@ -327,15 +326,11 @@ Gnucash::Gnucash::parse_command_line (int argc, char **argv)
exit(0); exit(0);
} }
m_add_quotes = m_opt_map["add-price-quotes"].as<bool>();
if (m_opt_map.count ("namespace")) if (m_opt_map.count ("namespace"))
gnc_prefs_set_namespace_regexp (m_opt_map["namespace"]. gnc_prefs_set_namespace_regexp (m_opt_map["namespace"].
as<std::string>().c_str()); as<std::string>().c_str());
if (m_opt_map.count ("add-price-quotes"))
m_quotes_file = m_opt_map["add-price-quotes"].as<std::string>();
if (m_opt_map.count ("input-file"))
m_file_to_load = m_opt_map["input-file"].as<std::string>();
} }
// Define command line options specific to gnucash. // Define command line options specific to gnucash.
@@ -361,18 +356,14 @@ Gnucash::Gnucash::configure_program_options (void)
bpo::options_description depr_options(_("Deprecated Options")); bpo::options_description depr_options(_("Deprecated Options"));
depr_options.add_options() depr_options.add_options()
("add-price-quotes", bpo::value<std::string>(), ("add-price-quotes", bpo::bool_switch(),
N_("Add price quotes to given GnuCash datafile.\n" N_("Add price quotes to given GnuCash datafile.\n"
"Note this option has been deprecated and will be removed in GnuCash 5.0.\n" "Note this option has been deprecated and will be removed in GnuCash 5.0.\n"
"Please use \"gnucash-cli --add-price-quotes\" instead.")) "Please use \"gnucash-cli --add-price-quotes\" instead."))
("namespace", bpo::value<std::string>(), ("namespace", bpo::value<std::string>(),
N_("Regular expression determining which namespace commodities will be retrieved" N_("Regular expression determining which namespace commodities will be retrieved"
"Note this option has been deprecated and will be removed in GnuCash 5.0.\n" "Note this option has been deprecated and will be removed in GnuCash 5.0.\n"
"Please use \"gnucash-cli --add-price-quotes\" instead.")) "Please use \"gnucash-cli --add-price-quotes\" instead."));
("input-file", bpo::value<std::string>(),
N_("[datafile]"));
m_pos_opt_desc.add("input-file", -1);
m_opt_desc->add (app_options).add (depr_options); m_opt_desc->add (app_options).add (depr_options);
} }
@@ -384,11 +375,18 @@ Gnucash::Gnucash::start ([[maybe_unused]] int argc, [[maybe_unused]] char **argv
// Test for the deprecated add-price-quotes option and run it // Test for the deprecated add-price-quotes option and run it
// Will be removed in 5.0 // Will be removed in 5.0
if (!m_quotes_file.empty()) if (m_add_quotes)
{ {
std::cerr << bl::translate ("The '--add-price-quotes' option to gnucash has been deprecated and will be removed in GnuCash 5.0. " std::cerr << bl::translate ("The '--add-price-quotes' option to gnucash has been deprecated and will be removed in GnuCash 5.0. "
"Please use 'gnucash-cli --add-price-quotes' instead.") << "\n"; "Please use 'gnucash-cli --add-price-quotes' instead.") << "\n";
return add_quotes (m_quotes_file); if (m_file_to_load.empty())
{
std::cerr << bl::translate("Missing data file parameter") << "\n\n"
<< *m_opt_desc.get();
return 1;
}
else
return add_quotes (m_file_to_load);
} }
/* Now the module files are looked up, which might cause some library /* Now the module files are looked up, which might cause some library