libdballe 9.13
cmdline.h
Go to the documentation of this file.
1#ifndef DBALLE_CMDLINE_CMDLINE_H
2#define DBALLE_CMDLINE_CMDLINE_H
3
8
9#include <dballe/file.h>
10#include <dballe/fwd.h>
11#include <list>
12#include <memory>
13#include <popt.h>
14#include <string>
15#include <vector>
16#include <wreport/error.h>
17
18namespace dballe {
19namespace cmdline {
20
22{
23 std::vector<std::string> names;
24 std::string usage;
25 std::string desc;
26 std::string longdesc;
27 int op_verbose;
28
29 virtual ~Subcommand() {}
30
31 virtual void add_to_optable(std::vector<poptOption>& opts) const;
32
34 virtual void init();
35
36 virtual int main(poptContext) = 0;
37
46 poptContext make_popt_context(int argc, const char* argv[],
47 std::vector<poptOption>& opts) const;
48 void manpage_print_options(FILE* out);
49};
50
51#define ODT_END {NULL, NULL, NULL, NULL, NULL, NULL}
52
53struct Command
54{
55 std::string name;
56 std::string desc;
57 std::string longdesc;
58 std::string manpage_examples_section;
59 std::string manpage_files_section;
60 std::string manpage_seealso_section;
61
62 std::vector<Subcommand*> ops;
63
64 ~Command();
65
68 void add_subcommand(std::unique_ptr<Subcommand>&& action);
69
70 Subcommand* find_action(const std::string& name) const;
71
72 void usage(const std::string& selfpath, FILE* out) const;
73 void manpage(FILE* out) const;
74
76 int main(int argc, const char* argv[]);
77};
78
80struct error_cmdline : public std::exception
81{
82 std::string msg;
83
85 error_cmdline(const std::string& msg) : msg(msg) {}
86 ~error_cmdline() throw() {}
87
88 const char* what() const noexcept override { return msg.c_str(); }
89
91 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
92};
93
98
103[[noreturn]] void dba_cmdline_error(poptContext optCon, const char* fmt, ...)
104 __attribute__((format(printf, 2, 3)));
105
109Encoding string_to_encoding(const char* type);
110
116unsigned dba_cmdline_get_query(poptContext optCon, Query& query);
117
122
124std::list<std::string> get_filenames(poptContext optCon);
125
126} // namespace cmdline
127} // namespace dballe
128#endif
Query used to filter DB-All.e data.
Definition query.h:17
void dba_cmdline_print_dba_error()
Print informations about the last error to stderr.
unsigned dba_cmdline_get_query(poptContext optCon, Query &query)
Get a DB-ALLe query from commandline parameters in the form key=value.
std::list< std::string > get_filenames(poptContext optCon)
Read all the command line arguments and return them as a list.
void dba_cmdline_error(poptContext optCon, const char *fmt,...) __attribute__((format(printf
Print an error that happened when parsing commandline arguments, then add usage informations and exit...
void list_templates()
List available output templates.
void Encoding string_to_encoding(const char *type)
Return the File::Encoding that corresponds to the name in the string.
#define WREPORT_THROWF_ATTRS(a, b)
Definition cmdline.h:54
int main(int argc, const char *argv[])
Process commandline arguments and perform the action requested.
void add_subcommand(Subcommand *action)
Add an action to this tool, taking ownership of its memory management.
Definition cmdline.h:22
virtual void init()
Optional initialization before main is called.
poptContext make_popt_context(int argc, const char *argv[], std::vector< poptOption > &opts) const
Create a popt context for this subcommand.
Report an error with command line options.
Definition cmdline.h:81
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
error_cmdline(const std::string &msg)
Definition cmdline.h:85
std::string msg
error message returned by what()
Definition cmdline.h:82