shflags icon indicating copy to clipboard operation
shflags copied to clipboard

Unexpected behavior with default true booleans

Open theanine opened this issue 6 years ago • 1 comments

Test program here:

FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
if [ ${FLAGS_foo} -eq ${FLAGS_TRUE} ]; then
	echo "true"
else
	echo "false"
fi

With:

DEFINE_boolean foo false "bar" "f"

Output (as expected):

$ foo-test.sh
false
$ foo-test.sh -f
true
$ foo-test.sh --nof
false
$ foo-test.sh --foo
true
$ foo-test.sh --nofoo
false

With:

DEFINE_boolean foo true "bar" "f"

Output (unexpected):

$ foo-test.sh
true
$ foo-test.sh -f
false
$ foo-test.sh --nof
false
$ foo-test.sh --foo
true
$ foo-test.sh --nofoo
false

If it's not obvious from the above output, the part that is unexpected is the output of foo-test.sh -f

theanine avatar Oct 01 '19 22:10 theanine

I agree, that is confusing.

I propose fixing this by enforcing the convention that single-character booleans, when passed, will always give a true value. Unfortunately, there is no good way to negate them, but this can be handled in code by following the convention that boolean values should always default to false, requiring them to be passed as flags to enable them.

Thoughts?

kward avatar Apr 13 '20 21:04 kward