Merge pull request #57 from alcah/master

Return exit code 2 if search term not found
This commit is contained in:
Evgeniy Dushistov
2020-03-17 17:09:42 +03:00
committed by GitHub
4 changed files with 41 additions and 9 deletions

View File

@@ -316,10 +316,10 @@ private:
};
}
bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
search_result Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
{
if (nullptr == loc_str)
return true;
return SEARCH_SUCCESS;
std::string query;
@@ -330,6 +330,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
gsize bytes_read;
gsize bytes_written;
glib::Error err;
search_result rval = SEARCH_SUCCESS;
glib::CharStr str;
if (!utf8_input_)
str.reset(g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, get_addr(err)));
@@ -339,11 +340,11 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
if (nullptr == get_impl(str)) {
fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str);
fprintf(stderr, "%s\n", err->message);
return false;
return SEARCH_FAILURE;
}
if (str[0] == '\0')
return true;
return SEARCH_SUCCESS;
TSearchResultList res_list;
@@ -443,10 +444,11 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
loc_str = utf8_to_locale_ign_err(get_impl(str));
if (!json_)
printf(_("Nothing similar to %s, sorry :(\n"), utf8_output_ ? get_impl(str) : loc_str.c_str());
rval = SEARCH_NO_RESULT;
}
if (json_) {
fputs("]\n", stdout);
}
return true;
return rval;
}

View File

@@ -23,6 +23,13 @@ struct TSearchResult {
typedef std::vector<TSearchResult> TSearchResultList;
//possible return values for Library.process_phase()
enum search_result {
SEARCH_SUCCESS = 0,
SEARCH_FAILURE,
SEARCH_NO_RESULT
};
//this class is wrapper around Dicts class for easy use
//of it
class Library : public Libs
@@ -38,7 +45,7 @@ public:
setFuzzy(!no_fuzzy);
}
bool process_phrase(const char *loc_str, IReadLine &io, bool force = false);
search_result process_phrase(const char *loc_str, IReadLine &io, bool force = false);
private:
bool utf8_input_;

View File

@@ -207,15 +207,16 @@ int main(int argc, char *argv[]) try {
std::unique_ptr<IReadLine> io(create_readline_object());
if (optind < argc) {
search_result rval = SEARCH_SUCCESS;
for (int i = optind; i < argc; ++i)
if (!lib.process_phrase(argv[i], *io, non_interactive)) {
return EXIT_FAILURE;
if ((rval = lib.process_phrase(argv[i], *io, non_interactive)) != SEARCH_SUCCESS) {
return rval;
}
} else if (!non_interactive) {
std::string phrase;
while (io->read(_("Enter word or phrase: "), phrase)) {
if (!lib.process_phrase(phrase.c_str(), *io))
if (lib.process_phrase(phrase.c_str(), *io) == SEARCH_FAILURE)
return EXIT_FAILURE;
phrase.clear();
}

22
tests/t_return_code Executable file
View File

@@ -0,0 +1,22 @@
#!/bin/sh
SDCV="$1"
TEST_DIR="$2"
unset SDCV_PAGER
test_return_code() {
WORD=$1
EXPECTED=$2
$SDCV -e -n --data-dir "$TEST_DIR" -u "Test synonyms" $WORD > /dev/null
RC=$?
if [ $RC -ne $EXPECTED ]; then
echo "Return code for $WORD should be '$EXPECTED' but was '$RC'"
exit 1
fi
}
test_return_code testawordy 0
test_return_code testawordies 2
exit 0