mapgd  0.4
A program for the Maximum-likelihood analysis of population genomic data.
 All Data Structures Functions Variables Friends Groups Pages
interface.h
1 /***** interface.h ********************************
2  * Description: Accepts arguments, sets flags, autoformats usage, verison, and help
3  * Not POSIX compliant...yet.
4  * Author: Matthew Ackerman
5  * Date: Mon Dec 09 12:00:00 2014.
6  * Licence: GNU General Public
7  ***************************************************/
8 
9 
10 #ifndef _INTERFACE_H_
11 #define _INTERFACE_H_
12 
13 #include <vector>
14 #include <list>
15 #include <iostream>
16 #include "typedef.h"
17 #include "region.h"
18 
19 #define ARG_ERROR -1
20 
21 /* /defgroup arg_parser Argument parsers
22  * @{
23 */
24 
26 //
27 int arg_set_vector_str(int, char **, void *);
28 inline int arg_set(int a, char **b, std::vector<std::string> &c){return arg_set_vector_str(a,b, &c); }
29 
31 //
32 int arg_set_region(int, char **, void *);
33 inline int arg_set(int a, char **b, Region &c){return arg_set_region(a,b, &c); }
34 
36 //
37 int arg_set_vector_uli(int, char **, void *);
38 inline int arg_set(int a, char **b, std::vector<unsigned long int> &c){return arg_set_vector_uli(a,b, &c); }
39 
40 int arg_set_vector_ulli(int, char **, void *);
41 inline int arg_set(int a, char **b, std::vector<unsigned long long int> &c){return arg_set_vector_ulli(a,b, &c); }
42 
44 //
45 int arg_set_vector_ui(int, char **, void *);
46 inline int arg_set(int a, char **b, std::vector<unsigned int> &c){return arg_set_vector_ui(a,b, &c); }
47 
50 int arg_set_int(int, char **, void *);
51 inline int arg_set(int a, char **b, int &c){return arg_set_int(a,b, &c); }
52 
54 //
55 int arg_set_vector_usi(int, char **, void *);
56 inline int arg_set(int a, char **b, std::vector<unsigned short int> &c){return arg_set_vector_usi(a,b, &c); }
57 
58 //<! Takes a reference to ...
59 int arg_set_str(int, char **, void *);
60 inline int arg_set(int a, char **b, std::string &c){return arg_set_str(a,b, &c); }
61 
62 //<! Same.
63 int arg_set_char(int, char **, void *);
64 inline int arg_set(int a, char **b, char &c){return arg_set_char(a,b, &c); }
65 
66 //<! Same.
67 int arg_set_float(int, char **, void *);
68 inline int arg_set(int a, char **b, float &c){return arg_set_float(a,b, &c); }
69 
70 //<! Same.
71 int arg_set_double(int, char **, void *);
72 inline int arg_set(int a, char **b, double &c){return arg_set_double(a,b, &c); }
73 
74 //<! Same.
75 int arg_set_long_double(int, char **, void *);
76 inline int arg_set(int a, char **b, long double &c){return arg_set_long_double(a,b, &c); }
77 
78 //<! Same.
79 //int arg_setc_str(int, char **, void *);
80 //int arg_set(int, char **, char **);
81 /* @}
82  */
83 
85 int flag_set(void *);
87 int flag_usage(void *);
89 int flag_version(void *);
91 int flag_help(void *);
93 int flag_commands(void *);
95 int flag_options(void *);
96 
98 int arg_error(int, char **, void *);
100 int flag_error(void *);
102 int command_error(int, char **);
103 
105 
107 class Flag {
108  private:
109  public:
110  Flag(){
111  opt='?';
112  lopt="error";
113  func=&flag_error;
114  emsg="emsg unset";
115  umsg="umsg unset";
116  };
117 
118  Flag (char opt_, char* lopt_, void *parm_, int (*func_)(void *), char *emsg_, char *umsg_){
119  opt=opt_;
120  lopt=lopt_;
121  func=func_;
122  parm=parm_;
123  emsg=emsg_;
124  umsg=umsg_;
125  };
126 
127  bool set;
128  char opt;
129  char *lopt;
130  void *parm;
131  int (*func)(void *);
132  char *emsg;
133  char *umsg;
134 };
135 
137 /*
138  *
139  */
140 class Argument {
141  private:
142  public:
143 
144  Argument(){
145  opt='?';
146  lopt="error";
147  func=&arg_error;
148  emsg="emsg unset";
149  umsg="umsg unset";
150  set=false;
151  required=false;
152  operand_type="none";
153  };
154 
156 
160  Argument(const char opt_,
161  char *lopt_,
162  void *parm_,
163  int (*func_)(int, char **, void *),
164  char *emsg_,
165  char *umsg_){
166  opt=opt_;
167  lopt=lopt_;
168  parm=parm_;
169  func=func_;
170  emsg=emsg_;
171  umsg=umsg_;
172  set=false;
173  required=false;
174  operand_type="none";
175  }
176 
177  template <class Type>
178  Argument(const char opt_,
179  char *lopt_,
180  Type &parm_,
181  char *emsg_,
182  char *umsg_){
183  opt=opt_;
184  lopt=lopt_;
185  parm=(void *)(&parm_);
186  func=(int (*)(int, char **, void*) )( (int (*)(int, char **, Type &) )&arg_set);
187  emsg=emsg_;
188  umsg=umsg_;
189  set=false;
190  required=false;
191  operand_type="none";
192  }
194  bool set;
195  bool required;
196  char opt;
197  char *lopt;
198  void *parm;
199  int (*func)(int, char **, void *);
200  char *emsg;
201  char *umsg;
202  char *operand_type;
204 };
205 
207 /* These are the subcommands of mapgd. They should generally take one or more
208  * data classes, and produce one or more data classes.
209  */
210 class Command {
211  private:
212  public:
213  Command(){
214  opt='?';
215  lopt="error";
216  emsg="emsg unset";
217  func=&command_error;
218  umsg="umsg unset";
219  set=false;
220  };
221  Command(char opt_, char* lopt_, int (*func_)(int, char **), char *emsg_, char*umsg_){
222  opt=opt_;
223  lopt=lopt_;
224  func=func_;
225  emsg=emsg_;
226  umsg=umsg_;
227  set=false;
228  };
229  bool set;
230  char opt;
231  char *lopt;
232  int (*func)(int, char **);
233  char *emsg;
234  char *umsg;
235 };
236 
238 /* TODO Write a long description you jerk!
239  *
240  */
241 class Environment{
242 private:
243 public:
244  void set_name(const char *c)
245  {
246  name=c;
247  };
248 
249  void set_version(const char *c)
250  {
251  version=c;
252  };
253 
254  void set_author(const char *c){
255  author=c;
256  };
257 
258  void set_description(const char *c){
260  };
261 
262  const char *name;
263  const char *version;
264  const char *author;
265  const char *description;
266  const char *footer_;
268  std::list <Flag> flags;
269  std::list <Argument> args;
270  std::list <Command> commands;
272  std::list <Argument *> required_args;
273  std::list <Argument *> positional_args;
274  std::list <Command *> required_coms;
275 
276  Environment(){
277  name="Unnamed program";
278  version="0.0";
279  author="Unknown";
280  description="Unknown purpose";
281  footer_="Fin";
282  }
283 
285  void optional_arg (char opt_, char* lopt_, void * parm_, int (*func_)(int, char **, void *), char *emsg_, char*umsg_)
286  {
287  args.push_back(Argument(opt_, lopt_, parm_, func_, emsg_, umsg_) );
288  }
289 
290  template <class Type>
291  inline void optional_arg (char opt_, char* lopt_, Type &parm_, char *emsg_, char*umsg_)
292  {
293  args.push_back(Argument(opt_, lopt_, parm_, emsg_, umsg_) );
294  }
295 
296  template <class Type>
297  void positional_arg (char opt_, char* lopt_, Type &parm_, char *emsg_, char*umsg_)
298  {
299  args.push_back(Argument(opt_, lopt_, parm_, emsg_, umsg_) );
300  positional_args.push_back(&args.back() );
301  }
302 
303  template <class Type>
304  void positional_arg (Type &parm_, char *emsg_, char*umsg_)
305  {
306  args.push_back(Argument("", "", parm_, emsg_, umsg_) );
307  positional_args.push_back(&args.back() );
308  }
309 
310 
315  void required_arg (char opt_, char* lopt_, void * parm_, int (*func_)(int, char **, void *), char *emsg_, char*umsg_)
316  {
317  args.push_back(Argument(opt_, lopt_, parm_, func_, emsg_, umsg_) );
318  args.back().required=true;
319  required_args.push_back(&args.back());
320 
321  }
322 
323  template <class Type>
324  void required_arg (char opt_, char* lopt_, Type &parm_, char *emsg_, char*umsg_)
325  {
326  args.push_back(Argument(opt_, lopt_, parm_, emsg_, umsg_) );
327  args.back().required=true;
328  required_args.push_back(&args.back());
329  }
330 
339  void command (char opt_, char* lopt_, int (*func_)(int, char **), char *emsg_, char*umsg_)
340  {
341  commands.push_back(Command(opt_, lopt_, func_, emsg_, umsg_) );
342  }
343 
348  void flag (char opt_, char* lopt_, void * parm_, int (*func_)(void *), char *emsg_, char*umsg_)
349  {
350  flags.push_back(Flag(opt_, lopt_, parm_, func_, emsg_, umsg_) );
351  }
352 
357  bool required_set (void)
358  {
359  std::list <Argument *>::iterator rarg=required_args.begin();
360  std::list <Argument *>::iterator end=required_args.end();
361  while(rarg!=end){
362  if(!(*rarg)->set) {std::cerr << (*rarg)->emsg << std::endl; return false;}
363  ++rarg;
364  };
365  return true;
366  }
367  void set_footer(const char *);
368  void close(void);
369 };
370 
371 void Usage(Environment);
376 int parsargs(int argc, char *argv[], Environment &env);
377 
380 void print_help(Environment env);
381 
384 void print_version(Environment env);
385 
388 void print_commands(Environment env);
389 
394 void print_usage(Environment env);
395 #endif
A command line argument. See the interface tutorial for a demonstration of usage. ...
Definition: interface.h:139
std::list< Command > commands
A list of sub-commands that can be called from the command line.
Definition: interface.h:269
std::list< Argument > args
A list of options that can be passed from the command line.
Definition: interface.h:268
void optional_arg(char opt_, char *lopt_, void *parm_, int(*func_)(int, char **, void *), char *emsg_, char *umsg_)
adds an optional argument to the list of arguments accepted by the program.
Definition: interface.h:284
bool required
flag toggles whether option is required.
Definition: interface.h:194
int(* func)(int, char **, void *)
the function to set the parameters.
Definition: interface.h:198
char * lopt
the long option name.
Definition: interface.h:196
A class that passes command line flags. It takes no arguments and chews only a single letter...
Definition: interface.h:106
char * emsg
A short error message to display when the proper parameters aren't passed to this option...
Definition: interface.h:131
const char * author
author(s)
Definition: interface.h:263
int(* func)(void *)
the function to set the parameters.
Definition: interface.h:130
std::list< Argument * > required_args
A list of options, all of which must be set.
Definition: interface.h:271
Definition: region.h:10
char * umsg
A short description of this command to be displayed in the usage and help message.
Definition: interface.h:233
bool set
flag toggles whether option has been set
Definition: interface.h:227
void command(char opt_, char *lopt_, int(*func_)(int, char **), char *emsg_, char *umsg_)
adds a function to be executed that can accept argc and argv [] arguments.
Definition: interface.h:338
int(* func)(int, char **)
the function to set the parameters
Definition: interface.h:231
char opt
the option name.
Definition: interface.h:195
char * umsg
Definition: interface.h:200
void * parm
pointer to the parameter to be set.
Definition: interface.h:129
char opt
the option name
Definition: interface.h:229
const char * version
the version
Definition: interface.h:262
const char * description
a brief description of the command being executed
Definition: interface.h:264
void required_arg(char opt_, char *lopt_, void *parm_, int(*func_)(int, char **, void *), char *emsg_, char *umsg_)
adds an required argument to the list of arguments accepted by the program.
Definition: interface.h:314
void flag(char opt_, char *lopt_, void *parm_, int(*func_)(void *), char *emsg_, char *umsg_)
adds a flag to the list of flags that can be accepted by the environment.
Definition: interface.h:347
std::list< Command * > required_coms
A list of sub-commands, one of which must be run.
Definition: interface.h:273
void * parm
pointer to the parameter to be set.
Definition: interface.h:197
bool set
flag toggles whether option has been set.
Definition: interface.h:124
const char * footer_
The ending text of the help menu.
Definition: interface.h:265
A sub-command of mapgd.
Definition: interface.h:209
bool set
flag toggles whether option has been set.
Definition: interface.h:193
char * emsg
A short error message to display when the proper parameters aren't passed to this option...
Definition: interface.h:232
const char * name
the name of the command
Definition: interface.h:259
A class for handling options and automatically formating –help, -h, -u and -v.
Definition: interface.h:240
char opt
the option name.
Definition: interface.h:127
std::list< Argument * > positional_args
A list of options that are called in order.
Definition: interface.h:272
char * lopt
the long option name.
Definition: interface.h:128
char * operand_type
A short description of this option to be displayed in the usage message.
Definition: interface.h:202
char * umsg
A short description of this option to be displayed in the usage message.
Definition: interface.h:132
char * lopt
the long option name
Definition: interface.h:230
bool required_set(void)
Checks to see if all required arguments are set.
Definition: interface.h:356
std::list< Flag > flags
A list of flags that can be set.
Definition: interface.h:267
char * emsg
A short error message to display when the proper parameters aren't passed to this option...
Definition: interface.h:199