From 7eaa0eb292f080acbc86f7b5c2a929f3c99fcfc3 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sat, 1 Oct 2022 16:07:23 -0700 Subject: [PATCH] [price-quotes] Add dump command to gnucash-cli. --- gnucash/gnucash-cli.cpp | 36 ++++++++++++++++++++++++++++-------- gnucash/gnucash-commands.cpp | 18 ++++++++++++++++++ gnucash/gnucash-commands.hpp | 5 +++++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/gnucash/gnucash-cli.cpp b/gnucash/gnucash-cli.cpp index 90e6a1b8cf..95a8aecf62 100644 --- a/gnucash/gnucash-cli.cpp +++ b/gnucash/gnucash-cli.cpp @@ -60,8 +60,9 @@ namespace Gnucash { private: void configure_program_options (void); - boost::optional m_quotes_cmd; + std::vector m_quotes_cmd; boost::optional m_namespace; + bool m_verbose = false; boost::optional m_report_cmd; boost::optional m_report_name; @@ -94,12 +95,17 @@ Gnucash::GnucashCli::configure_program_options (void) { bpo::options_description quotes_options(_("Price Quotes Retrieval Options")); quotes_options.add_options() - ("quotes,Q", bpo::value (&m_quotes_cmd), + ("quotes,Q", bpo::value> (&m_quotes_cmd)->multitoken(), _("Execute price quote related commands. The following commands are supported.\n\n" " info: \tShow Finance::Quote version and exposed quote sources.\n" - " get: \tFetch current quotes for all foreign currencies and stocks in the given GnuCash datafile.\n")) + " get: \tFetch current quotes for all foreign currencies and stocks in the given GnuCash datafile.\n" + " dump: \tFetch current quotes for specified currencies or stocks from a specified namespace and print the results to the console.\n" + " \tThis must be followed with a source and one or more symbols, unless the source is \"currency\" in which case it must be followed with two or more symbols, the first of which is the currency in which exchange rates for the rest will be quoted.\n")) ("namespace", bpo::value (&m_namespace), - _("Regular expression determining which namespace commodities will be retrieved for")); + _("Regular expression determining which namespace commodities will be retrieved for when using the get command")) + ("verbose,V", bpo::bool_switch (&m_verbose), + _("When using the dump command list all of the parameters Finance::Quote returns for the symbol instead of the ones that Gnucash requires.")); + m_opt_desc_display->add (quotes_options); m_opt_desc_all.add (quotes_options); @@ -127,13 +133,13 @@ Gnucash::GnucashCli::start ([[maybe_unused]] int argc, [[maybe_unused]] char **a { Gnucash::CoreApp::start(); - if (m_quotes_cmd) + if (!m_quotes_cmd.empty()) { - if (*m_quotes_cmd == "info") + if (m_quotes_cmd.front() == "info") { return Gnucash::quotes_info (); } - else if (*m_quotes_cmd == "get") + else if (m_quotes_cmd.front() == "get") { if (!m_file_to_load || m_file_to_load->empty()) @@ -145,9 +151,23 @@ Gnucash::GnucashCli::start ([[maybe_unused]] int argc, [[maybe_unused]] char **a else return Gnucash::add_quotes (m_file_to_load); } + else if (m_quotes_cmd.front() == "dump") + { + if (m_quotes_cmd.size() < 3 || + (m_quotes_cmd[1] == "currency" && + m_quotes_cmd.size() < 4)) + { + std::cerr << bl::translate("Not enough information for quotes dump") << std::endl; + return 1; + } + auto source = m_quotes_cmd[1]; + m_quotes_cmd.erase(m_quotes_cmd.begin(), m_quotes_cmd.begin() + 2); + return Gnucash::report_quotes(source.c_str(), m_quotes_cmd, + m_verbose); + } else { - std::cerr << bl::format (bl::translate("Unknown quotes command '{1}'")) % *m_quotes_cmd << "\n\n" + std::cerr << bl::format (bl::translate("Unknown quotes command '{1}'")) % m_quotes_cmd.front() << "\n\n" << *m_opt_desc_display.get() << std::endl; return 1; } diff --git a/gnucash/gnucash-commands.cpp b/gnucash/gnucash-commands.cpp index ab0892ec0d..a655f472b7 100644 --- a/gnucash/gnucash-commands.cpp +++ b/gnucash/gnucash-commands.cpp @@ -362,6 +362,24 @@ Gnucash::add_quotes (const bo_str& uri) return 0; } +int +Gnucash::report_quotes (const char* source, const StrVec& commodities, bool verbose) +{ + try + { + GncQuotes quotes; + quotes.report(source, commodities, verbose); + if (quotes.had_failures()) + std::cerr << quotes.report_failures() << std::endl; + } + catch (const GncQuoteException& err) + { + std::cerr << bl::translate("Price retrieval failed: ") << err.what() << std::endl; + return -1; + } + return 0; +} + int Gnucash::run_report (const bo_str& file_to_load, const bo_str& run_report, diff --git a/gnucash/gnucash-commands.hpp b/gnucash/gnucash-commands.hpp index d5958ae5f7..09133cfc5b 100644 --- a/gnucash/gnucash-commands.hpp +++ b/gnucash/gnucash-commands.hpp @@ -26,14 +26,19 @@ #define GNUCASH_COMMANDS_HPP #include +#include #include using bo_str = boost::optional ; +using StrVec = std::vector; namespace Gnucash { int quotes_info (void); int add_quotes (const bo_str& uri); + int report_quotes (const char* source, + const StrVec& commodities, + bool verbose); int run_report (const bo_str& file_to_load, const bo_str& run_report, const bo_str& export_type,