00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef O2SCL_CLI_H
00024 #define O2SCL_CLI_H
00025
00026 #include <iostream>
00027 #include <vector>
00028 #include <algorithm>
00029 #include <sstream>
00030
00031 #include <o2scl/collection.h>
00032 #include <o2scl/columnify.h>
00033
00034 #ifndef DOXYGENP
00035 namespace o2scl {
00036 #endif
00037
00038
00039
00040
00041
00042
00043 class comm_option_funct {
00044
00045 public:
00046
00047 comm_option_funct() {}
00048
00049 virtual ~comm_option_funct() {}
00050
00051
00052 virtual int operator()(std::vector<std::string> &cstr, bool itive_com) {
00053 return 0;
00054 }
00055
00056 #ifndef DOXYGENP
00057
00058 private:
00059 comm_option_funct(const comm_option_funct &);
00060 comm_option_funct& operator=(const comm_option_funct&);
00061
00062 #endif
00063
00064 };
00065
00066
00067 template<class tclass> class comm_option_mfptr : public comm_option_funct {
00068
00069 public:
00070
00071
00072 comm_option_mfptr(tclass *tp, int (tclass::*fp)(std::vector<std::string> &,
00073 bool)) {
00074 tptr=tp;
00075 fptr=fp;
00076 }
00077
00078 virtual ~comm_option_mfptr() {}
00079
00080
00081 virtual int operator()(std::vector<std::string> &cstr, bool itive_com) {
00082 return (*tptr.*fptr)(cstr,itive_com);
00083 }
00084
00085 #ifndef DOXYGEN_INTERNAL
00086
00087 protected:
00088
00089
00090 int (tclass::*fptr)(std::vector<std::string> &cstr, bool itive_com);
00091
00092
00093 tclass *tptr;
00094
00095 #endif
00096
00097 #ifndef DOXYGENP
00098
00099 private:
00100 comm_option_mfptr(const comm_option_mfptr &);
00101 comm_option_mfptr& operator=(const comm_option_mfptr&);
00102
00103 #endif
00104
00105 };
00106
00107
00108
00109
00110
00111
00112 typedef struct {
00113
00114
00115 char shrt;
00116
00117 std::string lng;
00118
00119 std::string desc;
00120
00121 int min_parms;
00122
00123 int max_parms;
00124
00125 std::string parm_desc;
00126
00127 std::string help;
00128
00129 comm_option_funct *func;
00130
00131 int type;
00132
00133 } comm_option_s;
00134
00135
00136
00137
00138
00139
00140 class comm_option : public comm_option_s {
00141
00142 public:
00143
00144 comm_option() {
00145 shrt=0;
00146 lng="";
00147 desc="";
00148 min_parms=0;
00149 max_parms=0;
00150 parm_desc="";
00151 help="";
00152 func=0;
00153 type=command;
00154 }
00155
00156 comm_option(comm_option_s c) {
00157 shrt=c.shrt;
00158 lng=c.lng;
00159 desc=c.desc;
00160 min_parms=c.min_parms;
00161 max_parms=c.max_parms;
00162 parm_desc=c.parm_desc;
00163 help=c.help;
00164 func=c.func;
00165 type=c.type;
00166 }
00167
00168
00169
00170 static const int command=0;
00171 static const int cl_param=1;
00172 static const int both=2;
00173
00174
00175 };
00176
00177
00178
00179 typedef struct {
00180
00181 std::string arg;
00182
00183 bool is_option;
00184
00185 bool is_valid;
00186
00187 std::vector<std::string> parms;
00188
00189 comm_option *cop;
00190 } cmd_line_arg;
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 class cli {
00206
00207 #ifndef DOXYGEN_INTERNAL
00208
00209 protected:
00210
00211
00212 int apply_alias(std::string &s, std::string sold, std::string snew);
00213
00214
00215 int separate(std::string str, std::vector<std::string> &sv);
00216
00217
00218 int verbose;
00219
00220
00221 collection *cop;
00222
00223
00224
00225 int comm_option_get(std::vector<std::string> &sv, bool itive_com);
00226 int comm_option_set(std::vector<std::string> &sv, bool itive_com);
00227 int comm_option_help(std::vector<std::string> &sv, bool itive_com);
00228 int comm_option_license(std::vector<std::string> &sv, bool itive_com);
00229 int comm_option_warranty(std::vector<std::string> &sv, bool itive_com);
00230 int comm_option_no_intro(std::vector<std::string> &sv, bool itive_com);
00231 int comm_option_alias(std::vector<std::string> &sv, bool itive_com);
00232
00233
00234
00235 char buf[300];
00236
00237
00238 comm_option_funct *user_set_func;
00239
00240
00241 std::vector<comm_option *> clist;
00242
00243
00244
00245 std::vector<std::string> ph_name, ph_desc;
00246
00247
00248
00249
00250 std::vector<std::string> al1, al2;
00251
00252
00253
00254 bool string_equal(std::string s1, std::string s2);
00255
00256 #endif
00257
00258 public:
00259
00260 cli();
00261
00262 virtual ~cli();
00263
00264
00265
00266
00267
00268
00269
00270 bool gnu_intro;
00271
00272
00273 int set_function(comm_option_funct &usf) {
00274 user_set_func=&usf;
00275 return 0;
00276 }
00277
00278
00279
00280 comm_option c_help;
00281 comm_option c_quit;
00282 comm_option c_exit;
00283 comm_option c_license;
00284 comm_option c_warranty;
00285 comm_option c_set;
00286 comm_option c_get;
00287 comm_option c_no_intro;
00288 comm_option c_alias;
00289
00290
00291
00292 bool sync_verbose;
00293
00294
00295
00296
00297 bool shell_cmd_allowed;
00298
00299
00300 std::string prompt;
00301
00302
00303 std::string desc;
00304
00305
00306 std::string cmd_name;
00307
00308
00309 std::string addl_help_cmd;
00310
00311
00312 std::string addl_help_cli;
00313
00314
00315 char *line_read;
00316
00317 virtual char *cli_gets(const char *c) {
00318 std::cout << c << std::flush;
00319 std::cin.getline(buf,300);
00320 return buf;
00321 }
00322
00323
00324 int call_args(std::vector<cmd_line_arg> &ca);
00325
00326
00327 int process_args(int argv, const char *argc[],
00328 std::vector<cmd_line_arg> &ca, int debug=0);
00329
00330
00331 int process_args(std::string s, std::vector<cmd_line_arg> &ca,
00332 int debug=0) {
00333 std::vector<std::string> sv;
00334 separate(s,sv);
00335 int argv=sv.size();
00336 const char **argc=new const char *[argv];
00337 for(int i=0;i<argv;i++) argc[i]=sv[i].c_str();
00338 int ret=process_args(argv,argc,ca,debug);
00339 delete[] argc;
00340 return ret;
00341 }
00342
00343
00344
00345
00346
00347
00348 int set_verbose(int v);
00349
00350
00351 int run_interactive();
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 int set_comm_option(comm_option &ic);
00365
00366
00367
00368
00369
00370 int set_parameters(collection &co);
00371
00372
00373 int set_param_help(std::string param, std::string help);
00374
00375
00376
00377
00378
00379
00380
00381 int set_alias(std::string alias, std::string str) {
00382 al1.push_back(alias);
00383 al2.push_back(str);
00384 return 0;
00385 }
00386
00387 };
00388
00389 #ifndef DOXYGENP
00390 }
00391 #endif
00392
00393 #endif