-
Notifications
You must be signed in to change notification settings - Fork 927
chore: remove urlfave/cli built-in -h flag that is causing confusing behavior
#2294
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
Open
Stratus3D
wants to merge
8
commits into
master
Choose a base branch
from
tb/remove-global-h-flag
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−2
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ec01c49
chore: remove urlfave/cli built-in help flag
Stratus3D 6692417
chore: print help when no subcommand specified
Stratus3D 4a9a79e
chore: print help with error message when unknown subcommand specified
Stratus3D cf3d5a2
chore: generate tests for set command
Stratus3D 677213f
chore: remove comments
Stratus3D d16b8e5
chore: fix linter warnings
Stratus3D 703b391
chore: generate tests for set command
Stratus3D 244655c
chore: remove incorrect email address
Stratus3D File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,6 +54,7 @@ func Execute(version string) { | |
| log.SetFlags(0) | ||
|
|
||
| app := &cli.Command{ | ||
| HideHelp: true, | ||
| Name: "asdf", | ||
| Version: version, | ||
| Copyright: "(c) 2024 Trevor Brown", | ||
|
|
@@ -62,6 +63,26 @@ func Execute(version string) { | |
| }, | ||
| Usage: "The multiple runtime version manager", | ||
| UsageText: usageText, | ||
| Action: func(_ context.Context, cmd *cli.Command) error { | ||
| // If no args, show help | ||
| if cmd.Args().Len() == 0 { | ||
| return helpCommand(logger, version, "", "") | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Show asdf's custom help output if no subcommand is specified. |
||
| } | ||
|
|
||
| // Check if first arg matches any command - if so, let urfave/cli handle it | ||
| firstArg := cmd.Args().Get(0) | ||
| for _, c := range cmd.Commands { | ||
| if c.Name == firstArg { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // No matching command - manually invoke CommandNotFound behavior | ||
| logger.Printf("invalid command provided: %s\n\n", firstArg) | ||
| helpCommand(logger, version, "", "") | ||
| cli.OsExiter(1) | ||
| return cli.Exit("", 1) | ||
| }, | ||
| Commands: []*cli.Command{ | ||
| { | ||
| Name: "cmd", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| #!/usr/bin/env bats | ||
|
|
||
| load test_helpers | ||
|
|
||
| setup() { | ||
| setup_asdf_dir | ||
| install_dummy_plugin | ||
| install_dummy_version "1.0.0" | ||
| install_dummy_version "1.1.0" | ||
|
|
||
| PROJECT_DIR="$HOME/project" | ||
| mkdir -p "$PROJECT_DIR" | ||
|
|
||
| cd "$PROJECT_DIR" || exit | ||
| } | ||
|
|
||
| teardown() { | ||
| clean_asdf_dir | ||
| } | ||
|
|
||
| @test "set should emit an error when called with incorrect arity" { | ||
| run asdf set | ||
| [ "$status" -eq 1 ] | ||
| [ "$output" = "tool and version must be provided as arguments" ] | ||
| } | ||
|
|
||
| @test "set should emit an error when version is not provided" { | ||
| run asdf set "dummy" | ||
| [ "$status" -eq 1 ] | ||
| [ "$output" = "version must be provided as an argument" ] | ||
| } | ||
|
|
||
| @test "set should create .tool-versions file in current directory" { | ||
| run asdf set "dummy" "1.0.0" | ||
| [ "$status" -eq 0 ] | ||
| [ -f "$PROJECT_DIR/.tool-versions" ] | ||
| run cat "$PROJECT_DIR/.tool-versions" | ||
| [ "$output" = "dummy 1.0.0" ] | ||
| } | ||
|
|
||
| @test "set should create .tool-versions file in home directory when --home flag is used" { | ||
| run asdf set --home "dummy" "1.0.0" | ||
| [ "$status" -eq 0 ] | ||
| [ -f "$HOME/.tool-versions" ] | ||
| run cat "$HOME/.tool-versions" | ||
| [ "$output" = "dummy 1.0.0" ] | ||
| } | ||
|
|
||
| @test "set -u should be an alias for --home" { | ||
| run asdf set -u "dummy" "1.0.0" | ||
| [ "$status" -eq 0 ] | ||
| [ -f "$HOME/.tool-versions" ] | ||
| run cat "$HOME/.tool-versions" | ||
| [ "$output" = "dummy 1.0.0" ] | ||
| } | ||
|
|
||
| @test "set should update parent directory .tool-versions when --parent flag is used" { | ||
| echo "dummy 1.0.0" >"$PROJECT_DIR/.tool-versions" | ||
|
|
||
| CHILD_DIR="$PROJECT_DIR/child" | ||
| mkdir -p "$CHILD_DIR" | ||
| cd "$CHILD_DIR" | ||
|
|
||
| run asdf set --parent "dummy" "1.1.0" | ||
| [ "$status" -eq 0 ] | ||
| [ -f "$PROJECT_DIR/.tool-versions" ] | ||
| [ ! -f "$CHILD_DIR/.tool-versions" ] | ||
| run cat "$PROJECT_DIR/.tool-versions" | ||
| [ "$output" = "dummy 1.1.0" ] | ||
| } | ||
|
|
||
| @test "set -p should be an alias for --parent" { | ||
| echo "dummy 1.0.0" >"$PROJECT_DIR/.tool-versions" | ||
|
|
||
| CHILD_DIR="$PROJECT_DIR/child" | ||
| mkdir -p "$CHILD_DIR" | ||
| cd "$CHILD_DIR" | ||
|
|
||
| run asdf set -p "dummy" "1.1.0" | ||
| [ "$status" -eq 0 ] | ||
| [ -f "$PROJECT_DIR/.tool-versions" ] | ||
| [ ! -f "$CHILD_DIR/.tool-versions" ] | ||
| run cat "$PROJECT_DIR/.tool-versions" | ||
| [ "$output" = "dummy 1.1.0" ] | ||
| } | ||
|
|
||
| @test "set should emit an error when both --home and --parent flags are used" { | ||
| run asdf set --home --parent "dummy" "1.0.0" | ||
| [ "$status" -eq 1 ] | ||
| [ "$output" = "home and parent flags cannot both be specified; must be one location or the other" ] | ||
| } | ||
|
|
||
| @test "set should emit an error when --parent is used but no .tool-versions file exists in parent directory" { | ||
| CHILD_DIR="$PROJECT_DIR/child" | ||
| mkdir -p "$CHILD_DIR" | ||
| cd "$CHILD_DIR" | ||
|
|
||
| run asdf set --parent "dummy" "1.0.0" | ||
| [ "$status" -eq 1 ] | ||
| [[ "$output" == *"No .tool-versions version file found in parent directory"* ]] | ||
| } | ||
|
|
||
| @test "set with -h flag should show error for undefined flag" { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the above changes this test would fail. |
||
| run asdf set -h "dummy" "1.0.0" | ||
| [ "$status" -eq 1 ] | ||
| [[ "$output" == *"flag provided but not defined: -h"* ]] | ||
| } | ||
|
|
||
| @test "set should support multiple versions" { | ||
| run asdf set "dummy" "1.0.0" "1.1.0" | ||
| [ "$status" -eq 0 ] | ||
| [ -f "$PROJECT_DIR/.tool-versions" ] | ||
| run cat "$PROJECT_DIR/.tool-versions" | ||
| [ "$output" = "dummy 1.0.0 1.1.0" ] | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't generate the global
--help/-hflag or the urlfave/cli generated help command. Our ownhelpsubcommand is better. The-hhelp flag behavior was also confusing in contexts where the user did not intend to request the help output.