Skip to content

Commit

Permalink
- Add support for unique in repeatable flag args
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Dec 7, 2023
1 parent 039ab1c commit b062c93
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 36 deletions.
2 changes: 1 addition & 1 deletion examples/render-mandoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ ISSUE TRACKER
AUTHORS
Lana Lang.

Version 0.1.0 November 2023 download(1)
Version 0.1.0 December 2023 download(1)


````
Expand Down
27 changes: 1 addition & 26 deletions examples/render-mandoc/docs/download.1
Original file line number Diff line number Diff line change
@@ -1,60 +1,35 @@
.\" Automatically generated by Pandoc 3.1.6
.\" Automatically generated by Pandoc 3.1.9
.\"
.\" Define V font for inline verbatim, using C font in formats
.\" that render this, and otherwise B font.
.ie "\f[CB]x\f[]"x" \{\
. ftr V B
. ftr VI BI
. ftr VB B
. ftr VBI BI
.\}
.el \{\
. ftr V CR
. ftr VI CI
. ftr VB CB
. ftr VBI CBI
.\}
.TH "download" "1" "December 2023" "Version 0.1.0" "Sample application"
.hy
.SH NAME
.PP
\f[B]download\f[R] - Sample application
.SH SYNOPSIS
.PP
\f[B]download\f[R] SOURCE [TARGET...]
OPTIONS
.SH DESCRIPTION
.PP
Sample application
.SH ARGUMENTS
.SS SOURCE
.PP
Source to download from
.IP \[bu] 2
\f[I]Required\f[R]
.IP \[bu] 2
Allowed Values: \f[B]server1, server2\f[R]
.SS TARGET
.PP
Target filename (default: same as source)
.IP \[bu] 2
\f[I]Repeatable\f[R]
.SH OPTIONS
.SS --force, -f
.PP
Overwrite existing files
.SS --debug, -d
.PP
Show debug information
.SH DEPENDENCIES
.SS aws-cli
.PP
Download from <https://aws.amazon.com/cli/>
.SH SEE ALSO
.PP
\f[B]docker\f[R](1), \f[B]docker-compose.yml\f[R](5)
.SH ISSUE TRACKER
.PP
Report issues at <https://github.com/lanalang/smallville>
.SH AUTHORS
Lana Lang.
31 changes: 30 additions & 1 deletion examples/repeatable-flag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ flags:
# needs to be converted to an array with `eval "data=(${args[--data]})"`
repeatable: true

- long: --path
short: -p
arg: location
help: Specify one or more paths
repeatable: true

# Setting this to true will ignore repeating arguments that are not unique
unique: true

- long: --verbose
short: -v
help: Set verbosity level
Expand All @@ -47,6 +56,7 @@ flags:

examples:
- download -d one -d "two three" -vvv
- download -d one -p /usr/bin -p /tmp
````

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

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

--path, -p LOCATION (repeatable)
Specify one or more paths

--verbose, -v (repeatable)
Set verbosity level

Expand All @@ -98,6 +111,7 @@ Options:

Examples:
download -d one -d "two three" -vvv
download -d one -p /usr/bin -p /tmp



Expand All @@ -119,5 +133,20 @@ args:

````

### `$ ./download -d one --path /bin --path /usr/lib --path /bin`

````shell
Data elements:
one

Verbosity level: 1

args:
- ${args[--data]} = "one"
- ${args[--path]} = "/bin" "/usr/lib"


````



11 changes: 10 additions & 1 deletion examples/repeatable-flag/src/bashly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ flags:
# needs to be converted to an array with `eval "data=(${args[--data]})"`
repeatable: true

- long: --path
short: -p
arg: location
help: Specify one or more paths
repeatable: true

# Setting this to true will ignore repeating arguments that are not unique
unique: true

- long: --verbose
short: -v
help: Set verbosity level
Expand All @@ -26,4 +35,4 @@ flags:

examples:
- download -d one -d "two three" -vvv

- download -d one -p /usr/bin -p /tmp
2 changes: 1 addition & 1 deletion examples/repeatable-flag/src/root_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ for i in "${data[@]}"; do
done

# The --verbose arg will contain the number of times it was used by the user
verbose=${args[--verbose]}
verbose=${args[--verbose]:-1}
echo ""
echo "Verbosity level: $verbose"
echo ""
Expand Down
1 change: 1 addition & 0 deletions examples/repeatable-flag/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ bashly generate

