]> git.donarmstrong.com Git - bamtools.git/blob - bamtools_options.h
added warning for duplicate @RG tag in header
[bamtools.git] / bamtools_options.h
1 // ***************************************************************************
2 // bamtools_options.h (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 1 June 2010
7 // ---------------------------------------------------------------------------
8 // Parses command line arguments and creates a help menu
9 // ---------------------------------------------------------------------------
10 // Modified from:
11 // The Mosaik suite's command line parser class: COptions
12 // (c) 2006 - 2009 Michael Str�mberg
13 // Marth Lab, Department of Biology, Boston College
14 // Dual licenced under the GNU General Public License 2.0+ license or as
15 // a commercial license with the Marth Lab.
16 //
17 // * Modified to fit BamTools code-style, otherwise code is same. (DB)
18 // ***************************************************************************
19
20 #ifndef BAMTOOLS_OPTIONS_H
21 #define BAMTOOLS_OPTIONS_H
22
23 #include <map>
24 #include <string>
25 #include <vector>
26 #include "bamtools_variant.h"
27
28 #ifndef WIN32
29     #include <stdint.h>
30 #endif
31
32 namespace BamTools {
33
34 #define ARGUMENT_LENGTH       35
35 #define DESC_LENGTH_FIRST_ROW 42
36 #define DESC_LENGTH           39
37 #define MAX_LINE_LENGTH       78
38
39 #ifdef WIN32
40   #define snprintf _snprintf
41   typedef __int64          int64_t;
42   typedef unsigned __int64 uint64_t;
43   #define strtoui64 _strtoui64
44 #else
45   #define strtoui64 strtoull
46 #endif
47
48 struct Option {
49   
50     // data members
51     std::string Argument;
52     std::string ValueDescription;
53     std::string Description;
54     bool StoreValue;
55     bool HasDefaultValue;
56     Variant DefaultValue;
57
58     // constructor
59     Option(void)
60         : StoreValue(true)
61         , HasDefaultValue(false)
62     { }
63 };
64
65 struct OptionValue {
66   
67     // data members
68     bool* pFoundArgument;
69     void* pValue;
70     std::string ValueTypeDescription;
71     bool UseVector;
72     bool StoreValue;
73     bool IsRequired;
74     Variant VariantValue;
75
76     // constructor
77     OptionValue(void)
78         : pFoundArgument(NULL)
79         , pValue(NULL)
80         , UseVector(false)
81         , StoreValue(true)
82         , IsRequired(false)
83     { } 
84 };
85
86 struct OptionGroup {
87     std::string Name;
88     std::vector<Option> Options;
89 };
90
91 class Options {
92   
93     // add option/argument rules
94     public:
95         // adds a simple option to the parser
96         static void AddOption(const std::string& argument, 
97                        const std::string& optionDescription, 
98                        bool& foundArgument, 
99                        OptionGroup* group);
100                        
101         // adds a value option to the parser
102         template<typename T>
103         static void AddValueOption(const std::string& argument, 
104                             const std::string& valueDescription, 
105                             const std::string& optionDescription, 
106                             const std::string& valueTypeDescription, 
107                             bool& foundArgument, 
108                             T& val, 
109                             OptionGroup* group);
110                             
111         // adds a value option to the parser (with a default value)
112         template<typename T, typename D>
113         static void AddValueOption(const std::string& argument, 
114                             const std::string& valueDescription, 
115                             const std::string& optionDescription, 
116                             const std::string& valueTypeDescription, 
117                             bool& foundArgument, 
118                             T& val, 
119                             OptionGroup* group, 
120                             D& defaultValue);
121        
122     // other API methods
123     public:
124         // creates an option group
125         static OptionGroup* CreateOptionGroup(const std::string& groupName);    
126         // displays the help menu
127         static void DisplayHelp(void);
128         // parses the command line
129         static void Parse(int argc, char* argv[], int offset = 0);
130         // sets the program info
131         static void SetProgramInfo(const std::string& programName, const std::string& description, const std::string& arguments);
132         // returns string representation of stdin
133         static const std::string& StandardIn(void);
134         // returns string representation of stdout
135         static const std::string& StandardOut(void);
136         
137     // static data members
138     private:
139         // the program name
140         static std::string m_programName;
141         // the main description
142         static std::string m_description;
143         // the example arguments
144         static std::string m_exampleArguments;
145         // stores the option groups
146         static std::vector<OptionGroup> m_optionGroups;
147         // stores the options in a map
148         static std::map<std::string, OptionValue> m_optionsMap;
149         // string representation of stdin
150         static std::string m_stdin;
151         // string representation of stdout
152         static std::string m_stdout;
153 };
154
155 // adds a value option to the parser
156 template<typename T>
157 void Options::AddValueOption(const std::string& argument, 
158                              const std::string& valueDescription, 
159                              const std::string& optionDescription, 
160                              const std::string& valueTypeDescription, 
161                              bool& foundArgument, 
162                              T& val, 
163                              OptionGroup* group) 
164 {
165         Option o;
166         o.Argument         = argument;
167         o.ValueDescription = valueDescription;
168         o.Description      = optionDescription;
169         group->Options.push_back(o);
170
171         OptionValue ov;
172         ov.pFoundArgument       = &foundArgument;
173         ov.pValue               = (void*)&val;
174         ov.VariantValue         = val;
175         ov.IsRequired           = (valueTypeDescription.empty() ? false : true);
176         ov.ValueTypeDescription = valueTypeDescription;
177         m_optionsMap[argument] = ov;
178 }
179
180 // adds a value option to the parser (with a default value)
181 template<typename T, typename D>
182 void Options::AddValueOption(const std::string& argument, 
183                              const std::string& valueDescription, 
184                              const std::string& optionDescription, 
185                              const std::string& valueTypeDescription, 
186                              bool& foundArgument, 
187                              T& val, 
188                              OptionGroup* group, 
189                              D& defaultValue) 
190 {
191         Option o;
192         o.Argument         = argument;
193         o.ValueDescription = valueDescription;
194         o.Description      = optionDescription;
195         o.DefaultValue     = defaultValue;
196         o.HasDefaultValue  = true;
197         group->Options.push_back(o);
198
199         OptionValue ov;
200         ov.pFoundArgument       = &foundArgument;
201         ov.pValue               = (void*)&val;
202         ov.VariantValue         = val;
203         ov.IsRequired           = (valueTypeDescription.empty() ? false : true);
204         ov.ValueTypeDescription = valueTypeDescription;
205         m_optionsMap[argument] = ov;
206 }
207
208 } // namespace BamTools
209
210 #endif // BAMTOOLS_OPTIONS_H