Skip to content

Commit

Permalink
feat: All commands print help on --help
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall committed Jan 29, 2023
1 parent f0fe961 commit cd05e7c
Show file tree
Hide file tree
Showing 40 changed files with 299 additions and 16 deletions.
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" "ASDF VERSION" "$(asdf_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_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
13 changes: 9 additions & 4 deletions lib/commands/command-which.bash
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# -*- sh -*-

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

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

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

print_exec() {
local plugin_name="$1"
local version="$2"
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 @@ -49,8 +55,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 @@ -106,8 +117,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 @@ -79,6 +86,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 @@ -129,6 +142,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 @@ -860,3 +860,15 @@ util_resolve_user_path() {
util_resolve_user_path_reply="$path"
fi
}

# @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 @@ -75,3 +75,9 @@ EOF
[ "$status" -eq 0 ]
# TODO: Assert asdf help output is printed
}

@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 @@ -25,3 +25,9 @@ teardown() {
[ "$status" -eq 0 ]
# TODO: Assert asdf info output is printed
}

@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 @@ -283,3 +283,9 @@ EOM
[ ! -d "$ASDF_DIR/installs/dummy-broken/1.1.0" ]
[ "$output" = "Download failed!" ]
}

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

0 comments on commit cd05e7c

Please sign in to comment.