Skip to content

Commit

Permalink
Refactor Argument Parsing Using a Function Map for Enhanced Modularity
Browse files Browse the repository at this point in the history
- Function Map (std::unordered_map):

Each argument is now directly associated with a specific function, improving clarity and reducing code complexity.
Eliminated multiple if-else statements for better readability.
Added extensibility: introducing new arguments is as simple as extending the argHandlers map.

- Modularity:

Encapsulated the logic for each argument into dedicated lambda functions.
Decoupled functions like showHelp and checkupdate for cleaner design.

- Error Handling:

Integrated std::filesystem to validate directory paths.
Added error messages and immediate termination for unrecognized arguments.

- Extensibility:

Simplified the process of adding arguments by centralizing control in argHandlers.

- Centralized Flag Management:

Directly manage flags (parameAFlag, parameDFlag, etc.) within the lambda functions of the map.
  • Loading branch information
rompelhd authored Jan 21, 2025
1 parent d11bf10 commit 91ec154
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions src/etree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,56 +294,49 @@ void showHelp(const std::map<std::string, std::string>& translations) {
std::cout << translations.at("help_option") << std::endl;
}

void param(int argc, char *argv[]) {
void param(int argc, char* argv[]) {
// Var
std::string directory = ".";
bool parameNFlag = false, parameAFlag = false, parameDFlag = false, parameFFlag = false;
bool parameLFlag = false, parameNRFlag = false, parameUFlag = false;

bool parameNFlag = false, parameAFlag = false, parameDFlag = false, parameFFlag = false, parameLFlag = false, parameNRFlag = false, parameUFlag = false;

// Config
std::string languageCode = ConfigLoad(Paths::getEtreeFolderPath() + "etree.conf");
std::string languageFilePath = Paths::getLanguageFilePath(languageCode);
std::map<std::string, std::string> translations = loadTranslations(languageFilePath);
std::set<std::string> recognizedArgs = {"--help", "--version", "--noreport", "-a", "-L", "-d", "-f", "-n", "-u"};

for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];

if (arg == "--help") {
showHelp(translations);
return;
} else if (arg == "--version") {
std::cout << Colours::blueColour << "Etree " << Colours::yellowColour << translations.at("version") << Colours::blueColour << " by " << Colours::redColour << "Rompelhd" << std::endl;
std::unordered_map<std::string, std::function<void()>> argHandlers = {
{"--help", [&]() { showHelp(translations); exit(0); }},
{"--version", [&]() {
std::cout << Colours::blueColour << "Etree " << Colours::yellowColour
<< translations.at("version") << Colours::blueColour
<< " by " << Colours::redColour << "Rompelhd" << std::endl;
checkupdate(translations);
return;
} else if (arg == "-a") {
parameAFlag = true;
} else if (arg == "-L") {
parameLFlag = true;
} else if (arg == "-d") {
parameDFlag = true;
} else if (arg == "-f") {
parameFFlag = true;
} else if (arg == "-u") {
parameUFlag = true;
} else if (arg == "--noreport") {
parameNRFlag = true;
} else if (arg == "-n") {
exit(0);
}},
{"-a", [&]() { parameAFlag = true; }},
{"-L", [&]() { parameLFlag = true; }},
{"-d", [&]() { parameDFlag = true; }},
{"-f", [&]() { parameFFlag = true; }},
{"-u", [&]() { parameUFlag = true; }},
{"--noreport", [&]() { parameNRFlag = true; }},
{"-n", [&]() {
parameNFlag = true;
if (i + 1 < argc) {
directory = argv[i + 1];
break;
}
} else {
directory = argv[i];
break;
}
}
if (argc > 1) directory = argv[argc - 1];
}}
};

for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg[0] == '-' && recognizedArgs.find(arg) == recognizedArgs.end()) {

if (argHandlers.count(arg)) {
argHandlers[arg]();
} else if (arg[0] == '-') {
std::cerr << "Etree: Argumento no reconocido: " << arg << std::endl;
showHelp(translations);
exit(1);
} else {
directory = arg;
}
}

Expand All @@ -359,11 +352,15 @@ void param(int argc, char *argv[]) {
tree.walk(directory, "", parameAFlag, parameDFlag, parameFFlag, parameNFlag);

if (!parameNRFlag) {
std::cout << "\n" << Colours::greenColour << dirs << Colours::endColour << " " << Colours::blueColour << translations.at("directories_trn") << Colours::endColour << ", " << Colours::greenColour << files << Colours::purpleColour << " " << translations.at("files_trn") << Colours::endColour << std::endl;
std::cout << "\n" << Colours::greenColour << dirs << Colours::endColour
<< " " << Colours::blueColour << translations.at("directories_trn")
<< Colours::endColour << ", " << Colours::greenColour << files
<< Colours::purpleColour << " " << translations.at("files_trn")
<< Colours::endColour << std::endl;
}
}

int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
param(argc, argv);
return 0;
}

0 comments on commit 91ec154

Please sign in to comment.