Skip to content

Commit

Permalink
[price-quotes] Add dump command to gnucash-cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
jralls committed Oct 2, 2022
1 parent d97ea77 commit 7eaa0eb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
36 changes: 28 additions & 8 deletions gnucash/gnucash-cli.cpp
Expand Up @@ -60,8 +60,9 @@ namespace Gnucash {
private:
void configure_program_options (void);

boost::optional <std::string> m_quotes_cmd;
std::vector<std::string> m_quotes_cmd;
boost::optional <std::string> m_namespace;
bool m_verbose = false;

boost::optional <std::string> m_report_cmd;
boost::optional <std::string> m_report_name;
Expand Down Expand Up @@ -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<std::vector<std::string>> (&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);

Expand Down Expand Up @@ -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())
Expand All @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions gnucash/gnucash-commands.cpp
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions gnucash/gnucash-commands.hpp
Expand Up @@ -26,14 +26,19 @@
#define GNUCASH_COMMANDS_HPP

#include <string>
#include <vector>
#include <boost/optional.hpp>

using bo_str = boost::optional <std::string>;
using StrVec = std::vector<std::string>;

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,
Expand Down

0 comments on commit 7eaa0eb

Please sign in to comment.