Skip to content

Commit

Permalink
Merge pull request #511 from DannyBen/fix/double-dash-passthru
Browse files Browse the repository at this point in the history
Fix input normalization to ignore anything after the double dash (--) operator
  • Loading branch information
DannyBen authored Mar 29, 2024
2 parents 0696ca4 + df22071 commit 0115c89
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
8 changes: 7 additions & 1 deletion examples/catch-all-advanced/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ bashly generate
./cli download -h
./cli download source
./cli download source target
./cli download source target and --additional stuff
./cli download source target --force

# when passing arbitrary arguments that start with a hyphen...
./cli download source target --force -abc --option=value

# ...use the double dash (--) operator to disable input normalization
./cli download source target --force -- -abc --option=value

./cli upload -h
./cli upload
Expand Down
10 changes: 8 additions & 2 deletions lib/bashly/views/command/normalize_input.gtx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
= view_marker

> normalize_input() {
> local arg flags
> local arg flags passthru
> passthru=false
>
> while [[ $# -gt 0 ]]; do
> arg="$1"
> if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
> if [[ $passthru == true ]]; then
> input+=("$arg")
> elif [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
> input+=("${BASH_REMATCH[1]}")
> input+=("${BASH_REMATCH[2]}")
> elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
Expand All @@ -20,6 +23,9 @@ if Settings.compact_short_flags
> done
end

> elif [[ "$arg" == "--" ]]; then
> passthru=true
> input+=("$arg")
> else
> input+=("$arg")
> fi
Expand Down
34 changes: 29 additions & 5 deletions spec/approvals/examples/catch-all-advanced
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,43 @@ args:
args:
- ${args[source]} = source
- ${args[target]} = target
+ ./cli download source target and --additional stuff
+ ./cli download source target --force
# this file is located in 'src/download_command.sh'
# code for 'cli download' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[--force]} = 1
- ${args[source]} = source
- ${args[target]} = target
+ ./cli download source target --force -abc --option=value
# this file is located in 'src/download_command.sh'
# code for 'cli download' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[--force]} = 1
- ${args[source]} = source
- ${args[target]} = target

other_args:
- ${other_args[*]} = -a -b -c --option value
- ${other_args[0]} = -a
- ${other_args[1]} = -b
- ${other_args[2]} = -c
- ${other_args[3]} = --option
- ${other_args[4]} = value
+ ./cli download source target --force -- -abc --option=value
# this file is located in 'src/download_command.sh'
# code for 'cli download' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[--force]} = 1
- ${args[source]} = source
- ${args[target]} = target

other_args:
- ${other_args[*]} = and --additional stuff
- ${other_args[0]} = and
- ${other_args[1]} = --additional
- ${other_args[2]} = stuff
- ${other_args[*]} = -abc --option=value
- ${other_args[0]} = -abc
- ${other_args[1]} = --option=value
+ ./cli upload -h
cli upload - Upload a file

Expand Down

0 comments on commit 0115c89

Please sign in to comment.