From d2ae8ab94c830bbaf31ca19ded4c880dac8016b9 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 1 Jun 2019 21:37:58 +0200 Subject: [PATCH] Path doesn't require '-p' to be specified, possible to specify file instead of directory --- functions.cpp | 88 +++++++++++++++++++++++++-------------------------- main.cpp | 47 +++++++++++++++++++++------ 2 files changed, 81 insertions(+), 54 deletions(-) diff --git a/functions.cpp b/functions.cpp index e2e3975..b4753fa 100644 --- a/functions.cpp +++ b/functions.cpp @@ -128,7 +128,7 @@ bool searchSpecificSeason( const char_t *const path, size_t &ep_pos, cur_pos++; while ( path[cur_pos] == '0' ) cur_pos++; - // if season number is 0, move back because previous while skipped it + // if season number is 0, move back because previous loop skipped it if( number == TEXT("0") ) cur_pos--; // make sure season's number is the same as provided in argument @@ -283,60 +283,58 @@ string getDefUrl( string &show, const string &language, Curl &c ) { } void printHelp() { - cout << "usage: tv_rename [--help] [--show show name] [--season season " - "number]" + cout << "Usage:" << std::endl; + cout << " tv_rename [options] [path]" << std::endl << std::endl; + cout << " -h, --help show this help message and exit" + << std::endl << std::endl; + cout << " path can be either a file or a directory, if it's a directory" << std::endl; - cout - << " [--correct-path] [--show-path show path] [--trust]" - << std::endl; - cout << " [--linux] [--lang language] [--print-langs]" + cout << " all files in it and its subdirectories will be renamed" + << std::endl << std::endl; + cout << "OPTIONS" << std::endl; + cout << " -s, --show TV show from which you want the"; + cout << " episode names" << std::endl; + cout << " -n, --season Season number/s (if multiple seasons," << std::endl; - cout << std::endl - << "Rename TV episodes" << std::endl - << std::endl - << "optional arguments:" << std::endl; - cout << " -h, --help\t\tshow this help message and exit" << std::endl; - cout << " --show show name, -s show name" << std::endl; - cout << "\t\t\tTV show from which you want episode names (needs to be" + cout << " must be seperated by one space)" + << " or 'all'" << std::endl; + cout << " for all seasons in selected directory" << std::endl; - cout << "\t\t\tin quotation marks if it has more than one word)" + cout << " --name-pattern Pattern to which change the file name." << std::endl; - cout << " --season season number, -n season number" << std::endl; - cout << "\t\t\tSeason number/s (if multiple seasons, put them in" + cout << " Possible sequences are:" << std::endl; + cout << " %filename - original filename" << std::endl; - cout << "\t\t\tquotation marks and seperate by one space)" << std::endl; - cout << "\t\t\tor 'all' for all seasons in selected subdirectory" + cout << " (without filetype" + << " extension)" << std::endl; + cout << " %show - show name from thetvdb" << std::endl; - cout << " --show-path show path, -p show path" << std::endl; - cout << "\t\t\tPath of the directory with episodes" << std::endl; - cout << " --correct-path, -c\tThis is the correct path, stop asking me!" + cout << " %epname - episode name from thetvdb" << std::endl; - cout << " --name-pattern pattern" << std::endl; - cout << "\t\t\tPattern to which change the file name. Possible sequences " - "are:" + cout << " %season - season number" << std::endl; - cout << "\t\t\t\t%filename - original filename (without filetype extension)" + cout << " it's possible to specify leading" + << " 0 like this:" << std::endl; + cout << " %2season (number means how many" + << " zeros)" << std::endl; + cout << " %episode - episode number" << std::endl; - cout << "\t\t\t\t%show - show name from thetvdb" << std::endl; - cout << "\t\t\t\t%epname - episode name from thetvdb" << std::endl; - cout << "\t\t\t\t%season - season number" << std::endl; - cout << "\t\t\t\ttpossible to specify leading 0 like this: %2season " - "(number means how many leading zeros)" + cout << " it's possible to specify leading" + << " 0 like this:" << std::endl; + cout << " %2episode (number means how many" + << " zeros)" << std::endl; + cout << " Default pattern is \"$filename -" + << " $epname\"" << std::endl; + cout << " -c, --correct-path This is the correct path," + << " stop asking me!" << std::endl; + cout << " -t, --trust Don't ask whether the names are correct" << std::endl; - cout << "\t\t\t\t%episode - episode number" << std::endl; - cout << "\t\t\t\t\tpossible to specify leading 0 like this: %2episode " - "(number means how many leading zeros)" - << std::endl; - cout << "\t\t\tDefault pattern is \"$filename - $epname\"" << std::endl; - cout << " --trust, -t\t\tDon't ask whether the names are correct" - << std::endl; - cout << " --linux, -x\t\tDon't replace characters characters that are " - "illegal in Windows" - << std::endl; - cout << " --lang language, -l language" << std::endl; - cout << "\t\t\tSelect which language the episode names shoud be in" - << std::endl; - cout << " --print-langs\t\tPring available language" << std::endl; + cout << " -x, --linux Don't replace characters characters" + << " that are" << std::endl; + cout << " illegal in Windows" << std::endl; + cout << " -l, --lang Select which language the episode" + << " names shoud be in" << std::endl; + cout << " --print-langs Pring available languages" << std::endl; } // parse command line argument --seasons (e.g. '1 2 3 4 5') diff --git a/main.cpp b/main.cpp index 899cde8..0bc9792 100644 --- a/main.cpp +++ b/main.cpp @@ -96,16 +96,16 @@ int handleArgument( char_t c, string &show, std::set< int > &seasons_num, #ifdef _WIN32 string getOptions( const char_t *option ) { - if ( option[1] != '-' ) + if ( option[0] != '-' ) + return L""; + else if ( option[1] != '-' ) return option + 1; - if ( !wcscmp( option, L"--show" ) ) + else if ( !wcscmp( option, L"--show" ) ) return L"s"; else if ( !wcscmp( option, L"--season" ) ) return L"n"; else if ( !wcscmp( option, L"--correct-path" ) ) return L"c"; - else if ( !wcscmp( option, L"--show-path" ) ) - return L"p"; else if ( !wcscmp( option, L"--trust" ) ) return L"t"; else if ( !wcscmp( option, L"--linux" ) ) @@ -126,9 +126,16 @@ int parseCommandLine( string &show, std::set< int > &seasons_num, string &path, bool &change_dir, string &language, string &pattern, bool &linux, bool &trust, const int argc, char_t **argv ) { + string options{}; + char_t *optional; for ( auto i = 1; i < argc; i++ ) { - auto options = getOptions( argv[i] ); - char_t *optional = ( i < argc - 1 ) ? argv[i + 1] : nullptr; + options = getOptions( argv[i] ); + if ( options == L"" ) { + options = L"p"; + optional = argv[i]; + } else { + optional = ( i < argc - 1 ) ? argv[i + 1] : nullptr; + } for ( const auto &x : options ) { auto res = handleArgument( x, show, seasons_num, change_dir, path, trust, @@ -150,13 +157,13 @@ int parseCommandLine( string &show, std::set< int > &seasons_num, string &path, { "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' }, { "name-pattern", required_argument, 0, '1' }, - { "help", no_argument, 0, 'h' } + { "help", no_argument, 0, 'h' }, + { 0, 0, 0, 0} }; int i{}; // this is useless, but needed for handleArgument @@ -173,6 +180,12 @@ int parseCommandLine( string &show, std::set< int > &seasons_num, string &path, return res; } + if ( optind < argc ) { + path = string( argv[optind] ); + // if path provided, assume it's correct + change_dir = false; + } + return 0; } @@ -209,8 +222,24 @@ int main return 0; } - if ( !FSLib::isDirectory( path ) ) + if ( !FSLib::isDirectory( path ) && FSLib::exists( path ) ) { + // specified file, not directory + auto *file_set = new std::set; + file_set->insert( path ); + size_t season_pos{}; + if ( !searchSeason( path.c_str(), season_pos ) ) { + cerr << "Specified file does not conform to filename pattern, " + << "cannot rename." << std::endl; + return 1; + } else { + auto season = std::stoi( path.c_str() + season_pos ); + singleSeason( path, show, season, TEXT(""), language, pattern, + linux, trust, c, file_set ); + return 0; + } + } else if ( !FSLib::isDirectory( path ) ) { change_dir = true; + } while ( change_dir ) { if ( !FSLib::isDirectory( path ) ) {