From 8ec558afc60c74b7fef2a3967ccbcf36b4ed0518 Mon Sep 17 00:00:00 2001 From: jdx <216188+jdx@users.noreply.github.com> Date: Wed, 8 Nov 2023 12:14:48 -0600 Subject: [PATCH] shell: fixed -h,--help flag (#976) --- src/cli/shell.rs | 2 +- src/shell/bash.rs | 12 +++++++----- src/shell/fish.rs | 16 ++++++++++++++-- .../rtx__shell__bash__tests__hook_init.snap | 12 +++++++----- .../rtx__shell__bash__tests__hook_init_nix.snap | 12 +++++++----- .../rtx__shell__fish__tests__hook_init.snap | 16 ++++++++++++++-- .../rtx__shell__fish__tests__hook_init_nix.snap | 16 ++++++++++++++-- .../rtx__shell__zsh__tests__hook_init.snap | 12 +++++++----- .../rtx__shell__zsh__tests__hook_init_nix.snap | 12 +++++++----- src/shell/zsh.rs | 12 +++++++----- 10 files changed, 85 insertions(+), 37 deletions(-) diff --git a/src/cli/shell.rs b/src/cli/shell.rs index 763190cda1..2738390eae 100644 --- a/src/cli/shell.rs +++ b/src/cli/shell.rs @@ -13,7 +13,7 @@ use crate::toolset::{ToolSource, ToolsetBuilder}; /// /// Only works in a session where rtx is already activated. #[derive(Debug, clap::Args)] -#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)] +#[clap(verbatim_doc_comment, alias = "s", after_long_help = AFTER_LONG_HELP)] pub struct Shell { /// Tool(s) to use #[clap(value_name="TOOL@VERSION", value_parser = ToolArgParser)] diff --git a/src/shell/bash.rs b/src/shell/bash.rs index 977f378c0c..db3c5dbe2b 100644 --- a/src/shell/bash.rs +++ b/src/shell/bash.rs @@ -28,13 +28,15 @@ impl Shell for Bash { shift case "$command" in - deactivate|shell) - eval "$(command rtx "$command" "$@")" - ;; - *) - command rtx "$command" "$@" + deactivate|s|shell) + # if argv doesn't contains -h,--help + if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]; then + eval "$(command rtx "$command" "$@")" + return $? + fi ;; esac + command rtx "$command" "$@" }} _rtx_hook() {{ diff --git a/src/shell/fish.rs b/src/shell/fish.rs index 61db98f655..9af41a919b 100644 --- a/src/shell/fish.rs +++ b/src/shell/fish.rs @@ -32,9 +32,21 @@ impl Shell for Fish { set command $argv[1] set -e argv[1] + if contains -- --help $argv + command rtx "$command" $argv + return $status + end + switch "$command" - case deactivate shell - source (command rtx "$command" $argv|psub) + case deactivate s shell + # if help is requested, don't eval + if contains -- -h $argv + command rtx "$command" $argv + else if contains -- --help $argv + command rtx "$command" $argv + else + source (command rtx "$command" $argv |psub) + end case '*' command rtx "$command" $argv end diff --git a/src/shell/snapshots/rtx__shell__bash__tests__hook_init.snap b/src/shell/snapshots/rtx__shell__bash__tests__hook_init.snap index cdd1d50f67..cdd27d9240 100644 --- a/src/shell/snapshots/rtx__shell__bash__tests__hook_init.snap +++ b/src/shell/snapshots/rtx__shell__bash__tests__hook_init.snap @@ -15,13 +15,15 @@ rtx() { shift case "$command" in - deactivate|shell) - eval "$(command rtx "$command" "$@")" - ;; - *) - command rtx "$command" "$@" + deactivate|s|shell) + # if argv doesn't contains -h,--help + if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]; then + eval "$(command rtx "$command" "$@")" + return $? + fi ;; esac + command rtx "$command" "$@" } _rtx_hook() { diff --git a/src/shell/snapshots/rtx__shell__bash__tests__hook_init_nix.snap b/src/shell/snapshots/rtx__shell__bash__tests__hook_init_nix.snap index 790a2a9cb3..9e555f3cdb 100644 --- a/src/shell/snapshots/rtx__shell__bash__tests__hook_init_nix.snap +++ b/src/shell/snapshots/rtx__shell__bash__tests__hook_init_nix.snap @@ -14,13 +14,15 @@ rtx() { shift case "$command" in - deactivate|shell) - eval "$(command rtx "$command" "$@")" - ;; - *) - command rtx "$command" "$@" + deactivate|s|shell) + # if argv doesn't contains -h,--help + if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]; then + eval "$(command rtx "$command" "$@")" + return $? + fi ;; esac + command rtx "$command" "$@" } _rtx_hook() { diff --git a/src/shell/snapshots/rtx__shell__fish__tests__hook_init.snap b/src/shell/snapshots/rtx__shell__fish__tests__hook_init.snap index 206d3b33e2..a9dfcd5beb 100644 --- a/src/shell/snapshots/rtx__shell__fish__tests__hook_init.snap +++ b/src/shell/snapshots/rtx__shell__fish__tests__hook_init.snap @@ -14,9 +14,21 @@ function rtx set command $argv[1] set -e argv[1] + if contains -- --help $argv + command rtx "$command" $argv + return $status + end + switch "$command" - case deactivate shell - source (command rtx "$command" $argv|psub) + case deactivate s shell + # if help is requested, don't eval + if contains -- -h $argv + command rtx "$command" $argv + else if contains -- --help $argv + command rtx "$command" $argv + else + source (command rtx "$command" $argv |psub) + end case '*' command rtx "$command" $argv end diff --git a/src/shell/snapshots/rtx__shell__fish__tests__hook_init_nix.snap b/src/shell/snapshots/rtx__shell__fish__tests__hook_init_nix.snap index 82f6744388..4a02b49672 100644 --- a/src/shell/snapshots/rtx__shell__fish__tests__hook_init_nix.snap +++ b/src/shell/snapshots/rtx__shell__fish__tests__hook_init_nix.snap @@ -13,9 +13,21 @@ function rtx set command $argv[1] set -e argv[1] + if contains -- --help $argv + command rtx "$command" $argv + return $status + end + switch "$command" - case deactivate shell - source (command rtx "$command" $argv|psub) + case deactivate s shell + # if help is requested, don't eval + if contains -- -h $argv + command rtx "$command" $argv + else if contains -- --help $argv + command rtx "$command" $argv + else + source (command rtx "$command" $argv |psub) + end case '*' command rtx "$command" $argv end diff --git a/src/shell/snapshots/rtx__shell__zsh__tests__hook_init.snap b/src/shell/snapshots/rtx__shell__zsh__tests__hook_init.snap index 7f0b514a3d..c6ebdde603 100644 --- a/src/shell/snapshots/rtx__shell__zsh__tests__hook_init.snap +++ b/src/shell/snapshots/rtx__shell__zsh__tests__hook_init.snap @@ -15,13 +15,15 @@ rtx() { shift case "$command" in - deactivate|shell) - eval "$(command rtx "$command" "$@")" - ;; - *) - command rtx "$command" "$@" + deactivate|s|shell) + # if argv doesn't contains -h,--help + if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]; then + eval "$(command rtx "$command" "$@")" + return $? + fi ;; esac + command rtx "$command" "$@" } _rtx_hook() { diff --git a/src/shell/snapshots/rtx__shell__zsh__tests__hook_init_nix.snap b/src/shell/snapshots/rtx__shell__zsh__tests__hook_init_nix.snap index 891bf6245e..f1218f4b40 100644 --- a/src/shell/snapshots/rtx__shell__zsh__tests__hook_init_nix.snap +++ b/src/shell/snapshots/rtx__shell__zsh__tests__hook_init_nix.snap @@ -14,13 +14,15 @@ rtx() { shift case "$command" in - deactivate|shell) - eval "$(command rtx "$command" "$@")" - ;; - *) - command rtx "$command" "$@" + deactivate|s|shell) + # if argv doesn't contains -h,--help + if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]; then + eval "$(command rtx "$command" "$@")" + return $? + fi ;; esac + command rtx "$command" "$@" } _rtx_hook() { diff --git a/src/shell/zsh.rs b/src/shell/zsh.rs index 1635584993..d600187567 100644 --- a/src/shell/zsh.rs +++ b/src/shell/zsh.rs @@ -32,13 +32,15 @@ impl Shell for Zsh { shift case "$command" in - deactivate|shell) - eval "$(command rtx "$command" "$@")" - ;; - *) - command rtx "$command" "$@" + deactivate|s|shell) + # if argv doesn't contains -h,--help + if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]; then + eval "$(command rtx "$command" "$@")" + return $? + fi ;; esac + command rtx "$command" "$@" }} _rtx_hook() {{