Use getopt_long for command line parsing, fix seasons parsing (when only a-zA-Z were present it behaved unpredictably), spaces instead of tabs
This commit is contained in:
parent
e45615b6c0
commit
c014a059b6
143
tv_rename.cpp
143
tv_rename.cpp
@ -6,6 +6,7 @@
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <getopt.h>
|
||||
#include "filesystem.hpp"
|
||||
|
||||
void singleSeason( const std::string &path, const std::string &show, int season, std::string url );
|
||||
@ -15,64 +16,105 @@ void allSeasons( const std::string &path, const std::string &show );
|
||||
std::string language{"en"};
|
||||
bool trust{false};
|
||||
bool linux{false};
|
||||
Curl c;
|
||||
Curl c; // global so the connection doesn't close at every function
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::map<std::string, std::string> languages{
|
||||
std::map<std::string, std::string> languages{
|
||||
{"en", "English"}, {"sv", "Svenska"}, {"no", "Norsk"}, {"da", "Dansk"}, {"fi", "Suomeksi"},
|
||||
{"nl", "Nederlands"}, {"de", "Deutsch"}, {"it", "Italiano"}, {"es", "Español"}, {"fr", "Français"},
|
||||
{"pl", "Polski"}, {"hu", "Magyar"}, {"el", "Greek"}, {"tr", "Turkish"}, {"ru", "Russian"},
|
||||
{"he", "Hebrew"}, {"ja", "Japanese"}, {"pt", "Portuguese"}, {"zh", "Chinese"}, {"cs", "Czech"},
|
||||
{"sl", "Slovenian"}, {"hr", "Croatian"}, {"ko","Korea"}
|
||||
};
|
||||
|
||||
void parseSeasonNumbers(std::set<int> &seasons_num, const char *argument) {
|
||||
size_t pos{0};
|
||||
|
||||
while((argument[pos] < '0' || argument[pos] > '9') && argument[pos] != ' ' && argument[pos] != '\0')
|
||||
pos++;
|
||||
|
||||
if( argument[pos] == '\0' ) {
|
||||
seasons_num.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
int temp;
|
||||
std::istringstream iss(&optarg[pos]);
|
||||
while(iss >> temp) {
|
||||
seasons_num.insert(temp);
|
||||
}
|
||||
}
|
||||
|
||||
bool parseCommandLine(std::string &show, std::set<int> &seasons_num, std::string &path, bool &change_dir, int argc, char **argv) {
|
||||
static struct option long_options[] = {
|
||||
{"show", required_argument, 0, 's' },
|
||||
{"season", required_argument, 0, 'n' },
|
||||
{"correct-path", no_argument, 0, 'c' },
|
||||
{"show-path", required_argument, 0, 'p' },
|
||||
{"trust", no_argument, 0, 't' },
|
||||
{"linux", no_argument, 0, 'x' },
|
||||
{"lang", required_argument, 0, 'l' },
|
||||
{"print-langs", no_argument, 0, 0 },
|
||||
{"help", no_argument, 0, 'h' },
|
||||
{0, 0, 0, 0 }
|
||||
};
|
||||
|
||||
while(1) {
|
||||
int option_index{0};
|
||||
auto c = getopt_long(argc, argv, "s:n:cp:txl:0h", long_options, &option_index);
|
||||
if( c == -1 )
|
||||
break;
|
||||
switch(c) {
|
||||
case 's':
|
||||
show = optarg;
|
||||
break;
|
||||
case 'n':
|
||||
parseSeasonNumbers(seasons_num, optarg);
|
||||
break;
|
||||
case 'c':
|
||||
change_dir = false;
|
||||
break;
|
||||
case 'p':
|
||||
path = std::string(optarg);
|
||||
if( !FSLib::isDirectory(path) )
|
||||
change_dir = true;
|
||||
break;
|
||||
case 't':
|
||||
trust = true;
|
||||
break;
|
||||
case 'x':
|
||||
linux = true;
|
||||
break;
|
||||
case 'l':
|
||||
if( languages.find(optarg) != languages.end() ) {
|
||||
language = optarg;
|
||||
} else {
|
||||
std::cerr << "Invalid language choice" << std::endl;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case '0':
|
||||
for( const auto &x : languages ) {
|
||||
std::cout << x.first << " - " << x.second << std::endl;
|
||||
}
|
||||
return true;
|
||||
case 'h':
|
||||
printHelp();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::string show{};
|
||||
std::string seasons{""};
|
||||
std::set<int> seasons_num;
|
||||
std::string path{"."};
|
||||
bool change_dir{true};
|
||||
|
||||
int x = 1;
|
||||
while ( x < argc ) {
|
||||
if( !(strcmp("-s", argv[x]) && strcmp("--show", argv[x])) ) {
|
||||
show = argv[x+1];
|
||||
x++;
|
||||
} else if ( !(strcmp("-n", argv[x]) && strcmp("--season", argv[x])) ) {
|
||||
size_t pos{0};
|
||||
while((argv[x+1][pos] < '0' || argv[x+1][pos] > '9') && argv[x+1][pos] != ' ')
|
||||
pos++;
|
||||
seasons = std::string(&argv[x+1][pos]);
|
||||
x++;
|
||||
} else if ( !(strcmp("-c", argv[x]) && strcmp("--correct-path", argv[x])) ) {
|
||||
change_dir = false;
|
||||
} else if ( !(strcmp("-sp", argv[x]) && strcmp("--show-path", argv[x])) ) {
|
||||
path = std::string(argv[x+1]);
|
||||
x++;
|
||||
if( !FSLib::isDirectory(path) )
|
||||
change_dir = true;
|
||||
} else if ( !(strcmp("-t", argv[x]) && strcmp("--trust", argv[x])) ) {
|
||||
trust = true;
|
||||
} else if ( !(strcmp("-x", argv[x]) && strcmp("--linux", argv[x])) ) {
|
||||
linux = true;
|
||||
} else if ( !(strcmp("-l", argv[x]) && strcmp("--lang", argv[x])) ) {
|
||||
if( languages.find(argv[x+1]) != languages.end() ) {
|
||||
language = argv[x+1];
|
||||
} else {
|
||||
std::cerr << "Invalid language choice" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
x++;
|
||||
} else if ( !(strcmp("-p", argv[x]) && strcmp("--print-langs", argv[x])) ) {
|
||||
for( const auto &x : languages ) {
|
||||
std::cout << x.first << " - " << x.second << std::endl;
|
||||
}
|
||||
return 0;
|
||||
} else if ( !(strcmp("-h", argv[x]) && strcmp("--help", argv[x])) ) {
|
||||
printHelp();
|
||||
return 0;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
parseCommandLine(show, seasons_num, path, change_dir, argc, argv);
|
||||
|
||||
while( change_dir ) {
|
||||
if( !FSLib::isDirectory(path) ) {
|
||||
@ -108,17 +150,10 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
|
||||
if( !seasons.empty() ) {
|
||||
int temp;
|
||||
std::istringstream iss(seasons);
|
||||
while(iss >> temp) {
|
||||
seasons_num.insert(temp);
|
||||
}
|
||||
if( seasons_num.size() == 1 ) {
|
||||
singleSeason(path, show, temp, "");
|
||||
} else {
|
||||
singleSeason(path, show, *seasons_num.begin(), "");
|
||||
} else if ( seasons_num.size() != 0 ) {
|
||||
multipleSeasons(path, show, seasons_num);
|
||||
}
|
||||
} else {
|
||||
allSeasons(path, show);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user