Skip to content

Commit 12b8cdd

Browse files
authored
Merge pull request #454 from DannyBen/add/unique-repeatable-flag
Add support for `unique` in repeatable flag args
2 parents 039ab1c + c528db0 commit 12b8cdd

File tree

25 files changed

+114
-44
lines changed

25 files changed

+114
-44
lines changed

examples/render-mandoc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ ISSUE TRACKER
102102
AUTHORS
103103
Lana Lang.
104104

105-
Version 0.1.0 November 2023 download(1)
105+
Version 0.1.0 December 2023 download(1)
106106

107107

108108
````
Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,35 @@
1-
.\" Automatically generated by Pandoc 3.1.6
1+
.\" Automatically generated by Pandoc 3.1.9
22
.\"
3-
.\" Define V font for inline verbatim, using C font in formats
4-
.\" that render this, and otherwise B font.
5-
.ie "\f[CB]x\f[]"x" \{\
6-
. ftr V B
7-
. ftr VI BI
8-
. ftr VB B
9-
. ftr VBI BI
10-
.\}
11-
.el \{\
12-
. ftr V CR
13-
. ftr VI CI
14-
. ftr VB CB
15-
. ftr VBI CBI
16-
.\}
173
.TH "download" "1" "December 2023" "Version 0.1.0" "Sample application"
18-
.hy
194
.SH NAME
20-
.PP
215
\f[B]download\f[R] - Sample application
226
.SH SYNOPSIS
23-
.PP
247
\f[B]download\f[R] SOURCE [TARGET...]
258
OPTIONS
269
.SH DESCRIPTION
27-
.PP
2810
Sample application
2911
.SH ARGUMENTS
3012
.SS SOURCE
31-
.PP
3213
Source to download from
3314
.IP \[bu] 2
3415
\f[I]Required\f[R]
3516
.IP \[bu] 2
3617
Allowed Values: \f[B]server1, server2\f[R]
3718
.SS TARGET
38-
.PP
3919
Target filename (default: same as source)
4020
.IP \[bu] 2
4121
\f[I]Repeatable\f[R]
4222
.SH OPTIONS
4323
.SS --force, -f
44-
.PP
4524
Overwrite existing files
4625
.SS --debug, -d
47-
.PP
4826
Show debug information
4927
.SH DEPENDENCIES
5028
.SS aws-cli
51-
.PP
5229
Download from <https://aws.amazon.com/cli/>
5330
.SH SEE ALSO
54-
.PP
5531
\f[B]docker\f[R](1), \f[B]docker-compose.yml\f[R](5)
5632
.SH ISSUE TRACKER
57-
.PP
5833
Report issues at <https://github.com/lanalang/smallville>
5934
.SH AUTHORS
6035
Lana Lang.

examples/repeatable-flag/README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ flags:
3636
# needs to be converted to an array with `eval "data=(${args[--data]})"`
3737
repeatable: true
3838

39+
- long: --path
40+
short: -p
41+
arg: location
42+
help: Specify one or more paths
43+
repeatable: true
44+
45+
# Setting this to true will ignore repeating arguments that are not unique
46+
unique: true
47+
3948
- long: --verbose
4049
short: -v
4150
help: Set verbosity level
@@ -47,6 +56,7 @@ flags:
4756

4857
examples:
4958
- download -d one -d "two three" -vvv
59+
- download -d one -p /usr/bin -p /tmp
5060
````
5161

5262
## `src/root_command.sh`
@@ -61,7 +71,7 @@ for i in "${data[@]}"; do
6171
done
6272

6373
# The --verbose arg will contain the number of times it was used by the user
64-
verbose=${args[--verbose]}
74+
verbose=${args[--verbose]:-1}
6575
echo ""
6676
echo "Verbosity level: $verbose"
6777
echo ""
@@ -87,6 +97,9 @@ Options:
8797
--data, -d DATA (required) (repeatable)
8898
Provide data values
8999

100+
--path, -p LOCATION (repeatable)
101+
Specify one or more paths
102+
90103
--verbose, -v (repeatable)
91104
Set verbosity level
92105

@@ -98,6 +111,7 @@ Options:
98111

