30 #include "gnucash-commands.hpp" 31 #include "gnucash-core-app.hpp" 33 #include <glib/gi18n.h> 37 #include <boost/locale.hpp> 38 #include <boost/optional.hpp> 40 #include <boost/nowide/args.hpp> 43 #include <gnc-quotes.hpp> 45 namespace bl = boost::locale;
48 [[maybe_unused]]
static QofLogModule log_module = GNC_MOD_GUI;
56 void parse_command_line (
int argc,
char **argv);
57 int start (
int argc,
char **argv);
59 void configure_program_options (
void);
61 std::vector<std::string> m_quotes_cmd;
62 boost::optional <std::string> m_namespace;
63 bool m_verbose =
false;
65 boost::optional <std::string> m_report_cmd;
66 boost::optional <std::string> m_report_name;
67 boost::optional <std::string> m_export_type;
68 boost::optional <std::string> m_output_file;
73 Gnucash::GnucashCli::GnucashCli (
const char *app_name) :
Gnucash::CoreApp (app_name)
75 configure_program_options();
79 Gnucash::GnucashCli::parse_command_line (
int argc,
char **argv)
81 Gnucash::CoreApp::parse_command_line (argc, argv);
83 if (!m_log_to_filename || m_log_to_filename->empty())
84 m_log_to_filename =
"stderr";
87 gnc_prefs_set_namespace_regexp (m_namespace->c_str());
92 Gnucash::GnucashCli::configure_program_options (
void)
94 bpo::options_description quotes_options(_(
"Price Quotes Retrieval Options"));
95 quotes_options.add_options()
96 (
"quotes,Q", bpo::value<std::vector<std::string>> (&m_quotes_cmd)->multitoken(),
97 _(
"Execute price quote related commands. The following commands are supported.\n\n" 98 " info: \tShow Finance::Quote version and exposed quote sources.\n" 99 " get: \tFetch current quotes for all foreign currencies and stocks in the given GnuCash datafile.\n" 100 " dump: \tFetch current quotes for specified currencies or stocks from a specified namespace and print the results to the console.\n" 101 " \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"))
102 (
"namespace", bpo::value (&m_namespace),
103 _(
"Regular expression determining which namespace commodities will be retrieved for when using the get command"))
104 (
"verbose,V", bpo::bool_switch (&m_verbose),
105 _(
"When using the dump command list all of the parameters Finance::Quote returns for the symbol instead of the ones that Gnucash requires."));
107 m_opt_desc_display->add (quotes_options);
108 m_opt_desc_all.add (quotes_options);
110 bpo::options_description report_options(_(
"Report Generation Options"));
111 report_options.add_options()
112 (
"report,R", bpo::value (&m_report_cmd),
113 _(
"Execute report related commands. The following commands are supported.\n\n" 114 " list: \tLists available reports.\n" 115 " show: \tDescribe the options modified in the named report. A datafile \ 116 may be specified to describe some saved options.\n" 117 " run: \tRun the named report in the given GnuCash datafile.\n"))
118 (
"name", bpo::value (&m_report_name),
119 _(
"Name of the report to run\n"))
120 (
"export-type", bpo::value (&m_export_type),
121 _(
"Specify export type\n"))
122 (
"output-file", bpo::value (&m_output_file),
123 _(
"Output file for report\n"));
124 m_opt_desc_display->add (report_options);
125 m_opt_desc_all.add (report_options);
130 Gnucash::GnucashCli::start ([[maybe_unused]]
int argc, [[maybe_unused]]
char **argv)
132 Gnucash::CoreApp::start();
134 if (!m_quotes_cmd.empty())
136 if (m_quotes_cmd.front() ==
"info")
138 return Gnucash::check_finance_quote ();
140 else if (m_quotes_cmd.front() ==
"get")
142 if (!m_file_to_load && m_quotes_cmd.size() == 2)
143 m_file_to_load = m_quotes_cmd[1];
145 if (!m_file_to_load || m_file_to_load->empty())
147 std::cerr << bl::translate(
"Missing data file parameter") <<
"\n\n" 148 << *m_opt_desc_display.get() << std::endl;
152 return Gnucash::add_quotes (m_file_to_load);
154 else if (m_quotes_cmd.front() ==
"dump")
156 if (m_quotes_cmd.size() < 3 ||
157 (m_quotes_cmd[1] ==
"currency" &&
158 m_quotes_cmd.size() < 4))
160 std::cerr << bl::translate(
"Not enough information for quotes dump") << std::endl;
163 auto source = m_quotes_cmd[1];
164 m_quotes_cmd.erase(m_quotes_cmd.begin(), m_quotes_cmd.begin() + 2);
165 return Gnucash::report_quotes(source.c_str(), m_quotes_cmd,
170 std::cerr << bl::format (bl::translate(
"Unknown quotes command '{1}'")) % m_quotes_cmd.front() <<
"\n\n" 171 << *m_opt_desc_display.get() << std::endl;
178 if (*m_report_cmd ==
"run")
180 if (!m_file_to_load || m_file_to_load->empty())
182 std::cerr << _(
"Missing data file parameter") <<
"\n\n" 183 << *m_opt_desc_display.get() << std::endl;
187 return Gnucash::run_report(m_file_to_load, m_report_name,
188 m_export_type, m_output_file);
196 else if (*m_report_cmd ==
"list")
197 return Gnucash::report_list ();
203 else if (*m_report_cmd ==
"show")
204 if (!m_report_name || m_report_name->empty())
206 std::cerr << _(
"Missing --name parameter") <<
"\n\n" 207 << *m_opt_desc_display.get() << std::endl;
211 return Gnucash::report_show (m_file_to_load, m_report_name);
214 std::cerr << bl::format (std::string{_(
"Unknown report command '{1}'")}) % *m_report_cmd <<
"\n\n" 215 << *m_opt_desc_display.get();
220 std::cerr << _(
"Missing command or option") <<
"\n\n" 221 << *m_opt_desc_display.get() << std::endl;
227 main(
int argc,
char **argv)
229 const char *app_name = PROJECT_NAME
"-cli";
232 boost::nowide::args a(argc, argv);
234 application.parse_command_line (argc, argv);
235 application.start (argc, argv);
All type declarations for the whole Gnucash engine.
Generic api to store and retrieve preferences.