libdballe  8.6
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 <dballe/fwd.h>
12 #include <popt.h>
13 #include <memory>
14 #include <vector>
15 #include <list>
16 #include <string>
17 
18 namespace dballe {
19 namespace cmdline {
20 
21 struct Subcommand
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[], std::vector<poptOption>& opts) const;
47  void manpage_print_options(FILE* out);
48 };
49 
50 #define ODT_END { NULL, NULL, NULL, NULL, NULL, NULL }
51 
52 struct Command
53 {
54  std::string name;
55  std::string desc;
56  std::string longdesc;
57  std::string manpage_examples_section;
58  std::string manpage_files_section;
59  std::string manpage_seealso_section;
60 
61  std::vector<Subcommand*> ops;
62 
63  ~Command();
64 
66  void add_subcommand(Subcommand* action);
67  void add_subcommand(std::unique_ptr<Subcommand>&& action);
68 
69  Subcommand* find_action(const std::string& name) const;
70 
71  void usage(const std::string& selfpath, FILE* out) const;
72  void manpage(FILE* out) const;
73 
75  int main(int argc, const char* argv[]);
76 };
77 
79 struct error_cmdline : public std::exception
80 {
81  std::string msg;
82 
84  error_cmdline(const std::string& msg) : msg(msg) {}
85  ~error_cmdline() throw () {}
86 
87  virtual const char* what() const throw () { return msg.c_str(); }
88 
90  static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2);
91 };
92 
93 
98 
103 void dba_cmdline_error(poptContext optCon, const char* fmt, ...) __attribute__ ((noreturn));
104 
108 Encoding string_to_encoding(const char* type);
109 
115 unsigned dba_cmdline_get_query(poptContext optCon, Query& query);
116 
120 void list_templates();
121 
123 std::list<std::string> get_filenames(poptContext optCon);
124 
125 }
126 }
127 #endif
error_cmdline(const std::string &msg)
Definition: cmdline.h:84
void dba_cmdline_error(poptContext optCon, const char *fmt,...) __attribute__((noreturn))
Print an error that happened when parsing commandline arguments, then add usage informations and exit...
poptContext make_popt_context(int argc, const char *argv[], std::vector< poptOption > &opts) const
Create a popt context for this subcommand.
Definition: cmdline.h:18
Encoding string_to_encoding(const char *type)
Return the File::Encoding that corresponds to the name in the string.
unsigned dba_cmdline_get_query(poptContext optCon, Query &query)
Get a DB-ALLe query from commandline parameters in the form key=value.
Definition: cmdline.h:21
virtual void init()
Optional initialization before main is called.
std::list< std::string > get_filenames(poptContext optCon)
Read all the command line arguments and return them as a list.
std::string msg
error message returned by what()
Definition: cmdline.h:81
void dba_cmdline_print_dba_error()
Print informations about the last error to stderr.
#define WREPORT_THROWF_ATTRS(a, b)
Report an error with command line options.
Definition: cmdline.h:79
Definition: cmdline.h:52
Query used to filter DB-All.e data.
Definition: query.h:14
void list_templates()
List available output templates.