1
0
Fork 0
mirror of https://gitlab.com/niansa/commsyfuse.git synced 2025-03-06 20:48:31 +01:00

Implemement better CLI input parsing

This commit is contained in:
niansa 2020-04-29 15:59:05 +02:00
parent 950fdad2ad
commit c4d3de6c03

View file

@ -1,4 +1,21 @@
#include <iostream>
/*
This file is part of CommSyFuse.
pilang is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pilang is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pilang. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
@ -22,6 +39,7 @@ static std::string server_sid;
static std::string room;
void panic(std::string message) {
std::cerr << std::endl << "An unhandled exception occured:" << std::endl << message << std::endl;
abort();
@ -285,8 +303,16 @@ std::string get_rss_url(GumboNode *node) { // Currently unused
int main(int argc, char *argv[]) {
// Show license note if --license was given as first argument
if (argc > 1 and !strncmp(argv[1], "--license", 9)) {
std::cout << "CommSyFuse Copyright (C) 2020 niansa" << std::endl;
std::cout << "This program comes with ABSOLUTELY NO WARRANTY; for details type `warranty'." << std::endl;
std::cout << "This is free software, and you are welcome to redistribute it" << std::endl;
std::cout << "under certain conditions; type `license' for details." << std::endl;
return 0;
}
// Check arguments
if (argc < 2) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <SID> <room>" << std::endl;
return 1;
}
@ -345,13 +371,26 @@ int main(int argc, char *argv[]) {
std::cout << numposts << ") " << postsmap_numname[numposts] << " " << postsmap_nummeta[numposts] << std::endl;
numposts++;
}
std::cout << "r) Refresh" << std::endl;
// Debug CLI
std::string userinstr;
unsigned long userin;
while (true) {
// Await user input
std::cout << "? " << std::flush;
std::cin >> userin;
// Check if input is valid
std::getline (std::cin,userinstr);
// Check string input options first5
if (userinstr == "r") {
execve(argv[0], argv, nullptr);
}
// Try to convert input to unsigned long
try {
userin = std::stoul(userinstr,nullptr,0);
} catch (const std::invalid_argument &) {
std::cerr << "Invalid input" << std::endl;
continue;
}
// Check if givenn number is valid
if (userin >= numposts) {
std::cerr << "No such post" << std::endl;
continue;