rename source file; fix argument parsing;
This commit is contained in:
parent
8a342387f6
commit
bde4be616f
@ -3,4 +3,4 @@ project(passwdgen)
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
add_executable(passwdgen main.cpp)
|
add_executable(passwdgen passwdgen.cpp)
|
@ -1,6 +1,12 @@
|
|||||||
|
//
|
||||||
|
// passwdgen.cpp
|
||||||
|
// Created by Greg Gauthier on 2020.10.22.
|
||||||
|
//
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
|
|
||||||
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";
|
||||||
@ -13,7 +19,7 @@ std::string random_string(std::size_t length, bool punc) {
|
|||||||
}
|
}
|
||||||
std::random_device random_device;
|
std::random_device random_device;
|
||||||
std::mt19937 generator(random_device());
|
std::mt19937 generator(random_device());
|
||||||
std::uniform_int_distribution<> distribution(0, CHARACTERS.size() - 1);
|
std::uniform_int_distribution<> distribution(0.0, CHARACTERS.size() - 1.0);
|
||||||
|
|
||||||
std::string random_string;
|
std::string random_string;
|
||||||
for (std::size_t i = 0; i < length; ++i) {
|
for (std::size_t i = 0; i < length; ++i) {
|
||||||
@ -25,15 +31,15 @@ std::string random_string(std::size_t length, bool punc) {
|
|||||||
void show_usage(std::string binname) {
|
void show_usage(std::string binname) {
|
||||||
std::cerr << "Usage: " << binname << " [OPTIONS] " << std::endl
|
std::cerr << "Usage: " << binname << " [OPTIONS] " << std::endl
|
||||||
<< "Options:" << std::endl
|
<< "Options:" << std::endl
|
||||||
<< "\t-h, --help \t\tShow this help message" << std::endl
|
<< "\t-h, --help \t\tShow this help message" << std::endl
|
||||||
<< "\t-l, --length \t\tThe length of the password (default: 32)" << std::endl
|
<< "\t-l, --length [n] \t\tThe length of the password (default: 32)" << std::endl
|
||||||
<< "\t-p, --punctuation \t\tToggle special characters (default: false)"
|
<< "\t-p, --punctuation \t\tToggle special characters (default: false)"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
std::string passlenstr;
|
std::string passlenstr;
|
||||||
int passlen;
|
int passlen = 0;
|
||||||
bool punc = false;
|
bool punc = false;
|
||||||
|
|
||||||
if (argc < 1) {
|
if (argc < 1) {
|
||||||
@ -48,18 +54,24 @@ int main(int argc, char *argv[]) {
|
|||||||
} else if (arg == "-p") {
|
} else if (arg == "-p") {
|
||||||
punc = true;
|
punc = true;
|
||||||
} else if ((arg == "-l") || (arg == "--length")) {
|
} else if ((arg == "-l") || (arg == "--length")) {
|
||||||
passlenstr = argv[i + 1];
|
if (not argv[i +1]) {
|
||||||
if (passlenstr.empty()) {
|
std::cerr << "Specify a password length" << std::endl;
|
||||||
std::cerr << "--destination option requires one argument." << std::endl;
|
|
||||||
return 1;
|
return 1;
|
||||||
|
} else {
|
||||||
|
passlenstr = argv[i + 1];
|
||||||
|
try {
|
||||||
|
passlen = std::stoi(passlenstr);
|
||||||
|
} catch (const std::invalid_argument& e){
|
||||||
|
std::cerr << "Length must be a valid integer" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (passlenstr.empty()) {
|
if (passlen == 0) {
|
||||||
passlenstr = "32";
|
passlen = 32;
|
||||||
}
|
}
|
||||||
passlen = std::stoi(passlenstr);
|
|
||||||
std::string password = random_string(passlen, punc);
|
std::string password = random_string(passlen, punc);
|
||||||
std::cout << password << std::endl;
|
std::cout << password << std::endl;
|
||||||
return 0;
|
return 0;
|
Loading…
Reference in New Issue
Block a user