File: CommandLineParser.h

package info (click to toggle)
trinityrnaseq 2.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 212,452 kB
  • ctags: 5,067
  • sloc: perl: 45,552; cpp: 19,678; java: 11,865; sh: 1,485; makefile: 613; ansic: 427; python: 313; xml: 83
file content (469 lines) | stat: -rw-r--r-- 13,327 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */

#include <string>
#include <typeinfo>
#include <sstream>
#include <iostream>

#include <map>
#include <set>
#include <stdlib.h>

#include "base/StringUtil.h"
#include "base/FileParser.h"

using namespace std;

template<typename argType>
class commandArg
{

 public:

    commandArg() {}

    commandArg(string name, string descrip) {
        mName = name;
        mDesc = descrip;
        mHasDefault = false;
        mHasValue = false;
    }

    commandArg(string name,string descrip,argType T) {
        mName = name;
        mDesc = descrip;
        mValue = T;
        mHasDefault = true;
        mHasValue = true;
    }

    void SetValue(argType &T) { mValue = T; mHasValue=true; }

    void SetDefault(argType &T) { mValue = T; mHasDefault = true; }
    void SetDescription(string d) { mDesc = d; }

    bool HasDefault() { return mHasDefault; }
    bool HasValue() { return mHasValue; }

    argType GetValue() { return mValue; }
    string GetName() const { return mName; }
    string GetDescription() { return mDesc; }

    friend bool operator< (const commandArg<argType> &lhs,
                           const commandArg<argType> &rhs)
    {
        if ( lhs.GetName() < rhs.GetName() )
            return true;

        return false;
    }

    string GetType()
    {
        string type = typeid(*this).name();
        if ( type.find("Ib") != string::npos )
            return "bool";
        else if ( type.find("Ii") != string::npos )
            return "int";
        else if ( type.find("Id") != string::npos )
            return "double";
        else if ( type.find("ISs") != string::npos )
            return "string";
        else if (type.find("IlE") != string::npos )
            return "long";
        else {
            std::cerr << "*** unknown type for [" << type << "]" << endl;
            return "unknown type";
        }
    }


 private:

    string mName, mDesc;
    argType mValue;
    bool mHasDefault, mHasValue;

};


class commandLineParser
{

 public:

    commandLineParser(int argc, char** argv, const string & description = "") {
        mArgc = argc;
        mArgv = argv;
        mDesc = description;
        mNumDefaults = 0;
        mNumArgs = 0;
    }

    void SetDescription(const string & s) {
        mDesc = s;
    }

 private:
    int mArgc;
    char** mArgv;
    stringstream mHelp;
    string mDesc;

    int mNumDefaults,mNumArgs;
    multimap<string,string> mNameVal;
    set<string> mArgNames, mCArgNames;
 public:


    template<typename T>
        void registerArg(commandArg<T> &arg)
        {
            if ( mHelp.str().empty() )
                mHelp << "\n";
            mHelp << string(arg.GetName()
                            + "<"
                            + arg.GetType()
                            + "> : "
                            + arg.GetDescription() );
            if ( arg.HasDefault() )
                {
                    mHelp << " (def=";
                    ostringstream o;
                    o << arg.GetValue() ;
                    mHelp << o.str() << ")";
                    ++mNumDefaults;
                }
            mHelp << "\n" << ends;

            mArgNames.insert(arg.GetName());
            ++mNumArgs;

        }

    template<typename T>
        void registerCompoundArg(commandArg<T> &arg)
        {
            if ( mHelp.str().empty() )
                mHelp << "\n";
            mHelp << string(arg.GetName()
                            + "<"
                            + arg.GetType()
                            + "> : "
                            + arg.GetDescription() );
            if ( arg.HasDefault() )
                {
                    mHelp << " (def=";
                    ostringstream o;
                    o << arg.GetValue() ;
                    mHelp << o.str() << ")";
                    ++mNumDefaults;
                }
            mHelp << "\n" << ends;

            mCArgNames.insert(arg.GetName());
            ++mNumArgs;
        }


    bool parse()
    {
        if ( mArgc == 1 && mNumArgs != mNumDefaults)
            {
                showHelp();
                exit(-1);
            }

        if ( requestHelp() )
            {
                showHelp();
                exit(-1);
            }

        if ( mArgNames.empty() )
            {
                showHelp();
                exit(-1);
            }

        int i(1);
        while ( i<mArgc )
            {
                //   for ( int i=1; i<mArgc; ++i )
                //   {


                string n(mArgv[i]);//, v(mArgv[i+1]);
                string v("");
                if ( i<mArgc-1)
                    v=string(mArgv[i+1]);

                bool is_compound(false);
                set<string>::iterator sIter = mArgNames.find(n);
                if ( sIter == mArgNames.end() )
                    {
                        sIter = mCArgNames.find(n);
                        if (sIter==mCArgNames.end())
                            {
                                if (n == "-print-command-line") {
                                    cout << "----------------------- Welcome to Trinity -------------------------------" << endl;
                                    cout << "This module was invoked via:" << endl;
                                    for (int k=0; k<mArgc; k++) {
                                        cout << mArgv[k] << " ";
                                    }
                                    cout << endl;
                                    cout << "----------------------- Welcome to Trinity -------------------------------" << endl << endl;
                                    if ( mArgc == 2 ) {
                                        showHelp();
                                        exit(-1);
                                    }
                                    i++;
                                    continue;
                                } else {

                                    cout << "\nInvalid command-line arg: " << n << endl;
                                    showHelp();
                                    exit(-1);
                                }
                            }
                        else
                            {
                                is_compound=true;
                            }
                    }

                string targ("-");
                StringParser sp; sp.SetLine(v);
                int num_v=sp.GetItemCount();

                if (!is_compound)
                    {
                        bool is_float(false);
                        if (num_v>1) is_float = sp.IsFloat(1);

                        if (ContainsAt(n,targ,0) && is_float)
                            {
                                mNameVal.insert(make_pair(n,v));
                                i+=2;
                            }
                        else if ( ContainsAt(n,targ,0) && !ContainsAt(v,targ,0) )
                            {
                                mNameVal.insert(make_pair(n,v));
                                i+=2;
                            }
                        else if ( ContainsAt(n,targ,0) && ContainsAt(v,targ,0) )
                            {
                                v="";
                                mNameVal.insert(make_pair(n,v));
                                i+=1;
                            }
                        else
                            {
                                i+=1;
                            }
                    }
                else
                    {
                        for (int k=0; k<num_v; ++k)
                            mNameVal.insert(make_pair(n,sp.AsString(k)));
                        i+=2;
                    }
            }
        return true;
    }

