diff --git a/lib/commands/command-current.bash b/lib/commands/command-current.bash index f4724626e..6dd8b7897 100644 --- a/lib/commands/command-current.bash +++ b/lib/commands/command-current.bash @@ -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 ' + exit 0 + fi + local plugin_name=$1 local terminal_format=$2 diff --git a/lib/commands/command-env.bash b/lib/commands/command-env.bash index 30cf7ec4e..de32fafca 100644 --- a/lib/commands/command-env.bash +++ b/lib/commands/command-env.bash @@ -5,8 +5,13 @@ shim_env_command() { local env_cmd="${2}" local env_args=("${@:3}") + local help_text="usage: asdf env " + if has_help_flag "$@"; then + printf '%s\n' "$help_text" + exit 0 + fi if [ -z "$shim_name" ]; then - printf "usage: asdf env \n" + display_error "$help_text" exit 1 fi diff --git a/lib/commands/command-exec.bash b/lib/commands/command-exec.bash index cb56b36c3..f6033140f 100644 --- a/lib/commands/command-exec.bash +++ b/lib/commands/command-exec.bash @@ -1,12 +1,18 @@ # -*- sh -*- shim_exec_command() { + local help_text="usage: asdf exec " + 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 \n" + display_error "$help_text" exit 1 fi diff --git a/lib/commands/command-export-shell-version.bash b/lib/commands/command-export-shell-version.bash index 92ace8abe..938cc6bb8 100644 --- a/lib/commands/command-export-shell-version.bash +++ b/lib/commands/command-export-shell-version.bash @@ -4,11 +4,17 @@ # Output from this command must be executable shell code shell_command() { + local help_text="usage: asdf shell {|--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 {|--unset}\n" >&2 + display_error "$help_text" printf "false\n" exit 1 fi diff --git a/lib/commands/command-help.bash b/lib/commands/command-help.bash index 77f1bb27c..71684bee7 100644 --- a/lib/commands/command-help.bash +++ b/lib/commands/command-help.bash @@ -37,6 +37,11 @@ help_command() { local tool_version="$2" local plugin_path + if has_help_flag "$@"; then + printf '%s\n' 'usage: asdf help []' + 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") diff --git a/lib/commands/command-info.bash b/lib/commands/command-info.bash index d4e761cf4..2ab651da3 100644 --- a/lib/commands/command-info.bash +++ b/lib/commands/command-info.bash @@ -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" diff --git a/lib/commands/command-list.bash b/lib/commands/command-list.bash index 6cb4cd4b9..1b0e1ecd7 100644 --- a/lib/commands/command-list.bash +++ b/lib/commands/command-list.bash @@ -4,6 +4,11 @@ list_command() { local plugin_name=$1 local query=$2 + if has_help_flag "$@"; then + printf '%s\n' 'usage: asdf list [version]' + exit 0 + fi + if [ -z "$plugin_name" ]; then local plugins_path plugins_path=$(get_plugin_path) diff --git a/lib/commands/command-plugin-list-all.bash b/lib/commands/command-plugin-list-all.bash index 232b69cd6..89cc60189 100644 --- a/lib/commands/command-plugin-list-all.bash +++ b/lib/commands/command-plugin-list-all.bash @@ -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 diff --git a/lib/commands/command-plugin-remove.bash b/lib/commands/command-plugin-remove.bash index 7b959bdf7..5a133b6ca 100644 --- a/lib/commands/command-plugin-remove.bash +++ b/lib/commands/command-plugin-remove.bash @@ -1,6 +1,11 @@ # -*- sh -*- plugin_remove_command() { + if has_help_flag "$@"; then + printf '%s\n' "usage: asdf plugin remove " + exit 0 + fi + local plugin_name=$1 check_if_plugin_exists "$plugin_name" diff --git a/lib/commands/command-shim-versions.bash b/lib/commands/command-shim-versions.bash index 9a8181a1e..ca70f03b6 100644 --- a/lib/commands/command-shim-versions.bash +++ b/lib/commands/command-shim-versions.bash @@ -1,6 +1,11 @@ # -*- sh -*- shim_versions_command() { + if has_help_flag "$@"; then + printf '%s\n' 'usage: asdf shim-versions ' + exit 0 + fi + local shim_name=$1 shim_plugin_versions "$shim_name" } diff --git a/lib/commands/command-uninstall.bash b/lib/commands/command-uninstall.bash index 2e1774f9b..875f3141b 100644 --- a/lib/commands/command-uninstall.bash +++ b/lib/commands/command-uninstall.bash @@ -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 ' + exit 0 + fi + plugin_path=$(get_plugin_path "$plugin_name") check_if_plugin_exists "$plugin_name" diff --git a/lib/commands/command-update.bash b/lib/commands/command-update.bash index f9912645f..c4adb7031 100644 --- a/lib/commands/command-update.bash +++ b/lib/commands/command-update.bash @@ -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 diff --git a/lib/commands/command-where.bash b/lib/commands/command-where.bash index d2b9d0396..a0165adc3 100644 --- a/lib/commands/command-where.bash +++ b/lib/commands/command-where.bash @@ -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 []' + exit 0 + fi + check_if_plugin_exists "$plugin_name" local version diff --git a/lib/commands/command-which.bash b/lib/commands/command-which.bash index 8696dbf3e..2d78243d6 100644 --- a/lib/commands/command-which.bash +++ b/lib/commands/command-which.bash @@ -1,11 +1,17 @@ # -*- sh -*- which_command() { + local help_text="usage: asdf which " + 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 \n" + display_error "$help_text" exit 1 fi diff --git a/lib/commands/reshim.bash b/lib/commands/reshim.bash index 2317973a1..f25f9db15 100644 --- a/lib/commands/reshim.bash +++ b/lib/commands/reshim.bash @@ -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 ' + exit 0 + fi + if [ -z "$plugin_name" ]; then local plugins_path plugins_path=$(get_plugin_path) diff --git a/lib/functions/installs.bash b/lib/functions/installs.bash index 036c1dab9..8b5a300b2 100644 --- a/lib/functions/installs.bash +++ b/lib/functions/installs.bash @@ -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 ' + printf '%s\n' 'usage: asdf install ' + printf '%s\n' 'usage: asdf install latest[:]' + exit 0 + fi + if [ "$plugin_name" = "" ] && [ "$full_version" = "" ]; then install_local_tool_versions "$extra_args" elif [[ $# -eq 1 ]]; then diff --git a/lib/functions/plugins.bash b/lib/functions/plugins.bash index 6f7a9435c..b7e439aad 100644 --- a/lib/functions/plugins.bash +++ b/lib/functions/plugins.bash @@ -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) @@ -47,8 +53,13 @@ plugin_list_command() { } plugin_add_command() { + local help_text="usage: asdf plugin add []" + 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 []" + display_error "$help_text" exit 1 fi @@ -104,8 +115,16 @@ plugin_add_command() { } plugin_update_command() { + local help_text1="usage: asdf plugin-update [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 { [git-ref] | --all}" + display_error "$help_text1" + display_error "$help_text2" exit 1 fi diff --git a/lib/functions/versions.bash b/lib/functions/versions.bash index 7d1d941d6..d15b7aa7e 100644 --- a/lib/functions/versions.bash +++ b/lib/functions/versions.bash @@ -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 \n" + if has_help_flag "$@" || [ "$#" -lt "3" ]; then + if [ "$cmd" = 'global' ]; then + printf '%s\n' "usage: asdf global " + printf '%s\n' "usage: asdf global latest[:]" else - printf "Usage: asdf local \n" + printf '%s\n' "usage: asdf local " + printf '%s\n' "usage: asdf local latest[:]" + fi + + if has_help_flag "$@"; then + exit 0 + else + exit 1 fi - exit 1 fi shift 2 @@ -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 []' + exit 0 + fi + plugin_path=$(get_plugin_path "$plugin_name") check_if_plugin_exists "$plugin_name" @@ -136,6 +149,12 @@ latest_command() { local query=$2 local plugin_path + if has_help_flag "$@"; then + printf '%s\n' 'usage: asdf latest []' + printf '%s\n' 'usage: asdf latest --all' + exit 0 + fi + if [ "$plugin_name" = "--all" ]; then latest_all fi diff --git a/lib/utils.bash b/lib/utils.bash index 21978a929..7e7a70105 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -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 +} diff --git a/test/banned_commands.bats b/test/banned_commands.bats index 84f8bf2a9..8245c67e1 100644 --- a/test/banned_commands.bats +++ b/test/banned_commands.bats @@ -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 diff --git a/test/current_command.bats b/test/current_command.bats index 687bc9da8..6a7baf1aa 100755 --- a/test/current_command.bats +++ b/test/current_command.bats @@ -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: '* ]] +} diff --git a/test/help_command.bats b/test/help_command.bats index 49e39654c..e5ba68c72 100644 --- a/test/help_command.bats +++ b/test/help_command.bats @@ -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: '* ]] +} diff --git a/test/info_command.bats b/test/info_command.bats index 3a8fc05d6..97074fe53 100644 --- a/test/info_command.bats +++ b/test/info_command.bats @@ -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: '* ]] +} diff --git a/test/install_command.bats b/test/install_command.bats index 9e70d544b..a3c03b1b7 100644 --- a/test/install_command.bats +++ b/test/install_command.bats @@ -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: '* ]] +} diff --git a/test/latest_command.bats b/test/latest_command.bats index 9c0bd5af3..fd666a5d3 100644 --- a/test/latest_command.bats +++ b/test/latest_command.bats @@ -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 #### #################################################### diff --git a/test/list_command.bats b/test/list_command.bats index f21f2cd1b..85f4716bc 100644 --- a/test/list_command.bats +++ b/test/list_command.bats @@ -75,6 +75,12 @@ teardown() { [ "$status" -eq 1 ] } +@test "list_command prints help if --help is passed" { + run asdf list --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} + @test "list_all_command lists available versions" { run asdf list-all dummy [ $'1.0.0\n1.1.0\n2.0.0' = "$output" ] @@ -109,3 +115,9 @@ teardown() { run asdf list-all dummy [[ "$output" != *"ignore this error"* ]] } + +@test "list_all_command prints help if --help is passed" { + run asdf list-all --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/plugin_add_command.bats b/test/plugin_add_command.bats index fa0348133..00c6d7fb5 100644 --- a/test/plugin_add_command.bats +++ b/test/plugin_add_command.bats @@ -174,3 +174,9 @@ EOM ADD" [ "$output" = "${expected_output}" ] } + +@test "plugin_add prints help if --help is passed" { + run asdf plugin add --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/plugin_list_all_command.bats b/test/plugin_list_all_command.bats index 6fa8bcb22..d0963b375 100644 --- a/test/plugin_list_all_command.bats +++ b/test/plugin_list_all_command.bats @@ -74,3 +74,9 @@ foo http://example.com/foo" [ "$status" -eq 0 ] [ "$output" = "$expected" ] } + +@test "plugin_list_all prints help if --help is passed" { + run asdf plugin list all --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/plugin_list_command.bats b/test/plugin_list_command.bats new file mode 100644 index 000000000..9ac58e58c --- /dev/null +++ b/test/plugin_list_command.bats @@ -0,0 +1,17 @@ +#!/usr/bin/env bats + +load test_helpers + +setup() { + setup_asdf_dir +} + +teardown() { + clean_asdf_dir +} + +@test "plugin_list_command prints help if --help is passed" { + run asdf plugin list --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/plugin_remove_command.bats b/test/plugin_remove_command.bats index 911ec374e..8d71ecbfa 100644 --- a/test/plugin_remove_command.bats +++ b/test/plugin_remove_command.bats @@ -26,3 +26,9 @@ teardown() { [ "$status" -eq 1 ] echo "$output" | grep "No such plugin: does-not-exist" } + +@test "plugin_remove prints help if --help is passed" { + run asdf plugin remove --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/plugin_update_command.bats b/test/plugin_update_command.bats index 9bde04c8f..7abb70107 100644 --- a/test/plugin_update_command.bats +++ b/test/plugin_update_command.bats @@ -230,3 +230,9 @@ UPDATE" local expected_output="Location of dummy plugin: $plugin_path" [[ "$output" == *"$expected_output"* ]] } + +@test "asdf plugin-update prints help if --help is passed" { + run asdf plugin update --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/reshim_command.bats b/test/reshim_command.bats index 4b1db7ef5..15fe2a67b 100644 --- a/test/reshim_command.bats +++ b/test/reshim_command.bats @@ -173,3 +173,9 @@ EOM run grep "asdf-plugin: dummy path:$ASDF_DIR/installs/dummy" "$ASDF_DIR/shims/dummy" [ "$status" -eq 0 ] } + +@test "reshim prints help if --help is passed" { + run asdf reshim --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/shim_env_command.bats b/test/shim_env_command.bats index c97da8502..993d9d5f3 100644 --- a/test/shim_env_command.bats +++ b/test/shim_env_command.bats @@ -82,3 +82,9 @@ teardown() { run grep -q '::' <<<"$path_line" [ "$status" -ne 0 ] } + +@test "asdf env prints help if --help is passed" { + run asdf env --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/shim_exec.bats b/test/shim_exec.bats index 5c6d0673e..ffc378b94 100644 --- a/test/shim_exec.bats +++ b/test/shim_exec.bats @@ -447,3 +447,9 @@ EOM [ "$output" = "This is Dummy 1.0! hello world" ] [ "$status" -eq 0 ] } + +@test "asdf exec prints help if --help is passed" { + run asdf exec --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/shim_versions_command.bats b/test/shim_versions_command.bats index 2371231b6..2a843e335 100644 --- a/test/shim_versions_command.bats +++ b/test/shim_versions_command.bats @@ -28,3 +28,9 @@ teardown() { echo "$output" | grep "dummy 3.0" echo "$output" | grep "dummy 1.0" } + +@test "shim_versions_command prints help if --help is passed" { + run asdf shim-versions --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/uninstall_command.bats b/test/uninstall_command.bats index c5bef1b46..b94b54d75 100644 --- a/test/uninstall_command.bats +++ b/test/uninstall_command.bats @@ -102,3 +102,9 @@ EOM run asdf uninstall dummy 1.0.0 [ "$output" = "removed dummy 1.0.0" ] } + +@test "uninstall command prints help if --help is passed" { + run asdf uninstall --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/update_command.bats b/test/update_command.bats index 9e377f16c..e7240bac3 100644 --- a/test/update_command.bats +++ b/test/update_command.bats @@ -109,3 +109,9 @@ teardown() { [ "$status" -eq 0 ] [ -f "$ASDF_DIR/shims/dummy" ] } + +@test "asdf update prints help if --help is passed" { + run asdf update --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/version_commands.bats b/test/version_commands.bats index 3c9fd0f76..1ba256d6e 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -36,7 +36,13 @@ teardown() { @test "local should emit an error when called with incorrect arity" { run asdf local "dummy" [ "$status" -eq 1 ] - [ "$output" = "Usage: asdf local " ] + [[ "$output" == "usage: "* ]] +} + +@test "local prints help if --help is passed" { + run asdf local --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] } @test "local should emit an error when plugin does not exist" { @@ -159,7 +165,7 @@ teardown() { @test "local -p/--parent should set should emit an error when called with incorrect arity" { run asdf local -p "dummy" [ "$status" -eq 1 ] - [ "$output" = "Usage: asdf local " ] + [[ "$output" == "usage: "* ]] } @test "local -p/--parent should emit an error when plugin does not exist" { @@ -204,6 +210,12 @@ teardown() { [ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0" ] } +@test "global prints help if --help is passed" { + run asdf global --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} + @test "[global - dummy_plugin] with latest should use the latest installed version" { run asdf global "dummy" "latest" [ "$status" -eq 0 ] diff --git a/test/where_command.bats b/test/where_command.bats index 491f6c2d0..1824f30a5 100644 --- a/test/where_command.bats +++ b/test/where_command.bats @@ -69,3 +69,9 @@ teardown() { [ "$status" -eq 1 ] [ "$output" = "$expected" ] } + +@test "where prints help if --help is passed" { + run asdf where --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +} diff --git a/test/which_command.bats b/test/which_command.bats index 3dc708a91..8d7ee4a95 100644 --- a/test/which_command.bats +++ b/test/which_command.bats @@ -106,3 +106,9 @@ teardown() { [ "$status" -eq 1 ] [ "$output" = "No dummy executable found for dummy 1.0" ] } + +@test "which prints help if --help is passed" { + run asdf which --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" == 'usage: '* ]] +}