From 1cbaea9574d7d03c5fa33809fcdc87ceb38ea399 Mon Sep 17 00:00:00 2001 From: dubo-dubon-duponey Date: Sat, 1 Dec 2018 22:00:15 -0800 Subject: [PATCH] Work around gnu grep attempt #1 Signed-off-by: dubo-dubon-duponey --- source/core/abash.sh | 8 ++++++++ source/core/commander.sh | 13 +++++++++++-- tests/unit/arg.sh | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/unit/arg.sh diff --git a/source/core/abash.sh b/source/core/abash.sh index bcaf9a1..0d480d8 100644 --- a/source/core/abash.sh +++ b/source/core/abash.sh @@ -17,3 +17,11 @@ _have_bash(){ } _have_bash + +_gnu_grep(){ + if grep --version | grep -q gnu; then + readonly _GNUGREP="true" + fi +} + +_gnu_grep diff --git a/source/core/commander.sh b/source/core/commander.sh index edaded8..1912752 100644 --- a/source/core/commander.sh +++ b/source/core/commander.sh @@ -72,7 +72,11 @@ dc::commander::declare::arg(){ local optional="$3" local fancy="$4" local description="$5" - local gf="${6:--Ei}" + if [ "$_GNUGREP" ]; then + gf="${6:--Pi}" + else + gf="${6:--Ei}" + fi local var="DC_PARGV_$number" local varexist="DC_PARGE_$number" @@ -122,7 +126,12 @@ dc::commander::declare::flag(){ local optional="$3" local description="$4" local alias="$5" - local gf="${6:--Ei}" + local gf + if [ "$_GNUGREP" ]; then + gf="${6:--Pi}" + else + gf="${6:--Ei}" + fi local display="--$name" local long="--$name" diff --git a/tests/unit/arg.sh b/tests/unit/arg.sh new file mode 100644 index 0000000..7927756 --- /dev/null +++ b/tests/unit/arg.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +testArgGrep() { + DC_PARGV_1=foobar + DC_PARGE_1=true + local result + result="$(dc::commander::declare::arg 1 "^foo(?:bar)?$" "" "a flag" "a flag")" + local exit=$? + dc-tools::assert::equal "$exit" "0" + + result="$(dc::commander::declare::arg 1 "^foo(?:abar)?$" "" "another flag" "another flag")" + local exit=$? + dc-tools::assert::equal "$exit" "$ERROR_ARGUMENT_INVALID" +}