From 1c3de588bf43b938a5e3eec38be6fad097729501 Mon Sep 17 00:00:00 2001 From: Andy Chu Date: Thu, 7 Jan 2021 22:01:02 -0800 Subject: [PATCH] [oil-language] Implement overlooked != operator Caught by Danilo Spinella. - Add test cases from the other examples. - Language FAQ: Explain limited proc / shell function return values. Thanks for the feedback! --- doc/oil-language-faq.md | 16 ++++++++++ oil_lang/expr_eval.py | 3 ++ spec/oil-user-feedback.test.sh | 55 ++++++++++++++++++++++++++++++++++ test/spec.sh | 5 ++++ 4 files changed, 79 insertions(+) create mode 100644 spec/oil-user-feedback.test.sh diff --git a/doc/oil-language-faq.md b/doc/oil-language-faq.md index 94a5b497a7..fe159a60d0 100644 --- a/doc/oil-language-faq.md +++ b/doc/oil-language-faq.md @@ -16,6 +16,7 @@ Old and new constructs exist side-by-side. New constructs have fewer
+ ## What's the difference between `$(dirname $x)` and `$len(x)` ? Superficially, both of these syntaxes take an argument `x` and return a @@ -29,6 +30,21 @@ string. But they are different: (Note: command subs may be optimized later, as `ksh` does.) +## How can I return rich values from shell functions / Oil `proc`s? + +There are two primary ways: + +- Print the "returned" data to `stdout`. Retrieve it with a command sub like + `$(myproc)` or a pipeline like `myproc | read --line`. +- Use an "out param" with [setref]($oil-help:setref). + +Oil may grow true functions with the `func` keyword at some point. However, +that must be done carefully, as a `proc` composes with processes, but a `func` +doesn't. + +Send us feedback if this doesn't make sense, or if you want a longer +explanation. + ## Why doesn't a raw string work here: `${array[r'\']}` ? Oil has two array index syntax: diff --git a/oil_lang/expr_eval.py b/oil_lang/expr_eval.py index 4a2de121b1..684b5df51f 100644 --- a/oil_lang/expr_eval.py +++ b/oil_lang/expr_eval.py @@ -350,8 +350,11 @@ def EvalExpr(self, node): result = left >= right elif op.id == Id.Arith_LessEqual: result = left <= right + elif op.id == Id.Arith_DEqual: result = left == right + elif op.id == Id.Arith_NEqual: + result = left != right elif op.id == Id.Expr_In: result = left in right diff --git a/spec/oil-user-feedback.test.sh b/spec/oil-user-feedback.test.sh new file mode 100644 index 0000000000..6756f0bd4a --- /dev/null +++ b/spec/oil-user-feedback.test.sh @@ -0,0 +1,55 @@ +# Oil user feedback + +# From Zulip: +# +# https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/Experience.20using.20oil + +#### setvar + +# This seems to work as expected? + +proc get_opt(arg, :out) { + setvar out = $arg +} +var a = '' +get_opt a 'lol' +echo hi +## status: 1 +## STDOUT: +## END + +#### != operator +var a = 'bar' + +# NOTE: a != foo is idiomatic) +if ($a != 'foo') { + echo 'not equal' +} + +if ($a != 'bar') { + echo 'should not get here' +} + +## STDOUT: +not equal +## END + + +#### Regex Literal + +# This seems to work as expected? + +proc get_opt(arg, :out) { + setref out = $(write -- $arg | cut -d'=' -f2) +} + +var variant = '' +var arg = '--variant=foo' +if ( arg ~ / '--variant=' / ) { + get_opt $arg :variant + echo variant=$variant +} + +## STDOUT: +variant=foo +## END diff --git a/test/spec.sh b/test/spec.sh index 70701d3722..5575084e0e 100755 --- a/test/spec.sh +++ b/test/spec.sh @@ -980,6 +980,11 @@ oil-interactive() { $OIL_LIST "$@" } +oil-user-feedback() { + sh-spec spec/oil-user-feedback.test.sh --osh-failures-allowed 0 \ + $OIL_LIST "$@" +} + ble-idioms() { sh-spec spec/ble-idioms.test.sh --osh-failures-allowed 0 \ $BASH $ZSH $MKSH $BUSYBOX_ASH $OSH_LIST "$@"