argeparse

This commit is contained in:
2025-01-18 14:10:25 +00:00
parent 5274c8a839
commit f41aa74de0
3 changed files with 609 additions and 103 deletions

View File

@@ -1,8 +1,8 @@
#include <iostream>
#include <regex>
#include <string>
#include <fstream>
#include <random>
#include <argparse.hpp>
using namespace std;
string replacements[6][2] = {
@@ -87,111 +87,24 @@ string owo(string input, bool no_full_word = false, bool no_random_ending = fals
return output;
}
int help() {
cout << "Usage: owo [-p] [-h] [-f file] [-o file] [text]" << endl;
cout << " owo [text]" << endl;
cout << " owo -f [file] -o [file]" << endl;
cout << " echo [text] | owo -p" << endl << endl;
cout << "Options:" << endl;
cout << " -p Read from stdin" << endl;
cout << " -h Show this help message" << endl;
cout << " -f Read from file" << endl;
cout << " -o Write to file" << endl;
cout << " -ne Stops extra stuff from being added to the end of sentences" << endl;
cout << " -nf Stops full word replacements" << endl;
cout << " text Text to convert" << endl;
return 0;
}
struct Args : public argparse::Args {
bool &no_full_word = flag("nfw", "Don't replace full words");
bool &no_random_ending = flag("nre", "Don't add random ending");
bool &_stdin = flag("p", "Read from stdin");
string &input = arg("input", "Input string").set_default("");
};
int main(int argc, char *argv[]) {
auto args = argparse::parse<Args>(argc, argv);
srand(time(nullptr));
string input, inputfile, outputfile;
bool _stdin = false, clipboard = false, no_full_word = false, no_random_ending = false;
if (argc < 2) {
cout << owo("No text provided") << endl << endl;
help();
return 1;
}
for (int i = 1; i < argc; i++) {
if (string(argv[i]) == "-f") {
inputfile = string(argv[i+1]);
i++;
} else if (string(argv[i]) == "-o") {
outputfile = string(argv[i+1]);
i++;
} else if (string(argv[i]) == "-p") {
_stdin = true;
} else if (string(argv[i]) == "-h") {
return help();
} else if (i == argc-1) {
input = string(argv[i]);
} else if (string(argv[i]) == "-ne") {
no_random_ending = true;
} else if (string(argv[i]) == "-nf") {
no_full_word = true;
} else {
cout << owo("Invalid argument: " + string(argv[i])) << endl << endl;
help();
return 1;
if (args._stdin) {
string line;
while (getline(cin, line)) {
cout << owo(line, args.no_full_word, args.no_random_ending) << endl;
}
}
if (_stdin && inputfile != "" || _stdin && input != "" || inputfile != "" && input != "") {
cout << owo("Cannot use multiple input methods at once") << endl << endl;
help();
return 1;
}
ostream *output;
if (outputfile != "") {
output = new ofstream(outputfile);
if (!output->good()) {
cout << owo("Failed to open output file") << endl;
return 1;
}
}
if (_stdin) {
while (getline(cin, input)) {
if (outputfile != "") {
*output << owo(input, no_full_word, no_random_ending) << endl;
} else {
cout << owo(input, no_full_word, no_random_ending) << endl;
}
}
} else if (inputfile != "") {
ifstream inputstream(inputfile);
if (!inputstream.good()) {
cout << owo("Failed to open input file") << endl;
if (outputfile != "") {
((ofstream*)output)->close();
}
return 1;
}
while (getline(inputstream, input)) {
if (outputfile != "") {
*output << owo(input, no_full_word, no_random_ending) << endl;
} else {
cout << owo(input, no_full_word, no_random_ending) << endl;
}
}
inputstream.close();
} else if (input != "") {
if (outputfile != "") {
*output << owo(input, no_full_word, no_random_ending) << endl;
} else {
cout << owo(input, no_full_word, no_random_ending) << endl;
}
}
if (outputfile != "") {
((ofstream*)output)->close();
} else {
cout << owo(args.input, args.no_full_word, args.no_random_ending) << endl;
}
return 0;