99112
Examples:
100113
download -d one -d "two three" -vvv
114+
download -d one -p /usr/bin -p /tmp
101115

102116

103117

@@ -119,5 +133,20 @@ args:
119133

120134
````
121135

136+
### `$ ./download -d one --path /bin --path /usr/lib --path /bin`
137+
138+
````shell
139+
Data elements:
140+
one
141+
142+
Verbosity level: 1
143+
144+
args:
145+
- ${args[--data]} = "one"
146+
- ${args[--path]} = "/bin" "/usr/lib"
147+
148+
149+
````
150+
122151

123152

examples/repeatable-flag/src/bashly.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ flags:
1515
# needs to be converted to an array with `eval "data=(${args[--data]})"`
1616
repeatable: true
1717

18+
- long: --path
19+
short: -p
20+
arg: location
21+
help: Specify one or more paths
22+
repeatable: true
23+
24+
# Setting this to true will ignore repeating arguments that are not unique
25+
unique: true
26+
1827
- long: --verbose
1928
short: -v
2029
help: Set verbosity level
@@ -26,4 +35,4 @@ flags:
2635

2736
examples:
2837
- download -d one -d "two three" -vvv
29-
38+
- download -d one -p /usr/bin -p /tmp

examples/repeatable-flag/src/root_command.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ for i in "${data[@]}"; do
77
done
88

99
# The --verbose arg will contain the number of times it was used by the user
10-
verbose=${args[--verbose]}
10+
verbose=${args[--verbose]:-1}
1111
echo ""
1212
echo "Verbosity level: $verbose"
1313
echo ""

examples/repeatable-flag/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ bashly generate
88

99
./download -h
1010
./download -d one -d "two three" -vvv
11+
./download -d one --path /bin --path /usr/lib --path /bin

lib/bashly/config_validator.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def assert_flag(key, value)
118118

119119
assert_boolean "#{key}.private", value['private']
120120
assert_boolean "#{key}.repeatable", value['repeatable']
121+
assert_boolean "#{key}.unique", value['unique']
121122
assert_boolean "#{key}.required", value['required']
122123
assert_array "#{key}.allowed", value['allowed'], of: :string
123124
assert_array "#{key}.conflicts", value['conflicts'], of: :string
@@ -140,6 +141,11 @@ def assert_flag(key, value)
140141
if value['completions']
141142
assert value['arg'], "#{key}.completions does not make sense without nub`arg`"
142143
end
144+
145+
if value['unique']
146+
condition = value['arg'] && value['repeatable']
147+
assert condition, "#{key}.unique does not make sense without nub`arg` and nub`repeatable`"
148+
end
143149
end
144150

145151
def assert_env_var(key, value)

lib/bashly/docs/flag.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ flag.short:
162162
arg: name
163163
help: Repository user name
164164
165+
flag.unique:
166+
help: Specify that the arguments provided by this repeatable flag must be unique. When this is set to `true`, non-unique values will be ignored.
167+
url: https://bashly.dannyb.co/configuration/flag/#unique
168+
example: |-
169+
flags:
170+
- long: --path
171+
arg: location
172+
help: Set one or more paths
173+
repeatable: true
174+
unique: true
175+
165176
flag.validate:
166177
help: Apply custom validation functions. Must be accompanied by `arg`.
167178

lib/bashly/script/flag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class << self
77
def option_keys
88
@option_keys ||= %i[
99
allowed arg completions conflicts default help long repeatable
10-
required short validate private
10+
required short unique validate private
1111
]
1212
end
1313
end

lib/bashly/views/flag/case_arg.gtx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
if repeatable
77
> if [[ -z ${args['{{ name }}']+x} ]]; then
88
> args['{{ name }}']="\"$2\""
9-
> else
10-
> args['{{ name }}']="${args[{{ name }}]} \"$2\""
9+
if unique
10+
> elif [[ ! "${args['{{ name }}']}" =~ \"$2\" ]]; then
11+
else
12+
> else
13+
end
14+
> args['{{ name }}']="${args['{{ name }}']} \"$2\""
1115
> fi
1216

1317
else

0 commit comments

Comments
 (0)