[cli-reports] implement --report list

Lists available reports.
This commit is contained in:
Christopher Lam 2020-06-06 11:51:12 +08:00
parent e4e36e6843
commit 4d921c67c3
4 changed files with 39 additions and 13 deletions

View File

@ -100,7 +100,8 @@ Gnucash::GnucashCli::configure_program_options (void)
bpo::options_description report_options(_("Report Generation Options"));
report_options.add_options()
("report,R", bpo::value (&m_report_cmd),
_("Execute report related commands. Currently only one command is supported.\n\n"
_("Execute report related commands. The following commands are supported.\n\n"
" list: \tLists available reports.\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"))
@ -150,6 +151,8 @@ 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);
}
else if (*m_report_cmd == "list")
return Gnucash::report_list ();
else
{
std::cerr << bl::format (bl::translate("Unknown report command '{1}'")) % *m_report_cmd << "\n\n"

View File

@ -197,6 +197,20 @@ scm_run_report (void *data,
return;
}
static void
scm_report_list ([[maybe_unused]] void *data,
[[maybe_unused]] int argc, [[maybe_unused]] char **argv)
{
scm_c_eval_string("(debug-set! stack 200000)");
scm_c_use_module ("gnucash app-utils");
scm_c_use_module ("gnucash reports");
gnc_report_init ();
scm_call_0 (scm_c_eval_string ("gnc:cmdline-report-list"));
gnc_shutdown (0);
return;
}
int
Gnucash::add_quotes (const bo_str& uri)
{
@ -221,3 +235,10 @@ Gnucash::run_report (const bo_str& file_to_load,
return 0;
}
int
Gnucash::report_list (void)
{
scm_boot_guile (0, nullptr, scm_report_list, NULL);
return 0;
}

View File

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

View File

@ -799,6 +799,18 @@ not found.")))
(assoc-ref parent-export-types export-type) output-file)
(display "done!\n" (current-error-port))))))
(define-public (gnc:cmdline-report-list)
(for-each
(lambda (template)
(stderr-log "* ~a ~a\n"
(if (gnc:report-template-parent-type template) "C" " ")
(gnc:report-template-name template)))
(sort (hash-fold
(lambda (k v p) (if (gnc:report-template-in-menu? v) (cons v p) p))
'() *gnc:_report-templates_*)
(lambda (a b)
(string<? (gnc:report-template-name a) (gnc:report-template-name b))))))
(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
@ -817,17 +829,7 @@ not found.")))
(cond
((null? templates)
(stderr-log "Cannot find ~s. Valid reports:\n" report)
(for-each
(lambda (pair)
(when (gnc:report-template-in-menu? (cdr pair))
(stderr-log "* ~a ~a\n"
(if (gnc:report-template-parent-type (cdr pair)) "C" " ")
(gnc:report-template-name (cdr pair)))))
(sort (hash-map->list cons *gnc:_report-templates_*)
(lambda (a b)
(string<?
(gnc:report-template-name (cdr a))
(gnc:report-template-name (cdr b))))))
(gnc:cmdline-report-list)
(stderr-log "\n"))
((pair? (cdr templates))