Options: parse input path

This commit is contained in:
rr- 2016-03-16 13:02:21 +01:00
parent 7f7766f175
commit 8f5a6bb534
4 changed files with 22 additions and 4 deletions

View File

@ -7,8 +7,8 @@ static void show_usage(const bool include_help)
{
static const auto usage =
"Usage: opustags --help\n"
" opustags [OPTIONS] FILE\n"
" opustags OPTIONS FILE -o FILE\n";
" opustags [OPTIONS] INPUT\n"
" opustags [OPTIONS] -o OUTPUT INPUT\n";
static const auto help =
"Options:\n"
@ -45,6 +45,7 @@ int main(int argc, char **argv)
}
std::cout << "Working...\n";
std::cout << "Input path: " << options.path_in << "\n";
} catch (const std::exception &e) {
std::cerr << e.what();
return EXIT_FAILURE;

View File

@ -120,5 +120,13 @@ Options opustags::parse_args(const int argc, char **argv)
}
}
if (optind == argc || argv[optind] == NULL) {
throw ArgumentError("Missing input path");
}
options.path_in = argv[optind];
if (options.path_in.empty())
throw ArgumentError("Input path cannot be empty");
return options;
}

View File

@ -16,6 +16,7 @@ namespace opustags
bool set_all;
bool in_place;
std::string path_in;
std::string path_out;
CompositeTagsHandler tags_handler;

View File

@ -16,13 +16,16 @@ static std::unique_ptr<char[]> string_to_uptr(const std::string &str)
return ret;
}
static Options retrieve_options(std::vector<std::string> args)
static Options retrieve_options(
std::vector<std::string> args, bool fake_input_path = true)
{
// need to pass non-const char*, but we got const objects. make copies
std::vector<std::unique_ptr<char[]>> arg_holders;
arg_holders.push_back(string_to_uptr("fake/path/to/program"));
for (size_t i = 0; i < args.size(); i++)
arg_holders.push_back(string_to_uptr(args[i]));
if (fake_input_path)
arg_holders.push_back(string_to_uptr("fake/path/to/input"));
auto plain_args = std::make_unique<char*[]>(arg_holders.size());
for (size_t i = 0; i < arg_holders.size(); i++)
@ -78,10 +81,15 @@ TEST_CASE("option parsing", "[options]")
REQUIRE(retrieve_options({"-iABC"}).path_out == "ABC");
}
SECTION("input") {
REQUIRE_THROWS(retrieve_options({}, false));
REQUIRE_THROWS(retrieve_options({""}, false));
REQUIRE(retrieve_options({"input"}, false).path_in == "input");
}
SECTION("--output") {
REQUIRE(retrieve_options({"--output", "ABC"}).path_out == "ABC");
REQUIRE(retrieve_options({"-o", "ABC"}).path_out == "ABC");
REQUIRE_THROWS(retrieve_options({"--delete", "invalid="}));
}
SECTION("--delete-all") {