    template<typename T>
        string GetStringValueFor(commandArg<T> &keyArg)
        {
            string key(keyArg.GetName());
            map<string,string>::iterator mIter = mNameVal.find(key);
            if ( mIter == mNameVal.end() )
                {
                    ostringstream o;
                    o << keyArg.GetValue();

                    if ( keyArg.GetType() == "string" )
                        {
                            if ( !keyArg.HasDefault() )
                                {
                                    cout << "need to specify " << key << endl;
                                    exit(-1);
                                }
                            return (o.str());
                        }
                    return (string(""));
                }

            return (mIter->second);

        }

    template<typename T>
        vector<string> GetCompoundStringValuesFor(commandArg<T> &keyArg)
        {
            vector<string> dummy;

            string key(keyArg.GetName());
            pair<multimap<string,string>::iterator,multimap<string,string>::iterator>  mIter = mNameVal.equal_range(key);
            if ( mIter.first == mIter.second)
                {
                    ostringstream o;
                    o << keyArg.GetValue();

                    if ( keyArg.GetType() == "string" )
                        {
                            if ( !keyArg.HasDefault() )
                                {
                                    cout << "need to specify " << key << endl;
                                    exit(-1);
                                }

                            dummy.push_back(o.str());
                            return (dummy);
                        }
                    return (dummy);
                }

            for (; mIter.first != mIter.second; ++mIter.first)
                dummy.push_back(mIter.first->second);

            return dummy;

        }


    double GetDoubleValueFor(commandArg<double> &keyArg)
    {
        string key(keyArg.GetName());
        string val = GetStringValueFor(keyArg);

        if ( val.empty() )
            {
                if (!keyArg.HasDefault() )
                    {
                        cout << "need to specify " << key << endl;
                        exit(-1);
                    }
                else
                    return ( keyArg.GetValue() );
            }
        else
            {
                //     if ( !val.IsDouble() )
                //     {
                //       cout << "invalid: " << key <<" "<< val << endl;
                //       exit(-1);
                //     }
                //     else
                return (atof(val.c_str()));

            }
    }

    int GetIntValueFor(commandArg<int> &keyArg)
    {
        string key(keyArg.GetName());
        string val = GetStringValueFor(keyArg);
        if ( val.empty() )
            {
                if (!keyArg.HasDefault() )
                    {
                        cout << "need to specify " << key << endl;
                        exit(-1);
                    }
                else
                    return ( keyArg.GetValue() );
            }
        else
            {
                //     if ( !val.IsInt() )
                //     {
                //       cout << "invalid: " << key <<" "<< val << endl;
                //       exit(-1);
                //     }
                //     else
                return (atoi(val.c_str()));

            }
    }

    int GetLongValueFor(commandArg<long> &keyArg)
    {
        string key(keyArg.GetName());
        string val = GetStringValueFor(keyArg);
        if ( val.empty() )
            {
                if (!keyArg.HasDefault() )
                    {
                        cout << "need to specify " << key << endl;
                        exit(-1);
                    }
                else
                    return ( keyArg.GetValue() );
            }
        else
            {
                //     if ( !val.IsInt() )
                //     {
                //       cout << "invalid: " << key <<" "<< val << endl;
                //       exit(-1);
                //     }
                //     else
                return (atol(val.c_str()));

            }
    }







    bool GetBoolValueFor(commandArg<bool> &keyArg)
    {
        string key(keyArg.GetName());
        map<string,string>::iterator mIter = mNameVal.find(key);
        if ( mIter == mNameVal.end() )
            {
                if ( keyArg.HasDefault() )
                    {
                        return keyArg.GetValue();
                    }
                else
                    {
                        cout << "need to specify " << key << endl;
                        exit(-1);
                    }
            }
        else
            return true;
    }


    bool requestHelp()
    {
        for ( int i=1; i<mArgc; ++i )
            {
                string this_arg(mArgv[i]);
                if ( this_arg == "-h" )
                    return true;
            }

        return false;
    }

    void showHelp()
    {
        cout << endl << mArgv[0] << ": ";
        if (mDesc != "")
            cout << mDesc << endl << endl;
        else
            cout << "a module in the code base 'Trinity'." << endl << endl;
        cout << "\nAvailable arguments:" << endl;
        cout << mHelp.str() << endl;
    }


};