Skip to content

Commit d2a437e

Browse files
authored
Merge pull request #375 from DannyBen/fix/short-flag
Fix `default` and `required` for flags with a `short` name only
2 parents 349bbb7 + 3e4cd6d commit d2a437e

File tree

7 files changed

+49
-2
lines changed

7 files changed

+49
-2
lines changed

lib/bashly/views/command/default_assignments.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if default_args.any? or default_flags.any?
66
end
77

88
default_flags.each do |flag|
9-
> [[ -n ${args['{{ flag.long }}']:-} ]] || args['{{ flag.long }}']="{{ flag.default }}"
9+
> [[ -n ${args['{{ flag.name }}']:-} ]] || args['{{ flag.name }}']="{{ flag.default }}"
1010
end
1111

1212
>

lib/bashly/views/command/required_flags_filter.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ if required_flags.any?
22
= view_marker
33

44
required_flags.each do |flag|
5-
> if [[ -z ${args['{{ flag.long }}']+x} ]]; then
5+
> if [[ -z ${args['{{ flag.name }}']+x} ]]; then
66
> printf "{{ strings[:missing_required_flag] % { usage: flag.usage_string } }}\n" >&2
77
> exit 1
88
> fi

spec/approvals/fixtures/short-flag

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
+ bundle exec bashly generate
2+
creating user files in src
3+
created src/root_command.sh
4+
created ./cli
5+
run ./cli --help to test your bash script
6+
+ ./cli
7+
missing required flag: -o PATH
8+
+ ./cli -o /tmp
9+
# this file is located in 'src/root_command.sh'
10+
# you can edit it freely and regenerate (it will not be overwritten)
11+
args:
12+
- ${args[-f]} = html
13+
- ${args[-o]} = /tmp
14+
+ ./cli -o /tmp -f json
15+
# this file is located in 'src/root_command.sh'
16+
# you can edit it freely and regenerate (it will not be overwritten)
17+
args:
18+
- ${args[-f]} = json
19+
- ${args[-o]} = /tmp
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cli
2+
src/*.sh
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This fixture tests that short flags default and required work properly
2+
Reference issue: https://github.com/DannyBen/bashly/issues/373
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: cli
2+
3+
flags:
4+
- short: -f
5+
arg: format
6+
help: Set format
7+
default: html
8+
9+
- short: -o
10+
arg: path
11+
help: Output to path
12+
required: true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f ./src/*.sh
4+
rm -f ./cli
5+
6+
set -x
7+
8+
bundle exec bashly generate
9+
10+
./cli
11+
./cli -o /tmp
12+
./cli -o /tmp -f json

0 commit comments

Comments
 (0)