summaryrefslogtreecommitdiff
path: root/src/options.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/options.cpp')
-rw-r--r--src/options.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/options.cpp b/src/options.cpp
index 3581500..4d64b22 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -3,6 +3,8 @@
3#include "unitnumber.h" 3#include "unitnumber.h"
4#include "unitparser.h" 4#include "unitparser.h"
5#include "number.h" 5#include "number.h"
6#include "lexer.h"
7#include "parser.h"
6 8
7#include <bu/sio.h> 9#include <bu/sio.h>
8#include <stdlib.h> 10#include <stdlib.h>
@@ -26,6 +28,12 @@ Options::Options( int argc, char *argv[] ) :
26 "Convert the provided number to the given radix in the format " 28 "Convert the provided number to the given radix in the format "
27 "number:to-radix or from-radix:number:to-radix. The radix should " 29 "number:to-radix or from-radix:number:to-radix. The radix should "
28 "be provided in base-10"); 30 "be provided in base-10");
31 addOption( Bu::slot(this, &Options::execute), 'e', "execute",
32 "Execute the next parameters as an equation and print the result. "
33 "If you want a different scale or radix then set them first. This will "
34 "use all remaining parameters as the equation (you may wish to quote "
35 "your equation to prevent special characters from being interpreted by "
36 "your shell).");
29 addHelpOption('h', "help", "This help"); 37 addHelpOption('h', "help", "This help");
30 38
31 parse( argc, argv ); 39 parse( argc, argv );
@@ -158,3 +166,32 @@ int Options::convert( Bu::StringArray aArgs )
158 return 0; 166 return 0;
159} 167}
160 168
169int Options::execute( Bu::StringArray aArgs )
170{
171 if( aArgs.getSize() == 1 )
172 {
173 Bu::println("Must provide an equation to execute.");
174 exit( 0 );
175 return 0;
176 }
177 Bu::MemBuf mbIn(aArgs[1]);
178 mbIn.setPos( mbIn.getSize() );
179 for( int j = 2; j < aArgs.getSize(); j++ )
180 {
181 mbIn.write(" ");
182 mbIn.write( aArgs[j] );
183 }
184// Bu::println("eq: '''%1'''").arg( mbIn.getString() );
185 mbIn.setPos( 0 );
186 Bu::MemBuf mbOut;
187 Lexer lex( mbIn );
188 lex.setScale( iScale );
189 lex.setRadix( iRadix );
190 Parser parser( lex, mbOut );
191 parser.parse();
192 Bu::println( mbOut.getString() );
193 exit( 0 );
194 return aArgs.getSize();
195}
196
197