]> git.donarmstrong.com Git - bamtools.git/blob - src/utils/bamtools_options.h
Added UTILS_EXPORT macro to classes in BamTools utility library
[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: 19 November 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 // 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 50
38 #define DESC_LENGTH           39
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, const std::string& description, const std::string& arguments);
134         // returns string representation of stdin
135         static const std::string& StandardIn(void);
136         // returns string representation of stdout
137         static const std::string& StandardOut(void);
138         
139     // static data members
140     private:
141         // the program name
142         static std::string m_programName;
143         // the main description
144         static std::string m_description;
145         // the example arguments
146         static std::string m_exampleArguments;
147         // stores the option groups
148         static std::vector<OptionGroup> m_optionGroups;
149         // stores the options in a map
150         static std::map<std::string, OptionValue> m_optionsMap;
151         // string representation of stdin
152         static std::string m_stdin;
153         // string representation of stdout
154         static std::string m_stdout;
155 };
156
157 // adds a value option to the parser
158 template<typename T>
159 void Options::AddValueOption(const std::string& argument, 
160                              const std::string& valueDescription, 
161                              const std::string& optionDescription, 
162                              const std::string& valueTypeDescription, 
163                              bool& foundArgument, 
164                              T& val, 
165                              OptionGroup* group) 
166 {
167         Option o;
168         o.Argument         = argument;
169         o.ValueDescription = valueDescription;
170         o.Description      = optionDescription;
171         group->Options.push_back(o);
172
173         OptionValue ov;
174         ov.pFoundArgument       = &foundArgument;
175         ov.pValue               = (void*)&val;
176         ov.VariantValue         = val;
177         ov.IsRequired           = (valueTypeDescription.empty() ? false : true);
178         ov.ValueTypeDescription = valueTypeDescription;
179         m_optionsMap[argument] = ov;
180 }
181
182 // adds a value option to the parser (with a default value)
183 template<typename T, typename D>
184 void Options::AddValueOption(const std::string& argument, 
185                              const std::string& valueDescription, 
186                              const std::string& optionDescription, 
187                              const std::string& valueTypeDescription, 
188                              bool& foundArgument, 
189                              T& val, 
190                              OptionGroup* group, 
191                              D& defaultValue) 
192 {
193         Option o;
194         o.Argument         = argument;
195         o.ValueDescription = valueDescription;
196         o.Description      = optionDescription;
197         o.DefaultValue     = defaultValue;
198         o.HasDefaultValue  = true;
199         group->Options.push_back(o);
200
201         OptionValue ov;
202         ov.pFoundArgument       = &foundArgument;
203         ov.pValue               = (void*)&val;
204         ov.VariantValue         = val;
205         ov.IsRequired           = (valueTypeDescription.empty() ? false : true);
206         ov.ValueTypeDescription = valueTypeDescription;
207         m_optionsMap[argument] = ov;
208 }
209
210 } // namespace BamTools
211
212 #endif // BAMTOOLS_OPTIONS_H