From bba2aac5a70a14887b12152c62eb307da2e79337 Mon Sep 17 00:00:00 2001 From: ivan-volnov Date: Sun, 14 Jun 2020 15:10:45 +0800 Subject: [PATCH] command line validation and help message --- src/main.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c6ec063..476bc26 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,16 +9,22 @@ void run(int argc, char *argv[]) { argh::parser cmdl(argc, argv, argh::parser::SINGLE_DASH_IS_MULTIFLAG); - if (cmdl[{"h", "help"}]) { - std::cout << "Usage: " << std::filesystem::path(argv[0]).filename().string() << " [options]\n\n" - "Optional arguments:\n" - "-h --help show this help message and exit\n" - "-S --stats show stats and exit\n" - "-s --sound read aloud the current phrase while typing\n" - " --import import cards from anki\n" - " --clear_removed remove cards that have been removed from anki deck\n" - << std::flush; - return; + { + auto flags = cmdl.flags(); + for (const char *flag : {"S", "stats", "s", "sound", "import", "clear_removed"}) { + flags.erase(flag); + } + if (!flags.empty() || !cmdl.params().empty() || cmdl.pos_args().size() > 1) { + std::cout << "Usage: " << std::filesystem::path(argv[0]).filename().string() << " [options]\n\n" + "Optional arguments:\n" + "-h --help show this help message and exit\n" + "-S --stats show stats and exit\n" + "-s --sound read aloud the current phrase while typing\n" + " --import import cards from anki\n" + " --clear_removed remove cards that have been removed from anki deck\n" + << std::flush; + return; + } } Config::instance().set_sound_enabled(cmdl[{"s", "sound"}]); auto trainer = std::make_shared();