[cli-reports] implement --report show

This commit is contained in:
Christopher Lam 2020-06-06 12:16:52 +08:00
parent 4d921c67c3
commit 25e55cee82
4 changed files with 73 additions and 14 deletions

View File

@ -102,6 +102,7 @@ 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"
" run: \tRun the named report in the given GnuCash datafile.\n"))
("name", bpo::value (&m_report_name),
_("Name of the report to run\n"))
@ -151,8 +152,22 @@ 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.
else if (*m_report_cmd == "list")
return Gnucash::report_list ();
else if (*m_report_cmd == "show")
if (!m_report_name || m_report_name->empty())
{
std::cerr << bl::translate("Missing --name parameter") << "\n\n"
<< *m_opt_desc.get();
return 1;
}
else
return Gnucash::report_show (m_report_name);
else
{
std::cerr << bl::format (bl::translate("Unknown report command '{1}'")) % *m_report_cmd << "\n\n"

View File

@ -197,6 +197,30 @@ scm_run_report (void *data,
return;
}
struct show_report_args {
const std::string& show_report;
};
static void
scm_report_show (void *data,
[[maybe_unused]] int argc, [[maybe_unused]] char **argv)
{
auto args = static_cast<show_report_args*>(data);
scm_c_eval_string("(debug-set! stack 200000)");
scm_c_use_module ("gnucash utilities");
scm_c_use_module ("gnucash app-utils");
scm_c_use_module ("gnucash reports");
gnc_report_init ();
scm_call_1 (scm_c_eval_string ("gnc:cmdline-report-show"),
scm_from_utf8_string (args->show_report.c_str()));
gnc_shutdown (0);
return;
}
static void
scm_report_list ([[maybe_unused]] void *data,
[[maybe_unused]] int argc, [[maybe_unused]] char **argv)
@ -236,6 +260,16 @@ Gnucash::run_report (const bo_str& file_to_load,
return 0;
}
int
Gnucash::report_show (const bo_str& show_report)
{
auto args = show_report_args { show_report ? *show_report : std::string() };
if (show_report && !show_report->empty())
scm_boot_guile (0, nullptr, scm_report_show, &args);
return 0;
}
int
Gnucash::report_list (void)
{

View File

@ -38,5 +38,6 @@ namespace Gnucash {
const bo_str& export_type,
const bo_str& output_file);
int report_list (void);
int report_show (const bo_str& run_report);
}
#endif

View File

@ -799,6 +799,12 @@ not found.")))
(assoc-ref parent-export-types export-type) output-file)
(display "done!\n" (current-error-port))))))
(define (reportname->templates report)
(or (and=> (gnc:find-report-template report) list)
(hash-fold
(lambda (k v p) (if (equal? (gnc:report-template-name v) report) (cons v p) p))
'() *gnc:_report-templates_*)))
(define-public (gnc:cmdline-report-list)
(for-each
(lambda (template)
@ -811,13 +817,23 @@ not found.")))
(lambda (a b)
(string<? (gnc:report-template-name a) (gnc:report-template-name b))))))
(define-public (gnc:cmdline-report-show report)
(let ((templates (reportname->templates report)))
(cond
((null? templates)
(stderr-log "Cannot find ~s. Valid reports:\n" report)
(gnc:cmdline-report-list))
(else
(for-each
(lambda (template)
(let* ((options-gen (gnc:report-template-options-generator template)))
(stderr-log "\n* guid: ~a\n~a"
(gnc:report-template-report-guid template)
(gnc:html-render-options-changed (options-gen) #t))))
templates)))))
(define-public (gnc:cmdline-run-report report export-type output-file dry-run?)
(let ((templates (or (and=> (gnc:find-report-template report) list)
(hash-fold
(lambda (report-guid template prev)
(if (equal? (gnc:report-template-name template) report)
(cons template prev) prev))
'() *gnc:_report-templates_*))))
(let ((templates (reportname->templates report)))
(define (run-report output-port)
(display
@ -834,14 +850,7 @@ not found.")))
((pair? (cdr templates))
(stderr-log "~s matches multiple reports. Select guid instead:\n" report)
(for-each
(lambda (template)
(let* ((options-gen (gnc:report-template-options-generator template))
(options (options-gen)))
(stderr-log "\n* guid: ~a\n~a"
(gnc:report-template-report-guid template)
(gnc:html-render-options-changed options #t))))
templates)
(gnc:cmdline-report-show report)
(stderr-log "\n"))
(export-type (template-export report (car templates)