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