Add local scope to user commands

This commit is contained in:
Maxime Coste 2024-04-29 12:18:25 +10:00
parent e8ad943532
commit 1c71fc0c09
2 changed files with 5 additions and 1 deletions

View File

@ -39,7 +39,9 @@ Scopes are named as follows:
*local*:: *local*::
A local scope is inserted by each *evaluate-commands* invocations A local scope is inserted by each *evaluate-commands* invocations
for its duration. Nested *evaluate-commands* each inject a new for its duration. Nested *evaluate-commands* each inject a new
local scope whose parent is the previous local scope. local scope whose parent is the previous local scope. User declared
commands defined with *define-command* also introduce a local scope
while executing.
A local scope is intended for temporarily overwriting some scoped A local scope is intended for temporarily overwriting some scoped
value, such as an option or an alias. value, such as an option or an alias.

View File

@ -1404,6 +1404,7 @@ void define_command(const ParametersParser& parser, Context& context, const Shel
desc = ParameterDesc{ {}, ParameterDesc::Flags::SwitchesAsPositional, min, max }; desc = ParameterDesc{ {}, ParameterDesc::Flags::SwitchesAsPositional, min, max };
cmd = [=](const ParametersParser& parser, Context& context, const ShellContext& sc) { cmd = [=](const ParametersParser& parser, Context& context, const ShellContext& sc) {
LocalScope local_scope{context};
CommandManager::instance().execute(commands, context, CommandManager::instance().execute(commands, context,
{ params_to_shell(parser), sc.env_vars }); { params_to_shell(parser), sc.env_vars });
}; };
@ -1412,6 +1413,7 @@ void define_command(const ParametersParser& parser, Context& context, const Shel
{ {
desc = ParameterDesc{ {}, ParameterDesc::Flags::SwitchesAsPositional, 0, 0 }; desc = ParameterDesc{ {}, ParameterDesc::Flags::SwitchesAsPositional, 0, 0 };
cmd = [=](const ParametersParser& parser, Context& context, const ShellContext& sc) { cmd = [=](const ParametersParser& parser, Context& context, const ShellContext& sc) {
LocalScope local_scope{context};
CommandManager::instance().execute(commands, context, { {}, sc.env_vars }); CommandManager::instance().execute(commands, context, { {}, sc.env_vars });
}; };
} }