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