-
Notifications
You must be signed in to change notification settings - Fork 390
Commit b603535
committed
feat(_comp_compgen): support
When `-U var` is specified for a builtin `compgen` call, variable
`var` is unlocalized by `_comp_unlocal` just before storing the result
to the target array. When `-U var` is specified for a generator call,
variable `var` is unlocalized by `_comp_unlocal` just before calling
the generator function `_comp_compgen_G2` (where `G2` is the generator
name)..
A generator should basically define local variables with the names
starting with `_`. However, a generator sometimes needs to use local
variable names that do not start with `_`. When the child generator
call with a variable name (such as `local var; _comp_compgen -v var`)
is used within the generator, the local variable can unexpectedly mask
a local variable of the upper call.
For example, the following call fails to obtain the result of
generator `mygen1` because the array `arr` is masked by the same name
of a local variable in `_comp_compgen_mygen1`.
# generator with a problem
_comp_compgen_mygen1()
{
local -a arr=(1 2 3)
_comp_compgen -av arr -- -W '4 5 6'
_comp_compgen_set "${arr[@]/#p}"
}
_comp_compgen -v arr mygen1 # fails to get the result in array `arr`
To avoid this, a generator that defines a local variable that does not
start with `_` can use the option `-U var` to unlocalize the variable
on assigning the final result.
# properly designed generator
_comp_compgen_mygen1()
{
local -a arr=(1 2 3)
_comp_compgen -av arr -- -W '4 5 6'
_comp_compgen -U arr set "${arr[@]/#p}"
}-U var
to unlocal var1 parent 201239c commit b603535Copy full SHA for b603535
File tree
Expand file treeCollapse file tree
3 files changed
+141
-64
lines changedFilter options
- doc
- test/t/unit
Expand file treeCollapse file tree
3 files changed
+141
-64
lines changed
0 commit comments