passwdgen/main.cpp
Greg Gauthier ed0b6ed110
All checks were successful
CMake / build (push) Successful in 1m1s
refactor, and add proper tests
2025-09-05 12:43:46 +01:00

47 lines
1.3 KiB
C++

//
// Created by gmgauthier on 05/09/25.
//
// main.cpp
#include "passwdgen.h"
#include <iostream>
#include <string>
int main(int argc, char *argv[]) {
int passwordLength = 0;
bool punc = false;
if (argc < 1) {
std::cout << random_string(32, false) << std::endl;
return 0;
}
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if ((arg == "-h") || (arg == "--help")) {
show_usage(argv[0]);
return 0;
}
if (arg == "-p") {
punc = true;
} else if ((arg == "-l") || (arg == "--length")) {
if (not argv[i +1]) {
std::cerr << "Specify a password length" << std::endl;
return 1;
}
std::string passwordLengthStr = argv[i + 1];
try {
passwordLength = std::stoi(passwordLengthStr);
} catch ([[maybe_unused]] const std::invalid_argument& e){
std::cerr << "Length must be a valid integer" << std::endl;
return 1;
}
}
}
if (passwordLength == 0) {
passwordLength = 32;
}
std::string password = random_string(passwordLength, punc);
std::cout << password << std::endl;
return 0;
}