./download -h
./download -d one -d "two three" -vvv
./download -d one --path /bin --path /usr/lib --path /bin
6 changes: 6 additions & 0 deletions lib/bashly/config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ def assert_flag(key, value)
if value['completions']
assert value['arg'], "#{key}.completions does not make sense without nub`arg`"
end

if value['unique']
condition = value['arg'] && value['repeatable']
assert condition, "#{key}.unique does not make sense without nub`arg` and nub`repeatable`"
end

end

def assert_env_var(key, value)
Expand Down
11 changes: 11 additions & 0 deletions lib/bashly/docs/flag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ flag.short:
arg: name
help: Repository user name
flag.unique:
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.
url: https://bashly.dannyb.co/configuration/flag/#unique
example: |-
flags:
- long: --path
arg: location
help: Set one or more paths
repeatable: true
unique: true
flag.validate:
help: Apply custom validation functions. Must be accompanied by `arg`.

Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/script/flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class << self
def option_keys
@option_keys ||= %i[
allowed arg completions conflicts default help long repeatable
required short validate private
required short unique validate private
]
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/bashly/views/flag/case_arg.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
if repeatable
> if [[ -z ${args['{{ name }}']+x} ]]; then
> args['{{ name }}']="\"$2\""
> else
> args['{{ name }}']="${args[{{ name }}]} \"$2\""
if unique
> elif [[ ! "${args['{{ name }}']}" =~ "\"$2\"" ]]; then
else
> else
end
> args['{{ name }}']="${args['{{ name }}']} \"$2\""
> fi

else
Expand Down
14 changes: 14 additions & 0 deletions spec/approvals/cli/doc/full
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,20 @@ flag.short

See https://bashly.dannyb.co/configuration/flag/#short

flag.unique

Specify that the arguments provided by this repeatable flag must be unique.
When this is set to true, non-unique values will be ignored.

flags:
- long: --path
arg: location
help: Set one or more paths
repeatable: true
unique: true

See https://bashly.dannyb.co/configuration/flag/#unique

flag.validate

Apply custom validation functions. Must be accompanied by arg.
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/cli/doc/index
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ flag.private
flag.repeatable
flag.required
flag.short
flag.unique
flag.validate
13 changes: 13 additions & 0 deletions spec/approvals/examples/repeatable-flag
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Options:
--data, -d DATA (required) (repeatable)
Provide data values

--path, -p LOCATION (repeatable)
Specify one or more paths

--verbose, -v (repeatable)
Set verbosity level

Expand All @@ -26,6 +29,7 @@ Options:

Examples:
download -d one -d "two three" -vvv
download -d one -p /usr/bin -p /tmp

+ ./download -d one -d 'two three' -vvv
Data elements:
Expand All @@ -37,3 +41,12 @@ Verbosity level: 3
args:
- ${args[--data]} = "one" "two three"
- ${args[--verbose]} = 3
+ ./download -d one --path /bin --path /usr/lib --path /bin
Data elements:
one

Verbosity level: 1

args:
- ${args[--data]} = "one"
- ${args[--path]} = "/bin" "/usr/lib"
2 changes: 0 additions & 2 deletions spec/approvals/libraries/render/mandoc/render-1-download.1
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ OPTIONS
Overwrite existing files

EXAMPLES

download example.com

download example.com ./output -f


Version 0.1.0 MONTH YEAR ... APPNAME
1 change: 1 addition & 0 deletions spec/approvals/libraries_validation/libraries
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nothing raised
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#<Bashly::ConfigurationError: root.flags[0].unique does not make sense without nub`arg` and nub`repeatable`>
2 changes: 1 addition & 1 deletion spec/bashly/library_source_config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe LibrarySourceConfig do
fixtures = Dir['spec/fixtures/libraries_errors/*.yml']
fixtures = Dir['spec/fixtures/libraries/*.yml']
subject { described_class.new path }

let(:path) { 'spec/fixtures/libraries/libraries.yml' }
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/script/validations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@
allowed: [one, two]
completions: [<file>]

:flag_unique_without_repeatable_and_arg:
name: invalid
help: flag must have an arg and be repeatable when using unique
flags:
- long: --ssh
repeatable: true
unique: true

:root_alias:
name: invalid
help: root cannot have alias
Expand Down

0 comments on commit b062c93

Please sign in to comment.