Skip to content

Commit b975c44

Browse files
authored
Merge pull request #245 from DannyBen/change/invalid-command-behavior
Show error instead of usage on invalid command call
2 parents 3964452 + fa623e6 commit b975c44

File tree

6 files changed

+28
-26
lines changed

6 files changed

+28
-26
lines changed

examples/commands-nested/test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ bashly generate
1212
./cli -h
1313
./cli dir
1414
./cli file
15+
# verify invalid commands properly error
16+
./cli dig
1517
./cli dir -h
1618
./cli file -h
1719
./cli dir list
1820
./cli dir list -h
21+
# verify invalid subcommands properly error
22+
./cli dir lost -h
1923
./cli file edit
2024
./cli file edit -h
2125
./cli file edit filename

lib/bashly/templates/strings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ version_flag_text: Show version number
2626
flag_requires_an_argument: "%{name} requires an argument: %{usage}"
2727
invalid_argument: "invalid argument: %s"
2828
invalid_flag: "invalid option: %s"
29+
invalid_command: "invalid command: %s"
2930
conflicting_flags: "conflicting options: %s cannot be used with %s"
3031
missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"
3132
missing_required_flag: "missing required flag: %{usage}"
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,42 @@
11
= view_marker
22

3+
> "" )
4+
> {{ function_name }}_usage
5+
> exit 1
6+
> ;;
7+
>
8+
> * )
9+
310
if default_command
4-
> "" )
5-
> {{ function_name }}_usage
6-
> exit 1
7-
> ;;
8-
>
9-
> * )
1011
> action="{{ default_command.name }}"
1112
> {{ default_command.function_name }}_parse_requirements "$@"
1213
> shift $#
1314
> ;;
1415
>
1516

1617
elsif extensible.is_a? String
17-
> "" )
18-
> {{ function_name }}_usage
19-
> exit 1
20-
> ;;
21-
>
22-
> * )
2318
> if [[ -x "$(command -v "{{ extensible }}")" ]]; then
2419
> exec {{ extensible }} "$@"
2520
> else
26-
> {{ function_name }}_usage
21+
> printf "{{ strings[:invalid_command] }}\n" "$action"
2722
> exit 1
2823
> fi
2924
>
3025

3126
elsif extensible
32-
> "" )
33-
> {{ function_name }}_usage
34-
> exit 1
35-
> ;;
36-
>
37-
> * )
3827
> if [[ -x "$(command -v "{{ function_name }}-$action")" ]]; then
3928
> shift
4029
> exec "{{ function_name }}-$action" "$@"
4130
> else
42-
> {{ function_name }}_usage
31+
> printf "{{ strings[:invalid_command] }}\n" "$action"
4332
> exit 1
4433
> fi
4534
>
4635

4736
else
48-
> * )
49-
> {{ function_name }}_usage
37+
> printf "{{ strings[:invalid_command] }}\n" "$action"
5038
> exit 1
5139
> ;;
5240
>
5341

5442
end
55-
56-

spec/approvals/examples/commands-nested

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Commands:
6464
show Show file contents
6565
edit Edit the file
6666

67+
+ ./cli dig
68+
invalid command: dig
6769
+ ./cli dir -h
6870
cli dir - Directory commands
6971

@@ -116,6 +118,8 @@ Arguments:
116118
PATH
117119
Directory path
118120

121+
+ ./cli dir lost -h
122+
invalid command: lost
119123
+ ./cli file edit
120124
missing required argument: PATH
121125
usage: cli file edit PATH

spec/approvals/fixtures/version-in-subcommands

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ created ./cli
66
run ./cli --help to test your bash script
77
+ ./cli --version
88
1.2.3
9-
+ ./cli help
9+
+ ./cli --help
1010
cli - Sample application
1111

1212
Usage:
@@ -17,6 +17,13 @@ Usage:
1717
Commands:
1818
git Delegate to git
1919

20+
Options:
21+
--help, -h
22+
Show this help
23+
24+
--version, -v
25+
Show version number
26+
2027
+ ./cli git --version
2128
# this file is located in 'src/git_command.sh'
2229
# code for 'cli git' goes here

spec/fixtures/workspaces/version-in-subcommands/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ set -x
77
bundle exec bashly generate
88

99
./cli --version
10-
./cli help
10+
./cli --help
1111
./cli git --version
1212
./cli git any "other args" -or --flags

0 commit comments

Comments
 (0)