Pārlūkot izejas kodu

cleaned up options

naelstrof 7 gadus atpakaļ
vecāks
revīzija
ba04cef94b
2 mainītis faili ar 47 papildinājumiem un 25 dzēšanām
  1. 38
    21
      src/options.cpp
  2. 9
    4
      src/options.hpp

+ 38
- 21
src/options.cpp Parādīt failu

@@ -1,19 +1,37 @@
1 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 21
 int Options::validateStringOption( int argc, char** argv, int argumentIndex ) {
4 22
     std::string argument = argv[argumentIndex];
5 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 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 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 35
                     throw new std::invalid_argument("Trailing characters on flag " + argument );
18 36
                 }
19 37
                 return parseStringOption( argc, argv, argumentIndex, index );
@@ -27,16 +45,17 @@ int Options::validateStringOption( int argc, char** argv, int argumentIndex ) {
27 45
 
28 46
 int Options::parseCharOption( int argc, char** argv, int argumentIndex, int validIndex ) {
29 47
     std::string argument = argv[argumentIndex];
48
+    Argument& check = validArguments[validIndex];
30 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 52
             for( int o=1;o<argument.length();o++ ) {
34 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 57
                             isValid = true;
39
-                            arguments.push_back( std::string()+validCharArguments[i] );
58
+                            arguments.push_back( std::string()+check.cname );
40 59
                             values.push_back("");
41 60
                             break;
42 61
                         } else {
@@ -57,7 +76,7 @@ int Options::parseCharOption( int argc, char** argv, int argumentIndex, int vali
57 76
     }
58 77
     arguments.push_back( std::string()+argument[1] );
59 78
     // If they supplied the parameters with spaces
60
-    if ( argument == std::string()+"-"+validCharArguments[validIndex] ) {
79
+    if ( argument == std::string()+"-"+check.cname ) {
61 80
         values.push_back(argv[argumentIndex+1]);
62 81
         return 2;
63 82
     }
@@ -72,7 +91,7 @@ int Options::parseCharOption( int argc, char** argv, int argumentIndex, int vali
72 91
 
73 92
 int Options::parseStringOption( int argc, char** argv, int argumentIndex, int validIndex ) {
74 93
     std::string argument = argv[argumentIndex];
75
-    if ( isFlagArgument[validIndex] ) {
94
+    if ( validArguments[validIndex].isFlagArgument ) {
76 95
         arguments.push_back( argument.substr(2) );
77 96
         values.push_back("");
78 97
         return 1;
@@ -85,8 +104,8 @@ int Options::parseStringOption( int argc, char** argv, int argumentIndex, int va
85 104
 int Options::validateCharOption( int argc, char** argv, int argumentIndex ) {
86 105
     std::string argument = argv[argumentIndex];
87 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 109
         if ( argument.length() < 2 ) {
91 110
             continue;
92 111
         }
@@ -107,6 +126,8 @@ void Options::validate( int argc, char** argv ) {
107 126
             if ( floatingValues.size() > maxFloatingValues ) {
108 127
                 throw new std::invalid_argument("Unexpected floating value `" + argument + "`. Forget to specify an option?" );
109 128
             }
129
+            i++;
130
+            continue;
110 131
         }
111 132
         if ( argument[0] == '-' && argument[1] == '-' ) {
112 133
             i += validateStringOption( argc, argv, i );
@@ -116,10 +137,6 @@ void Options::validate( int argc, char** argv ) {
116 137
     }
117 138
 }
118 139
 
119
-Options::Options( int argc, char** argv ) {
120
-    validate( argc, argv );
121
-}
122
-
123 140
 bool Options::getFloat( std::string name, char namec, float& found ) {
124 141
     for( unsigned int i=0;i<arguments.size();i++ ) {
125 142
         if ( arguments[i] == name || arguments[i] == std::string("")+namec ) {

+ 9
- 4
src/options.hpp Parādīt failu

@@ -28,10 +28,15 @@
28 28
 #include <vector>
29 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 40
 static unsigned int maxFloatingValues = 0;
36 41
 
37 42
 class Options {