bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Enable compgen even when programmable completions are not available?


From: Martin D Kealey
Subject: Re: Enable compgen even when programmable completions are not available?
Date: Mon, 26 Jun 2023 17:09:47 +1000

Hi Eli

How about using the shell itself to parse the output of "typeset" (an alias
for "declare"), but redefining "declare" to do something different. This is
a bit verbose but it works cleanly:

```
(
  function declare {
    while [[ $1 = -* ]] ; do shift ; done
    printf %s\\n "${@%%=*}"
  }
  eval "$( typeset -p )"
)
```
or
```
(
  declare -a var_names=()
  function declare {
    local v
    for v do
      [[ $v = -* ]] ||
        var_names+=("${v%%=*}")
    done
  }
  eval "$( typeset -p )"
  unset -f declare
  do_something_with "${var_names[@]}"
)
```

-- 
-Martin

On Mon, 26 Jun 2023 at 04:38, Eli Schwartz <eschwartz93@gmail.com> wrote:

> compgen is a useful builtin for inspecting information about the shell
> context e.g. in scripts -- a good example of this is compgen -A function
> or compgen -A variable.
>
> But it's not always available depending on how bash is built, which
> results in people lacking confidence that it can / should be used in
> scripts. See e.g. https://bugs.gentoo.org/909148
>
> Would it be possible to have a slightly more minimal version of it with
> readline functionality and "currently inside programmable completions"
> functionality stripped out, which just allows it to be used for general
> environment introspection?
>
> An alternative for compgen -A function does exist -- declare -F in
> combination with e.g. sed. Variables is harder since AFAICT the only way
> to print all of them prints multiline values too (declare -p is similar
> to declare -f, there is no declare -P similar to declare -F, using
> ${!a@} to expand variable names does not work generically enough since
> you need to specify a prefix -- ${!@} won't expand all of them).
>
>
> --
> Eli Schwartz
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]