Browse Source

cleaned up options

naelstrof 7 years ago
parent
commit
ba04cef94b
2 changed files with 47 additions and 25 deletions
  1. 38
    21
      src/options.cpp
  2. 9
    4
      src/options.hpp

+ 38
- 21
src/options.cpp View File

1
 #include "options.hpp"
1
 #include "options.hpp"
2
 
2
 
3
+Options::Options( int argc, char** argv ) {
4
+    validArguments.push_back( Argument( "bordersize",   'b', false ) );
5
+    validArguments.push_back( Argument( "padding",      'p', false ) );
6
+    validArguments.push_back( Argument( "color",        'c', false ) );
7
+    validArguments.push_back( Argument( "shader",       'r', false ) );
8
+    validArguments.push_back( Argument( "highlight",    'h', true ) );
9
+    validArguments.push_back( Argument( "format",       'f', false ) );
10
+    validArguments.push_back( Argument( "tolerance",    't', false ) );
11
+    validArguments.push_back( Argument( "nodecorations", 'n', false ) );
12
+    validArguments.push_back( Argument( "nokeyboard",   'k', true ) );
13
+    validArguments.push_back( Argument( "help",         'h', true ) );
14
+    validArguments.push_back( Argument( "xdisplay",     'x', false ) );
15
+    validArguments.push_back( Argument( "version",      'v', true ) );
16
+    validArguments.push_back( Argument( "quiet",        'q', true ) );
17
+    validate( argc, argv );
18
+}
19
+
20
+
3
 int Options::validateStringOption( int argc, char** argv, int argumentIndex ) {
21
 int Options::validateStringOption( int argc, char** argv, int argumentIndex ) {
4
     std::string argument = argv[argumentIndex];
22
     std::string argument = argv[argumentIndex];
5
     unsigned int index = 0;
23
     unsigned int index = 0;
6
-    while( index < validArgumentCount ) {
7
-        std::string& check = validStringArguments[index];
8
-        for( unsigned int i=0;i<check.length();i++ ) {
9
-            if ( check[i] != argument[i+2] ) {
24
+    while( index < validArguments.size() ) {
25
+        Argument& check = validArguments[index];
26
+        for( unsigned int i=0;i<check.name.length();i++ ) {
27
+            if ( check.name[i] != argument[i+2] ) {
10
                 break;
28
                 break;
11
             }
29
             }
12
-            if ( i == check.length()-1 ) {
13
-                if ( !isFlagArgument[index] && argument.find("=") == std::string::npos ) {
30
+            if ( i == check.name.length()-1 ) {
31
+                if ( !check.isFlagArgument && argument.find("=") == std::string::npos ) {
14
                     throw new std::invalid_argument("Expected `=` after " + arguments[i]);
32
                     throw new std::invalid_argument("Expected `=` after " + arguments[i]);
15
                 }
33
                 }
16
-                if ( isFlagArgument[index] && i+3 != argument.length() ) {
34
+                if ( check.isFlagArgument && i+3 != argument.length() ) {
17
                     throw new std::invalid_argument("Trailing characters on flag " + argument );
35
                     throw new std::invalid_argument("Trailing characters on flag " + argument );
18
                 }
36
                 }
19
                 return parseStringOption( argc, argv, argumentIndex, index );
37
                 return parseStringOption( argc, argv, argumentIndex, index );
27
 
45
 
28
 int Options::parseCharOption( int argc, char** argv, int argumentIndex, int validIndex ) {
46
 int Options::parseCharOption( int argc, char** argv, int argumentIndex, int validIndex ) {
29
     std::string argument = argv[argumentIndex];
47
     std::string argument = argv[argumentIndex];
48
+    Argument& check = validArguments[validIndex];
30
     // If we're a flag, we take no arguments, nor do we allow = signs or whatever.
49
     // If we're a flag, we take no arguments, nor do we allow = signs or whatever.
31
-    if ( isFlagArgument[validIndex] ) {
32
-        if ( argument != std::string()+"-"+validCharArguments[validIndex] ) {
50
+    if ( check.isFlagArgument ) {
51
+        if ( argument != std::string()+"-"+check.cname ) {
33
             for( int o=1;o<argument.length();o++ ) {
52
             for( int o=1;o<argument.length();o++ ) {
34
                 bool isValid = false;
53
                 bool isValid = false;
35
-                for( int i=0;i<validArgumentCount&&!isValid;i++ ) {
36
-                    if ( argument[o] == validCharArguments[i] ) {
37
-                        if ( isFlagArgument[i] ) {
54
+                for( int i=0;i<validArguments.size()&&!isValid;i++ ) {
55
+                    if ( argument[o] == check.cname ) {
56
+                        if ( check.isFlagArgument ) {
38
                             isValid = true;
57
                             isValid = true;
39
-                            arguments.push_back( std::string()+validCharArguments[i] );
58
+                            arguments.push_back( std::string()+check.cname );
40
                             values.push_back("");
59
                             values.push_back("");
41
                             break;
60
                             break;
42
                         } else {
61
                         } else {
57
     }
76
     }
58
     arguments.push_back( std::string()+argument[1] );
77
     arguments.push_back( std::string()+argument[1] );
59
     // If they supplied the parameters with spaces
78
     // If they supplied the parameters with spaces
60
-    if ( argument == std::string()+"-"+validCharArguments[validIndex] ) {
79
+    if ( argument == std::string()+"-"+check.cname ) {
61
         values.push_back(argv[argumentIndex+1]);
80
         values.push_back(argv[argumentIndex+1]);
62
         return 2;
81
         return 2;
63
     }
82
     }
72
 
91
 
73
 int Options::parseStringOption( int argc, char** argv, int argumentIndex, int validIndex ) {
92
 int Options::parseStringOption( int argc, char** argv, int argumentIndex, int validIndex ) {
74
     std::string argument = argv[argumentIndex];
93
     std::string argument = argv[argumentIndex];
75
-    if ( isFlagArgument[validIndex] ) {
94
+    if ( validArguments[validIndex].isFlagArgument ) {
76
         arguments.push_back( argument.substr(2) );
95
         arguments.push_back( argument.substr(2) );
77
         values.push_back("");
96
         values.push_back("");
78
         return 1;
97
         return 1;
85
 int Options::validateCharOption( int argc, char** argv, int argumentIndex ) {
104
 int Options::validateCharOption( int argc, char** argv, int argumentIndex ) {
86
     std::string argument = argv[argumentIndex];
105
     std::string argument = argv[argumentIndex];
87
     unsigned int index = 0;
106
     unsigned int index = 0;
88
-    while( index < validArgumentCount ) {
89
-        char check = validCharArguments[index];
107
+    while( index < validArguments.size() ) {
108
+        char check = validArguments[index].cname;
90
         if ( argument.length() < 2 ) {
109
         if ( argument.length() < 2 ) {
91
             continue;
110
             continue;
92
         }
111
         }
107
             if ( floatingValues.size() > maxFloatingValues ) {
126
             if ( floatingValues.size() > maxFloatingValues ) {
108
                 throw new std::invalid_argument("Unexpected floating value `" + argument + "`. Forget to specify an option?" );
127
                 throw new std::invalid_argument("Unexpected floating value `" + argument + "`. Forget to specify an option?" );
109
             }
128
             }
129
+            i++;
130
+            continue;
110
         }
131
         }
111
         if ( argument[0] == '-' && argument[1] == '-' ) {
132
         if ( argument[0] == '-' && argument[1] == '-' ) {
112
             i += validateStringOption( argc, argv, i );
133
             i += validateStringOption( argc, argv, i );
116
     }
137
     }
117
 }
138
 }
118
 
139
 
119
-Options::Options( int argc, char** argv ) {
120
-    validate( argc, argv );
121
-}
122
-
123
 bool Options::getFloat( std::string name, char namec, float& found ) {
140
 bool Options::getFloat( std::string name, char namec, float& found ) {
124
     for( unsigned int i=0;i<arguments.size();i++ ) {
141
     for( unsigned int i=0;i<arguments.size();i++ ) {
125
         if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {
142
         if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {

+ 9
- 4
src/options.hpp View File

28
 #include <vector>
28
 #include <vector>
29
 #include <glm/glm.hpp>
29
 #include <glm/glm.hpp>
30
 
30
 
31
-static std::string validStringArguments[] = { "bordersize", "padding", "color", "shader", "highlight", "format", "tolerance", "nodecorations", "nokeyboard", "help", "xdisplay", "version", "quiet" };
32
-static char validCharArguments[] = { 'b', 'p', 'c', 'r', 'l', 'f', 't', 'n', 'k', 'h', 'x', 'v', 'q' };
33
-static unsigned int isFlagArgument[] = { false, false, false, false, true, false, false, false, true, true, false, true, true };
34
-static unsigned int validArgumentCount = 13;
31
+class Argument {
32
+public:
33
+    std::string name;
34
+    char cname;
35
+    bool isFlagArgument;
36
+    Argument( std::string name, char cname, bool isFlagArgument ) : name(name), cname(cname), isFlagArgument(isFlagArgument) {}
37
+};
38
+
39
+static std::vector<Argument> validArguments;
35
 static unsigned int maxFloatingValues = 0;
40
 static unsigned int maxFloatingValues = 0;
36
 
41
 
37
 class Options {
42
 class Options {