Skip to content

Commit 57a4721

Browse files
committed
added flags to command line arguments
1 parent f5d1d64 commit 57a4721

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/main.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using namespace std;
1515
using json = nlohmann::json;
1616

17+
const string APP_VERSION = "1.1.1";
1718
const string JSON_EXTENSION = ".json";
1819
const size_t JSON_EXTENSION_LENGTH = JSON_EXTENSION.length();
1920
const 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+
188195
int 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
}

static/output.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"regex": "^20(1(008|30[^02])|2399|3(1(3[15]|41|55)|20[^0456]))"
3+
}

0 commit comments

Comments
 (0)