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

feat: Print help menu on --help for all commands #1456

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/commands/command-current.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

# shellcheck disable=SC2059
plugin_current_command() {
if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf current'
printf '%s\n' 'usage: asdf current <name>'
exit 0
fi

local plugin_name=$1
local terminal_format=$2

Expand Down
7 changes: 6 additions & 1 deletion lib/commands/command-env.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ shim_env_command() {
local env_cmd="${2}"
local env_args=("${@:3}")

local help_text="usage: asdf env <command>"
if has_help_flag "$@"; then
printf '%s\n' "$help_text"
exit 0
fi
if [ -z "$shim_name" ]; then
printf "usage: asdf env <command>\n"
display_error "$help_text"
exit 1
fi

Expand Down
8 changes: 7 additions & 1 deletion lib/commands/command-exec.bash
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# -*- sh -*-

shim_exec_command() {
local help_text="usage: asdf exec <command>"
if has_help_flag "$@"; then
printf '%s\n' "$help_text"
exit 0
fi

local shim_name
shim_name=$(basename "$1")
local shim_args=("${@:2}")

if [ -z "$shim_name" ]; then
printf "usage: asdf exec <command>\n"
display_error "$help_text"
exit 1
fi

Expand Down
8 changes: 7 additions & 1 deletion lib/commands/command-export-shell-version.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

# Output from this command must be executable shell code
shell_command() {
local help_text="usage: asdf shell <name> {<version>|--unset}"
if has_help_flag "$@"; then
printf '%s\n' "printf '%s\n' \"$help_text\""
exit 0
fi

local asdf_shell="$1"
shift

if [ "$#" -lt "2" ]; then
printf "Usage: asdf shell <name> {<version>|--unset}\n" >&2
display_error "$help_text"
printf "false\n"
exit 1
fi
Expand Down
5 changes: 5 additions & 0 deletions lib/commands/command-help.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ help_command() {
local tool_version="$2"
local plugin_path

if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf help <name> [<version>]'
exit 0
fi

# If plugin name is present as first argument output plugin help info
if [ -n "$plugin_name" ]; then
plugin_path=$(get_plugin_path "$plugin_name")
Expand Down
5 changes: 5 additions & 0 deletions lib/commands/command-info.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash"

info_command() {
if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf info'
exit 0
fi

printf "%s:\n%s\n\n" "OS" "$(uname -a)"
printf "%s:\n%s\n\n" "SHELL" "$("$SHELL" --version)"
printf "%s:\n%s\n\n" "BASH VERSION" "$BASH_VERSION"
Expand Down
5 changes: 5 additions & 0 deletions lib/commands/command-list.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ list_command() {
local plugin_name=$1
local query=$2

if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf list <name> [version]'
exit 0
fi

if [ -z "$plugin_name" ]; then
local plugins_path
plugins_path=$(get_plugin_path)
Expand Down
5 changes: 5 additions & 0 deletions lib/commands/command-plugin-list-all.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# -*- sh -*-

plugin_list_all_command() {
if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf plugin list all'
exit 0
fi

initialize_or_update_plugin_repository

local plugins_index_path
Expand Down
5 changes: 5 additions & 0 deletions lib/commands/command-plugin-remove.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# -*- sh -*-

plugin_remove_command() {
if has_help_flag "$@"; then
printf '%s\n' "usage: asdf plugin remove <name>"
exit 0
fi

local plugin_name=$1
check_if_plugin_exists "$plugin_name"

Expand Down
5 changes: 5 additions & 0 deletions lib/commands/command-shim-versions.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# -*- sh -*-

shim_versions_command() {
if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf shim-versions <command>'
exit 0
fi

local shim_name=$1
shim_plugin_versions "$shim_name"
}
Expand Down
6 changes: 6 additions & 0 deletions lib/commands/command-uninstall.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ uninstall_command() {
local plugin_name=$1
local full_version=$2
local plugin_path

if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf uninstall <name> <version>'
exit 0
fi

plugin_path=$(get_plugin_path "$plugin_name")

check_if_plugin_exists "$plugin_name"
Expand Down
6 changes: 6 additions & 0 deletions lib/commands/command-update.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
update_command() {
local update_to_head=$1

if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf update'
printf '%s\n' 'usage: asdf update --head'
exit 0
fi

(
cd "$(asdf_dir)" || exit 1

Expand Down
6 changes: 6 additions & 0 deletions lib/commands/command-where.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
where_command() {
local plugin_name=$1
local full_version=$2

if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf where <name> [<version>]'
exit 0
fi

check_if_plugin_exists "$plugin_name"

local version
Expand Down
8 changes: 7 additions & 1 deletion lib/commands/command-which.bash
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# -*- sh -*-

which_command() {
local help_text="usage: asdf which <command>"
if has_help_flag "$@"; then
printf '%s\n' "$help_text"
exit 0
fi

local shim_name
shim_name=$(basename "$1")

if [ -z "$shim_name" ]; then
printf "usage: asdf which <command>\n"
display_error "$help_text"
exit 1
fi

Expand Down
5 changes: 5 additions & 0 deletions lib/commands/reshim.bash
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ reshim_command() {
local plugin_name=$1
local full_version=$2

if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf reshim <name> <version>'
exit 0
fi

if [ -z "$plugin_name" ]; then
local plugins_path
plugins_path=$(get_plugin_path)
Expand Down
8 changes: 8 additions & 0 deletions lib/functions/installs.bash
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ install_command() {
local full_version=$2
local extra_args="${*:3}"

if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf install'
printf '%s\n' 'usage: asdf install <name>'
printf '%s\n' 'usage: asdf install <name> <version>'
printf '%s\n' 'usage: asdf install <name> latest[:<version>]'
exit 0
fi

if [ "$plugin_name" = "" ] && [ "$full_version" = "" ]; then
install_local_tool_versions "$extra_args"
elif [[ $# -eq 1 ]]; then
Expand Down
23 changes: 21 additions & 2 deletions lib/functions/plugins.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
plugin_list_command() {
if has_help_flag "$@"; then
printf '%s\n' "usage: asdf plugin list [--urls] [--refs]"
printf '%s\n' "usage: asdf plugin list all"
exit 0
fi

local plugins_path
plugins_path=$(get_plugin_path)

Expand Down Expand Up @@ -47,8 +53,13 @@ plugin_list_command() {
}

plugin_add_command() {
local help_text="usage: asdf plugin add <name> [<git-url>]"
if has_help_flag "$@"; then
printf '%s\n' "$help_text"
exit 0
fi
if [[ $# -lt 1 || $# -gt 2 ]]; then
display_error "usage: asdf plugin add <name> [<git-url>]"
display_error "$help_text"
exit 1
fi

Expand Down Expand Up @@ -104,8 +115,16 @@ plugin_add_command() {
}

plugin_update_command() {
local help_text1="usage: asdf plugin-update <name> [git-ref]"
local help_text2="usage: asdf plugin-update --all"
if has_help_flag "$@"; then
printf '%s\n' "$help_text1"
printf '%s\n' "$help_text2"
exit 0
fi
if [ "$#" -lt 1 ]; then
display_error "usage: asdf plugin-update {<name> [git-ref] | --all}"
display_error "$help_text1"
display_error "$help_text2"
exit 1
fi

Expand Down
29 changes: 24 additions & 5 deletions lib/functions/versions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ version_command() {
local cmd=$1
local plugin_name=$2

if [ "$#" -lt "3" ]; then
if [ "$cmd" = "global" ]; then
printf "Usage: asdf global <name> <version>\n"
if has_help_flag "$@" || [ "$#" -lt "3" ]; then
if [ "$cmd" = 'global' ]; then
printf '%s\n' "usage: asdf global <name> <version>"
printf '%s\n' "usage: asdf global <name> latest[:<version>]"
else
printf "Usage: asdf local <name> <version>\n"
printf '%s\n' "usage: asdf local <name> <version>"
printf '%s\n' "usage: asdf local <name> latest[:<version>]"
fi

if has_help_flag "$@"; then
exit 0
else
exit 1
fi
exit 1
fi

shift 2
Expand Down Expand Up @@ -86,6 +93,12 @@ list_all_command() {
local std_out_file
local std_err_file
local output

if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf list all <name> [<version>]'
exit 0
fi

plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"

Expand Down Expand Up @@ -136,6 +149,12 @@ latest_command() {
local query=$2
local plugin_path

if has_help_flag "$@"; then
printf '%s\n' 'usage: asdf latest <name> [<version>]'
printf '%s\n' 'usage: asdf latest --all'
exit 0
fi

if [ "$plugin_name" = "--all" ]; then
latest_all
fi
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,15 @@ get_plugin_remote_gitref() {
plugin_path="$(get_plugin_path "$plugin_name")"
git --git-dir "$plugin_path/.git" rev-parse --short HEAD 2>/dev/null
}

# @description Returns true if any arguments is '-h' or '--help'
has_help_flag() {
local arg=
for arg in "$@"; do
case $arg in
-h | --help) return 0 ;;
esac
done

return 1
}
1 change: 1 addition & 0 deletions test/banned_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ teardown() {
| grep -v '#.*$cmd'\
| grep -v '\".*$cmd.*\"' \
| grep -v '${cmd}_'\
| grep -v 'usage: asdf'\
| grep -v '# asdf_allow: $cmd'"

# Only print output if we've found a banned command
Expand Down
6 changes: 6 additions & 0 deletions test/current_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,9 @@ foobar 1.0.0 $PROJECT_DIR/.tool-versions"
[ "$status" -eq 0 ]
[ "$output" = "$expected" ]
}

@test "current prints help if --help is passed" {
run asdf current --help
[ "$status" -eq 0 ]
[[ "${lines[0]}" == 'usage: '* ]]
}
6 changes: 6 additions & 0 deletions test/help_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@ EOF
[[ $output == *$'UTILS\n'* ]]
[[ $output == *$'"Late but latest"\n-- Rajinikanth' ]]
}

@test "help prints help if --help is passed" {
run asdf help --help
[ "$status" -eq 0 ]
[[ "${lines[0]}" == 'usage: '* ]]
}
6 changes: 6 additions & 0 deletions test/info_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ teardown() {
[[ $output == *$'ASDF INSTALLED PLUGINS:\n'* ]]

}

@test "info prints help if --help is passed" {
run asdf info --help
[ "$status" -eq 0 ]
[[ "${lines[0]}" == 'usage: '* ]]
}
6 changes: 6 additions & 0 deletions test/install_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,9 @@ EOM

[[ "$output" == *'asdf: Warn:'*'not be preserved'* ]]
}

@test "install_command prints help if --help is passed" {
run asdf install --help
[ "$status" -eq 0 ]
[[ "${lines[0]}" == 'usage: '* ]]
}
6 changes: 6 additions & 0 deletions test/latest_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ teardown() {
clean_asdf_dir
}

@test "[laetst_command] prints help if --help is passed" {
run asdf latest --help
[ "$status" -eq 0 ]
[[ "${lines[0]}" == 'usage: '* ]]
}

####################################################
#### plugin with bin/latest-stable ####
####################################################
Expand Down
Loading
Loading