1414using namespace std ;
1515using json = nlohmann::json;
1616
17+ const string APP_VERSION = " 1.1.1" ;
1718const string JSON_EXTENSION = " .json" ;
1819const size_t JSON_EXTENSION_LENGTH = JSON_EXTENSION.length();
1920const string ANY_DIGIT_REGEX = " [0-9]" ;
@@ -185,16 +186,34 @@ bool isJsonFile(const string& filePath){
185186 }
186187}
187188
189+ json createJSONRegex (const string& regex){
190+ json result;
191+ result[" regex" ] = regex;
192+ return result;
193+ }
194+
188195int main (int argc, char * argv[]) {
189- if ( argc != 4 ){
190- cerr << " Usage: " << argv[0 ] << " <input-file-path>.json <output-file-path> <regex-limit>" << " \n " ;
191- return 1 ;
192- }
193- string inputFilePath = argv[1 ];
194- string outputFilePath = argv[2 ];
196+ string inputFilePath, outputFilePath;
195197 int regexLengthLimit = 0 ;
198+
199+ // Argument parsing
196200 try {
197- regexLengthLimit = stoi (argv[3 ]);
201+ for (int i = 1 ; i < argc; ++i) {
202+ string arg = argv[i];
203+ if (arg == " --version" ) {
204+ cout << " PinRex Version " << APP_VERSION << endl;
205+ return 0 ;
206+ } else if (arg == " -i" && i + 1 < argc) {
207+ inputFilePath = argv[++i];
208+ } else if (arg == " -o" && i + 1 < argc) {
209+ outputFilePath = argv[++i];
210+ } else if (arg == " -l" && i + 1 < argc) {
211+ regexLengthLimit = stoi (argv[++i]);
212+ } else {
213+ cerr << " Usage: " << argv[0 ] << " -i <input-file-path> -o <output-file-path> -l <regex-limit>" << " \n " ;
214+ return 1 ;
215+ }
216+ }
198217 }
199218 catch (const exception& e){
200219 cerr << " Error occurred :.\n " << e.what () << " \n " ;
@@ -227,14 +246,20 @@ int main(int argc, char* argv[]) {
227246 postalCodes.push_back (to_string (code.get <int >()));
228247 }
229248
230- // Output the postal codes to verify
231- for (const auto & code : postalCodes) {
232- cout << " Postal Code: " << code << ' \n ' ;
233- }
234-
235249 Node* root = buildTreeFromPostalCodes (postalCodes);
236250 string regex = buildRegexFromTree (root);
237- cout << " Regex: " << regex << " \n " ;
238251
252+ // write the regex to the output file
253+ ofstream outputFile (outputFilePath);
254+ json result = createJSONRegex (regex);
255+ if (outputFile.is_open ()){
256+ outputFile << result.dump (4 );
257+ outputFile.close ();
258+ cout << " Regex saved to " << outputFilePath << endl;
259+ }
260+ else {
261+ cerr << " Error: Unable to open output file for writing." << endl;
262+ return 1 ;
263+ }
239264 return 0 ;
240265}
0 commit comments