Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 'pre_execution' Hook to Receive Command Details as an Argument #15174

Open
KamilKleina opened this issue Feb 25, 2025 · 1 comment
Open
Labels
enhancement New feature or request needs-triage An issue that hasn't had any proper look

Comments

@KamilKleina
Copy link

Related problem

Currently, the pre_execution hook in Nushell runs before a command executes but does not receive any details about the command itself. This feature request proposes passing a record containing the command's details (e.g., the full command line) to the hook block, allowing for more advanced pre-execution logic.

Motivation

Currently, the pre_execution hook does not receive any command details, limiting its usefulness for debugging, logging, and command analysis. Many tools in other shells, such as alias-finder in Zsh, rely on access to the command being executed. Adding this feature would enable similar functionality in Nushell, allowing users to build plugins for alias discovery, shell logging, and execution monitoring.

Describe the solution you'd like

Proposed Behavior

  • If the hook block is defined without parameters, it behaves as it does today.
  • If the block expects a parameter, Nushell passes a record containing details about the command being executed.
  • At a minimum, the record should include the full command string before execution.

Usage Example

With this feature, users could define a pre_execution hook like this:

$env.config = (
    $env.config
    | upsert hooks.pre_execution [ { |cmd| print $cmd } ]
)

### Describe alternatives you've considered

_No response_

### Additional context and details

_No response_
@KamilKleina KamilKleina added enhancement New feature or request needs-triage An issue that hasn't had any proper look labels Feb 25, 2025
@fdncred
Copy link
Collaborator

fdncred commented Feb 25, 2025

There's a test that does some minimal testing with commandline, which should be able to get the command line in pre_execution. Have you tried that?

nushell/tests/hooks/mod.rs

Lines 326 to 338 in 9521b20

#[test]
fn pre_execution_commandline() {
let inp = &[
&pre_execution_hook_code("{|| $env.repl_commandline = (commandline) }"),
"$env.repl_commandline",
];
let actual_repl = nu!(nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "$env.repl_commandline");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs-triage An issue that hasn't had any proper look
Projects
None yet
Development

No branches or pull requests

2 participants