libdballe  7.29
cmdline.h
Go to the documentation of this file.
1 #ifndef DBALLE_CMDLINE_CMDLINE_H
2 #define DBALLE_CMDLINE_CMDLINE_H
3 
9 #include <wreport/error.h>
10 #include <dballe/file.h>
11 #include <popt.h>
12 #include <memory>
13 #include <vector>
14 #include <list>
15 #include <string>
16 
17 namespace dballe {
18 struct Record;
19 struct Query;
20 
21 namespace core {
22 struct Query;
23 }
24 
25 namespace cmdline {
26 
27 struct Subcommand
28 {
29  std::vector<std::string> names;
30  std::string usage;
31  std::string desc;
32  std::string longdesc;
33  int op_verbose;
34 
35  virtual ~Subcommand() {}
36 
37  virtual void add_to_optable(std::vector<poptOption>& opts) const;
38 
40  virtual void init();
41 
42  virtual int main(poptContext) = 0;
43 
52  poptContext make_popt_context(int argc, const char* argv[], std::vector<poptOption>& opts) const;
53  void manpage_print_options(FILE* out);
54 };
55 
56 #define ODT_END { NULL, NULL, NULL, NULL, NULL, NULL }
57 
58 struct Command
59 {
60  std::string name;
61  std::string desc;
62  std::string longdesc;
63  std::string manpage_examples_section;
64  std::string manpage_files_section;
65  std::string manpage_seealso_section;
66 
67  std::vector<Subcommand*> ops;
68 
69  ~Command();
70 
72  void add_subcommand(Subcommand* action);
73  void add_subcommand(std::unique_ptr<Subcommand>&& action);
74 
75  Subcommand* find_action(const std::string& name) const;
76 
77  void usage(const std::string& selfpath, FILE* out) const;
78  void manpage(FILE* out) const;
79 
81  int main(int argc, const char* argv[]);
82 };
83 
85 struct error_cmdline : public std::exception
86 {
87  std::string msg;
88 
90  error_cmdline(const std::string& msg) : msg(msg) {}
91  ~error_cmdline() throw () {}
92 
93  virtual const char* what() const throw () { return msg.c_str(); }
94 
96  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
97 };
98 
99 
103 void dba_cmdline_print_dba_error();
104 
109 void dba_cmdline_error(poptContext optCon, const char* fmt, ...) __attribute__ ((noreturn));
110 
114 File::Encoding string_to_encoding(const char* type);
115 
121 unsigned dba_cmdline_get_query(poptContext optCon, Query& query);
122 
126 void list_templates();
127 
129 std::list<std::string> get_filenames(poptContext optCon);
130 
131 }
132 }
133 #endif
error_cmdline(const std::string &msg)
Definition: cmdline.h:90
Key/value store where keys are strings and values are wreport variables.
Definition: record.h:17
static void throwf(const char *fmt,...) WREPORT_THROWF_ATTRS(1
Throw the exception, building the message printf-style.
poptContext make_popt_context(int argc, const char *argv[], std::vector< poptOption > &opts) const
Create a popt context for this subcommand.
Definition: cmdline.h:27
void add_subcommand(Subcommand *action)
Add an action to this tool, taking ownership of its memory management.
virtual void init()
Optional initialization before main is called.
Query used to filter DB-All.e data.
Definition: query.h:14
std::string msg
error message returned by what()
Definition: cmdline.h:87
Report an error with command line options.
Definition: cmdline.h:85
Definition: cmdline.h:58
int main(int argc, const char *argv[])
Process commandline arguments and perform the action requested.