refactor(cli): improve argument parsing and update special characters
All checks were successful
CMake / build (push) Successful in 42s
All checks were successful
CMake / build (push) Successful in 42s
- Enhance CLI handling in main.cpp with better error messages, default length as size_t, and proper option validation - Simplify special characters in passwdgen.cpp to a safer, common subset
This commit is contained in:
parent
5de5884aef
commit
24adb7d618
44
src/main.cpp
44
src/main.cpp
@ -7,38 +7,42 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
int passwordLength = 0;
|
std::string progname(argv[0]);
|
||||||
|
size_t passwordLength = 32;
|
||||||
bool punc = false;
|
bool punc = false;
|
||||||
|
|
||||||
if (argc < 1) {
|
int i = 1;
|
||||||
std::cout << random_string(32, false) << std::endl;
|
while (i < argc) {
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
for (int i = 1; i < argc; ++i) {
|
|
||||||
std::string arg = argv[i];
|
std::string arg = argv[i];
|
||||||
if ((arg == "-h") || (arg == "--help")) {
|
if (arg == "-h" || arg == "--help") {
|
||||||
show_usage(argv[0]);
|
show_usage(progname);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
} else if (arg == "-p" || arg == "--punctuation") {
|
||||||
if (arg == "-p") {
|
|
||||||
punc = true;
|
punc = true;
|
||||||
} else if ((arg == "-l") || (arg == "--length")) {
|
++i;
|
||||||
if (not argv[i +1]) {
|
} else if (arg == "-l" || arg == "--length") {
|
||||||
std::cerr << "Specify a password length" << std::endl;
|
if (++i >= argc) {
|
||||||
|
std::cerr << progname << ": error: option requires an argument -- " << arg << std::endl;
|
||||||
|
show_usage(progname);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
std::string passwordLengthStr = argv[i + 1];
|
|
||||||
try {
|
try {
|
||||||
passwordLength = std::stoi(passwordLengthStr);
|
int tmp = std::stoi(argv[i]);
|
||||||
} catch ([[maybe_unused]] const std::invalid_argument& e){
|
if (tmp < 1) {
|
||||||
std::cerr << "Length must be a valid integer" << std::endl;
|
std::cerr << progname << ": invalid argument '" << argv[i] << "' for option " << arg << ": value too small" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
passwordLength = static_cast<size_t>(tmp);
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << progname << ": invalid argument '" << argv[i] << "' for option " << arg << ": " << e.what() << std::endl;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
++i;
|
||||||
|
} else {
|
||||||
|
std::cerr << progname << ": unrecognized option '" << arg << "'" << std::endl;
|
||||||
|
show_usage(progname);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (passwordLength == 0) {
|
|
||||||
passwordLength = 32;
|
|
||||||
}
|
}
|
||||||
std::string password = random_string(passwordLength, punc);
|
std::string password = random_string(passwordLength, punc);
|
||||||
std::cout << password << std::endl;
|
std::cout << password << std::endl;
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
std::string random_string(std::size_t length, bool punc) {
|
std::string random_string(std::size_t length, bool punc) {
|
||||||
std::string CHARACTERS;
|
std::string CHARACTERS;
|
||||||
std::string ALPHANUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
std::string ALPHANUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
const std::string SPECIALS = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
|
const std::string SPECIALS = "!@#$%^&*()_+-=[]{}|;:,./?";
|
||||||
|
|
||||||
if (punc) {
|
if (punc) {
|
||||||
CHARACTERS = ALPHANUM.append(SPECIALS);
|
CHARACTERS = ALPHANUM.append(SPECIALS);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user