Skip to content

Commit

Permalink
Merge pull request #245 from DannyBen/change/invalid-command-behavior
Browse files Browse the repository at this point in the history
Show error instead of usage on invalid command call
  • Loading branch information
DannyBen authored Aug 15, 2022
2 parents 3964452 + fa623e6 commit b975c44
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
4 changes: 4 additions & 0 deletions examples/commands-nested/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ bashly generate
./cli -h
./cli dir
./cli file
# verify invalid commands properly error
./cli dig
./cli dir -h
./cli file -h
./cli dir list
./cli dir list -h
# verify invalid subcommands properly error
./cli dir lost -h
./cli file edit
./cli file edit -h
./cli file edit filename
1 change: 1 addition & 0 deletions lib/bashly/templates/strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ version_flag_text: Show version number
flag_requires_an_argument: "%{name} requires an argument: %{usage}"
invalid_argument: "invalid argument: %s"
invalid_flag: "invalid option: %s"
invalid_command: "invalid command: %s"
conflicting_flags: "conflicting options: %s cannot be used with %s"
missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"
missing_required_flag: "missing required flag: %{usage}"
Expand Down
34 changes: 10 additions & 24 deletions lib/bashly/views/command/command_fallback.gtx
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
= view_marker

> "" )
> {{ function_name }}_usage
> exit 1
> ;;
>
> * )

if default_command
> "" )
> {{ function_name }}_usage
> exit 1
> ;;
>
> * )
> action="{{ default_command.name }}"
> {{ default_command.function_name }}_parse_requirements "$@"
> shift $#
> ;;
>

elsif extensible.is_a? String
> "" )
> {{ function_name }}_usage
> exit 1
> ;;
>
> * )
> if [[ -x "$(command -v "{{ extensible }}")" ]]; then
> exec {{ extensible }} "$@"
> else
> {{ function_name }}_usage
> printf "{{ strings[:invalid_command] }}\n" "$action"
> exit 1
> fi
>

elsif extensible
> "" )
> {{ function_name }}_usage
> exit 1
> ;;
>
> * )
> if [[ -x "$(command -v "{{ function_name }}-$action")" ]]; then
> shift
> exec "{{ function_name }}-$action" "$@"
> else
> {{ function_name }}_usage
> printf "{{ strings[:invalid_command] }}\n" "$action"
> exit 1
> fi
>

else
> * )
> {{ function_name }}_usage
> printf "{{ strings[:invalid_command] }}\n" "$action"
> exit 1
> ;;
>

end


4 changes: 4 additions & 0 deletions spec/approvals/examples/commands-nested
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Commands:
show Show file contents
edit Edit the file

+ ./cli dig
invalid command: dig
+ ./cli dir -h
cli dir - Directory commands

Expand Down Expand Up @@ -116,6 +118,8 @@ Arguments:
PATH
Directory path

+ ./cli dir lost -h
invalid command: lost
+ ./cli file edit
missing required argument: PATH
usage: cli file edit PATH
Expand Down
9 changes: 8 additions & 1 deletion spec/approvals/fixtures/version-in-subcommands
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ created ./cli
run ./cli --help to test your bash script
+ ./cli --version
1.2.3
+ ./cli help
+ ./cli --help
cli - Sample application

Usage:
Expand All @@ -17,6 +17,13 @@ Usage:
Commands:
git Delegate to git

Options:
--help, -h
Show this help

--version, -v
Show version number

+ ./cli git --version
# this file is located in 'src/git_command.sh'
# code for 'cli git' goes here
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/workspaces/version-in-subcommands/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ set -x
bundle exec bashly generate

./cli --version
./cli help
./cli --help
./cli git --version
./cli git any "other args" -or --flags

0 comments on commit b975c44

Please sign in to comment.