diff --git a/gnucash/gnucash-cli.cpp b/gnucash/gnucash-cli.cpp index ec2a34a3da..20f78ff8a3 100644 --- a/gnucash/gnucash-cli.cpp +++ b/gnucash/gnucash-cli.cpp @@ -103,7 +103,8 @@ Gnucash::GnucashCli::configure_program_options (void) ("report,R", bpo::value (&m_report_cmd), _("Execute report related commands. The following commands are supported.\n\n" " list: \tLists available reports.\n" - " show: \tDescribe the options modified in the named report.\n" + " show: \tDescribe the options modified in the named report. A datafile \ +may be specified to describe some saved options.\n" " run: \tRun the named report in the given GnuCash datafile.\n")) ("name", bpo::value (&m_report_name), _("Name of the report to run\n")) @@ -154,13 +155,19 @@ Gnucash::GnucashCli::start ([[maybe_unused]] int argc, [[maybe_unused]] char **a return Gnucash::run_report(m_file_to_load, m_report_name, m_export_type, m_output_file); } - // the following two commands list and show do *not* test&pass - // the m_file_to_load argument because the reports are global - // rather than per-file objects. In the future, saved reports - // may be saved into the datafile, therefore one will need to - // be specified for loading. + + // The command "list" does *not* test&pass the m_file_to_load + // argument because the reports are global rather than + // per-file objects. In the future, saved reports may be saved + // into the datafile, therefore one will need to be specified + // for loading. else if (*m_report_cmd == "list") return Gnucash::report_list (); + + // The command "show" does test&pass the m_file_to_load + // argument, and will attempt to load datafile prior to + // describing report. If loading fails, it will continue + // showing report options. else if (*m_report_cmd == "show") if (!m_report_name || m_report_name->empty()) { @@ -169,7 +176,7 @@ Gnucash::GnucashCli::start ([[maybe_unused]] int argc, [[maybe_unused]] char **a return 1; } else - return Gnucash::report_show (m_report_name); + return Gnucash::report_show (m_file_to_load, m_report_name); else { std::cerr << bl::format (bl::translate("Unknown report command '{1}'")) % *m_report_cmd << "\n\n" diff --git a/gnucash/gnucash-commands.cpp b/gnucash/gnucash-commands.cpp index cda95f3de2..d39b1c3fd2 100644 --- a/gnucash/gnucash-commands.cpp +++ b/gnucash/gnucash-commands.cpp @@ -203,6 +203,7 @@ scm_run_report (void *data, struct show_report_args { + const std::string file_to_load; const std::string show_report; }; @@ -218,6 +219,20 @@ scm_report_show (void *data, scm_c_use_module ("gnucash reports"); gnc_report_init (); + if (!args->file_to_load.empty()) + { + auto datafile = args->file_to_load.c_str(); + PINFO ("Loading datafile %s...\n", datafile); + + auto session = gnc_get_current_session (); + if (session) + { + qof_session_begin (session, datafile, SESSION_READ_ONLY); + if (qof_session_get_error (session) == ERR_BACKEND_NO_ERR) + qof_session_load (session, report_session_percentage); + } + } + scm_call_2 (scm_c_eval_string ("gnc:cmdline-report-show"), scm_from_utf8_string (args->show_report.c_str ()), scm_current_output_port ()); @@ -267,9 +282,11 @@ Gnucash::run_report (const bo_str& file_to_load, } int -Gnucash::report_show (const bo_str& show_report) +Gnucash::report_show (const bo_str& file_to_load, + const bo_str& show_report) { - auto args = show_report_args { show_report ? *show_report : std::string() }; + auto args = show_report_args { file_to_load ? *file_to_load : std::string(), + show_report ? *show_report : std::string() }; if (show_report && !show_report->empty()) scm_boot_guile (0, nullptr, scm_report_show, &args); diff --git a/gnucash/gnucash-commands.hpp b/gnucash/gnucash-commands.hpp index d55f3d1696..01511d5847 100644 --- a/gnucash/gnucash-commands.hpp +++ b/gnucash/gnucash-commands.hpp @@ -38,6 +38,7 @@ namespace Gnucash { const bo_str& export_type, const bo_str& output_file); int report_list (void); - int report_show (const bo_str& run_report); + int report_show (const bo_str& file_to_load, + const bo_str& run_report); } #endif