Double expansion/expansion inside an expansion?

I am trying to use options with names depending on user input ( that is, argument to a command), since there is no arrays or other collection types in kakoune.
So far I have been able to create such options with

declare-option str “bridge_%arg{1}_pid”
and when I call
my-command AAA
I get an option named bridge_AAA_pid

However I don’t know how to use the content of the created option, in either %{} or %sh{} blocks.
So far I’ve tried

define-command -override test-pid -params 1 %{
echo %opt{bridge_%arg{1}pid}
echo %opt{bridge
"%arg{1}“pid}
echo %opt{"bridge
%arg{1}pid"}
echo %opt"{bridge
%arg{1}pid}"
echo "%opt{bridge
%arg{1}_pid}”
}
test-pid AAA
but none of them worked.

Unlike POSIX shell, which has multiple expansion passes and thus frequently allows things to be expanded more times than the programmer expects, Kakoune is very careful to only ever expand things once, unless you ask for it specifically. That means you can’t do the kind of thing you’re trying to do deliberately, but it also means you can’t do it accidentally, so overall I think it’s a win.

If you want multiple rounds of expansion, you’re going to need to use the eval command, something like this:

define-command -override test-pid -params 1 %{
    eval "echo %%opt{bridge_%arg{1}_pid}"
}

Calling :test-pid AAA will result in the following transformations:

  • eval "echo %%opt{bridge_%arg{1}_pid}"
  • eval 'echo %opt{bridge_AAA_pid}'
  • echo %opt{bridge_AAA_pid}
  • echo THE_VALUE

…assuming the current value of bridge_AAA_pid is “THE_VALUE”.

Thank you very much.

The problem is now solved by

declare-option str bridge_pid_temp
define-command -override test-pid -params 1 %{
eval “set-option global bridge_pid_temp %%opt{bridge_%arg{1}_pid}”
do-whatever-with %opt{bridge_pid_temp}
echo %sh{ echo $kak_opt_bridge_pid_temp now accessible in shell}
}
test-pid AAA

Though I wonder if there is a solution without the intermediate bridge_pid_temp.