[PATCH v2 2/3] allow positional arguments with "run" command

Simon Glass
2020-10-12T03:34:54Z

On Wed, 7 Oct 2020 at 01:21, Rasmus Villemoes
<rasmus.villemoes@prevas.dk> wrote:
>
> Currently, the only way to emulate functions with arguments in the
> U-Boot shell is by doing "foo=arg1; bar=arg2; run func" and having
> "func" refer to $foo and $bar. That works, but is a bit clunky, and
> also suffers from foo and bar being set globally - if func itself wants
> to run other "functions" defined in the environment, those other
> functions better not use the same parameter names:
>
> setenv g 'do_g_stuff $foo'
> setenv f 'do_f_stuff $foo $bar; foo=123; run g; do_more_f_stuff $foo $bar'
> foo=arg1; bar=arg2; run f
>
> Sure, f could do a "saved_foo=$foo; .... foo=$saved_foo" dance, but
> that makes everything even more clunky.
>
> In order to increase readability, allow passing positional arguments
> to the functions invoked via run: When invoked with a -- separator,
> the remaining arguments are use to set the local shell variables $1 through
> $9 (and $#). As in a "real" shell, they are local to the current
> function, so if f is called with two arguments, and f calls g with one
> argument, g sees $2 as unset. Then the above can be written
>
> setenv g 'do_g_stuff $1'
> setenv f 'do_f_stuff $1 $2; run g -- 123; do_more_f_stuff $1 $2'
> run f -- arg1 arg2
>
> Everything except
>
> - b_addchr(dest, '?');
> + b_addchr(dest, ch);
>
> is under CONFIG_CMD_RUN_ARGS, and when CONFIG_CMD_RUN_ARGS=n, the ch
> there can only be '?'. So no functional change when
> CONFIG_CMD_RUN_ARGS is not selected.
>
> Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
> ---
> cmd/Kconfig | 10 ++++++++++
> cmd/nvedit.c | 7 ++++++-
> common/cli.c | 44 ++++++++++++++++++++++++++++++++++++++------
> common/cli_hush.c | 32 +++++++++++++++++++++++++++++++-
> include/cli_hush.h | 9 +++++++++
> 5 files changed, 94 insertions(+), 8 deletions(-)
>

Reviewed-by: Simon Glass <sjg@chromium.org>

I'm not sure where the previous discussion went. But please think
about how we can add some tests here.

- Simon