diff --git a/src/main.cc b/src/main.cc index 59afda4..d826029 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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; diff --git a/src/options.cc b/src/options.cc index 9d343b2..77278c6 100644 --- a/src/options.cc +++ b/src/options.cc @@ -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; } diff --git a/src/options.h b/src/options.h index 083b0ed..6064a52 100644 --- a/src/options.h +++ b/src/options.h @@ -16,6 +16,7 @@ namespace opustags bool set_all; bool in_place; + std::string path_in; std::string path_out; CompositeTagsHandler tags_handler; diff --git a/tests/options_test.cc b/tests/options_test.cc index a008d77..9aa39e9 100644 --- a/tests/options_test.cc +++ b/tests/options_test.cc @@ -16,13 +16,16 @@ static std::unique_ptr string_to_uptr(const std::string &str) return ret; } -static Options retrieve_options(std::vector args) +static Options retrieve_options( + std::vector args, bool fake_input_path = true) { // need to pass non-const char*, but we got const objects. make copies std::vector> 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(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") {