From 1c71fc0c097f14ba857936fb56253c65abbb62d6 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 29 Apr 2024 12:18:25 +1000 Subject: [PATCH] Add local scope to user commands --- doc/pages/scopes.asciidoc | 4 +++- src/commands.cc | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/pages/scopes.asciidoc b/doc/pages/scopes.asciidoc index 02bb5470..c74a7c21 100644 --- a/doc/pages/scopes.asciidoc +++ b/doc/pages/scopes.asciidoc @@ -39,7 +39,9 @@ Scopes are named as follows: *local*:: A local scope is inserted by each *evaluate-commands* invocations 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 value, such as an option or an alias. diff --git a/src/commands.cc b/src/commands.cc index d8335664..373f3b5c 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1404,6 +1404,7 @@ void define_command(const ParametersParser& parser, Context& context, const Shel desc = ParameterDesc{ {}, ParameterDesc::Flags::SwitchesAsPositional, min, max }; cmd = [=](const ParametersParser& parser, Context& context, const ShellContext& sc) { + LocalScope local_scope{context}; CommandManager::instance().execute(commands, context, { 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 }; cmd = [=](const ParametersParser& parser, Context& context, const ShellContext& sc) { + LocalScope local_scope{context}; CommandManager::instance().execute(commands, context, { {}, sc.env_vars }); }; }