]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/utils/bamtools_options.h
Reorganized source tree & build system
[bamtools.git] / src / utils / bamtools_options.h
diff --git a/src/utils/bamtools_options.h b/src/utils/bamtools_options.h
new file mode 100644 (file)
index 0000000..54c4764
--- /dev/null
@@ -0,0 +1,210 @@
+// ***************************************************************************
+// bamtools_options.h (c) 2010 Derek Barnett, Erik Garrison
+// Marth Lab, Department of Biology, Boston College
+// All rights reserved.
+// ---------------------------------------------------------------------------
+// Last modified: 1 June 2010
+// ---------------------------------------------------------------------------
+// Parses command line arguments and creates a help menu
+// ---------------------------------------------------------------------------
+// Modified from:
+// The Mosaik suite's command line parser class: COptions
+// (c) 2006 - 2009 Michael Str�mberg
+// Marth Lab, Department of Biology, Boston College
+// Dual licenced under the GNU General Public License 2.0+ license or as
+// a commercial license with the Marth Lab.
+//
+// * Modified to fit BamTools code-style, otherwise code is same. (DB)
+// ***************************************************************************
+
+#ifndef BAMTOOLS_OPTIONS_H
+#define BAMTOOLS_OPTIONS_H
+
+#include <map>
+#include <string>
+#include <vector>
+#include "bamtools_variant.h"
+
+#ifndef WIN32
+    #include <stdint.h>
+#endif
+
+namespace BamTools {
+
+#define ARGUMENT_LENGTH       35
+#define DESC_LENGTH_FIRST_ROW 50
+#define DESC_LENGTH           39
+#define MAX_LINE_LENGTH       78
+
+#ifdef WIN32
+  #define snprintf _snprintf
+  typedef __int64          int64_t;
+  typedef unsigned __int64 uint64_t;
+  #define strtoui64 _strtoui64
+#else
+  #define strtoui64 strtoull
+#endif
+
+struct Option {
+  
+    // data members
+    std::string Argument;
+    std::string ValueDescription;
+    std::string Description;
+    bool StoreValue;
+    bool HasDefaultValue;
+    Variant DefaultValue;
+
+    // constructor
+    Option(void)
+        : StoreValue(true)
+        , HasDefaultValue(false)
+    { }
+};
+
+struct OptionValue {
+  
+    // data members
+    bool* pFoundArgument;
+    void* pValue;
+    std::string ValueTypeDescription;
+    bool UseVector;
+    bool StoreValue;
+    bool IsRequired;
+    Variant VariantValue;
+
+    // constructor
+    OptionValue(void)
+        : pFoundArgument(NULL)
+        , pValue(NULL)
+        , UseVector(false)
+        , StoreValue(true)
+        , IsRequired(false)
+    { } 
+};
+
+struct OptionGroup {
+    std::string Name;
+    std::vector<Option> Options;
+};
+
+class Options {
+  
+    // add option/argument rules
+    public:
+        // adds a simple option to the parser
+        static void AddOption(const std::string& argument, 
+                       const std::string& optionDescription, 
+                       bool& foundArgument, 
+                       OptionGroup* group);
+                       
+        // adds a value option to the parser
+        template<typename T>
+        static void AddValueOption(const std::string& argument, 
+                            const std::string& valueDescription, 
+                            const std::string& optionDescription, 
+                            const std::string& valueTypeDescription, 
+                            bool& foundArgument, 
+                            T& val, 
+                            OptionGroup* group);
+                            
+        // adds a value option to the parser (with a default value)
+        template<typename T, typename D>
+        static void AddValueOption(const std::string& argument, 
+                            const std::string& valueDescription, 
+                            const std::string& optionDescription, 
+                            const std::string& valueTypeDescription, 
+                            bool& foundArgument, 
+                            T& val, 
+                            OptionGroup* group, 
+                            D& defaultValue);
+       
+    // other API methods
+    public:
+        // creates an option group
+        static OptionGroup* CreateOptionGroup(const std::string& groupName);    
+        // displays the help menu
+        static void DisplayHelp(void);
+        // parses the command line
+        static void Parse(int argc, char* argv[], int offset = 0);
+        // sets the program info
+        static void SetProgramInfo(const std::string& programName, const std::string& description, const std::string& arguments);
+        // returns string representation of stdin
+        static const std::string& StandardIn(void);
+        // returns string representation of stdout
+        static const std::string& StandardOut(void);
+        
+    // static data members
+    private:
+        // the program name
+        static std::string m_programName;
+        // the main description
+        static std::string m_description;
+        // the example arguments
+        static std::string m_exampleArguments;
+        // stores the option groups
+        static std::vector<OptionGroup> m_optionGroups;
+        // stores the options in a map
+        static std::map<std::string, OptionValue> m_optionsMap;
+        // string representation of stdin
+        static std::string m_stdin;
+        // string representation of stdout
+        static std::string m_stdout;
+};
+
+// adds a value option to the parser
+template<typename T>
+void Options::AddValueOption(const std::string& argument, 
+                             const std::string& valueDescription, 
+                             const std::string& optionDescription, 
+                             const std::string& valueTypeDescription, 
+                             bool& foundArgument, 
+                             T& val, 
+                             OptionGroup* group) 
+{
+        Option o;
+        o.Argument         = argument;
+        o.ValueDescription = valueDescription;
+        o.Description      = optionDescription;
+        group->Options.push_back(o);
+
+        OptionValue ov;
+        ov.pFoundArgument       = &foundArgument;
+        ov.pValue               = (void*)&val;
+        ov.VariantValue         = val;
+        ov.IsRequired           = (valueTypeDescription.empty() ? false : true);
+        ov.ValueTypeDescription = valueTypeDescription;
+        m_optionsMap[argument] = ov;
+}
+
+// adds a value option to the parser (with a default value)
+template<typename T, typename D>
+void Options::AddValueOption(const std::string& argument, 
+                             const std::string& valueDescription, 
+                             const std::string& optionDescription, 
+                             const std::string& valueTypeDescription, 
+                             bool& foundArgument, 
+                             T& val, 
+                             OptionGroup* group, 
+                             D& defaultValue) 
+{
+        Option o;
+        o.Argument         = argument;
+        o.ValueDescription = valueDescription;
+        o.Description      = optionDescription;
+        o.DefaultValue     = defaultValue;
+        o.HasDefaultValue  = true;
+        group->Options.push_back(o);
+
+        OptionValue ov;
+        ov.pFoundArgument       = &foundArgument;
+        ov.pValue               = (void*)&val;
+        ov.VariantValue         = val;
+        ov.IsRequired           = (valueTypeDescription.empty() ? false : true);
+        ov.ValueTypeDescription = valueTypeDescription;
+        m_optionsMap[argument] = ov;
+}
+
+} // namespace BamTools
+
+#endif // BAMTOOLS_OPTIONS_H
\ No newline at end of file