-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #513 from DannyBen/add/conjoined-flag-args-setting
Add `conjoined_flag_args` to allow disabling the `--flag=arg` normalization
- Loading branch information
Showing
19 changed files
with
248 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,7 @@ | ||
= view_marker | ||
|
||
> normalize_input() { | ||
> local arg flags passthru | ||
> passthru=false | ||
> | ||
> while [[ $# -gt 0 ]]; do | ||
> arg="$1" | ||
> 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 | ||
> input+=("${BASH_REMATCH[1]}") | ||
> input+=("${BASH_REMATCH[2]}") | ||
|
||
if Settings.compact_short_flags | ||
> elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then | ||
> flags="${BASH_REMATCH[1]}" | ||
> for ((i = 0; i < ${#flags}; i++)); do | ||
> input+=("-${flags:i:1}") | ||
> done | ||
if Settings.compact_short_flags || Settings.conjoined_flag_args | ||
= render :normalize_input_function | ||
else | ||
= render :normalize_input_simple | ||
end | ||
|
||
> elif [[ "$arg" == "--" ]]; then | ||
> passthru=true | ||
> input+=("$arg") | ||
> else | ||
> input+=("$arg") | ||
> fi | ||
> | ||
> shift | ||
> done | ||
> } | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
= view_marker | ||
|
||
> normalize_input() { | ||
> local arg flags passthru | ||
> passthru=false | ||
> | ||
> while [[ $# -gt 0 ]]; do | ||
> arg="$1" | ||
> if [[ $passthru == true ]]; then | ||
> input+=("$arg") | ||
|
||
if Settings.conjoined_flag_args | ||
> elif [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then | ||
> input+=("${BASH_REMATCH[1]}") | ||
> input+=("${BASH_REMATCH[2]}") | ||
> elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then | ||
> input+=("${BASH_REMATCH[1]}") | ||
> input+=("${BASH_REMATCH[2]}") | ||
end | ||
|
||
if Settings.compact_short_flags | ||
> elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then | ||
> flags="${BASH_REMATCH[1]}" | ||
> for ((i = 0; i < ${#flags}; i++)); do | ||
> input+=("-${flags:i:1}") | ||
> done | ||
end | ||
|
||
> elif [[ "$arg" == "--" ]]; then | ||
> passthru=true | ||
> input+=("$arg") | ||
> else | ||
> input+=("$arg") | ||
> fi | ||
> | ||
> shift | ||
> done | ||
> } | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
= view_marker | ||
|
||
> normalize_input() { | ||
> input=("$@") | ||
> } | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
creating user files in src | ||
skipped src/root_command.sh (exists) | ||
created ./cli | ||
run ./cli --help to test your bash script | ||
+ ./cli --user=admin -p=secret -- --region=us-east-1 -static | ||
# this file is located in 'src/root_command.sh' | ||
# you can edit it freely and regenerate (it will not be overwritten) | ||
args: | ||
- ${args[--password]} = secret | ||
- ${args[--user]} = admin | ||
|
||
other_args: | ||
- ${other_args[*]} = --region=us-east-1 -static | ||
- ${other_args[0]} = --region=us-east-1 | ||
- ${other_args[1]} = -static | ||
+ BASHLY_CONJOINED_FLAG_ARGS=no | ||
+ bundle exec bashly generate | ||
creating user files in src | ||
skipped src/root_command.sh (exists) | ||
created ./cli | ||
run ./cli --help to test your bash script | ||
+ ./cli --user admin -p secret --region=us-east-1 | ||
# this file is located in 'src/root_command.sh' | ||
# you can edit it freely and regenerate (it will not be overwritten) | ||
args: | ||
- ${args[--password]} = secret | ||
- ${args[--user]} = admin | ||
|
||
other_args: | ||
- ${other_args[*]} = --region=us-east-1 | ||
- ${other_args[0]} = --region=us-east-1 | ||
+ ./cli --user admin -p secret -- --region=us-east-1 -static | ||
# this file is located in 'src/root_command.sh' | ||
# you can edit it freely and regenerate (it will not be overwritten) | ||
args: | ||
- ${args[--password]} = secret | ||
- ${args[--user]} = admin | ||
|
||
other_args: | ||
- ${other_args[*]} = --region=us-east-1 -static | ||
- ${other_args[0]} = --region=us-east-1 | ||
- ${other_args[1]} = -static | ||
+ ./cli --user=admin -p=secret -- --region=us-east-1 -static | ||
missing required flag: --user, -u NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
creating user files in src | ||
skipped src/root_command.sh (exists) | ||
created ./cli | ||
run ./cli --help to test your bash script | ||
+ echo '=== bad - since conjoined_flag_args is disabled' | ||
=== bad - since conjoined_flag_args is disabled | ||
+ ./cli --user=admin | ||
missing required flag: --user, -u NAME | ||
+ echo '=== bad - since compact_short_flags is disabled (-fd will be considered as catch_all)' | ||
=== bad - since compact_short_flags is disabled (-fd will be considered as catch_all) | ||
+ ./cli --user admin --password secret -fd | ||
args: | ||
- ${args[--password]} = secret | ||
- ${args[--user]} = admin | ||
|
||
other_args: | ||
- ${other_args[*]} = -fd | ||
- ${other_args[0]} = -fd | ||
+ echo '=== good' | ||
=== good | ||
+ ./cli --user admin -p secret -f -d --region=us-east-1 -static | ||
args: | ||
- ${args[--debug]} = 1 | ||
- ${args[--force]} = 1 | ||
- ${args[--password]} = secret | ||
- ${args[--user]} = admin | ||
|
||
other_args: | ||
- ${other_args[*]} = --region=us-east-1 -static | ||
- ${other_args[0]} = --region=us-east-1 | ||
- ${other_args[1]} = -static | ||
+ echo '=== good' | ||
=== good | ||
+ ./cli --user admin -p secret -- --region=us-east-1 -static | ||
args: | ||
- ${args[--password]} = secret | ||
- ${args[--user]} = admin | ||
|
||
other_args: | ||
- ${other_args[*]} = --region=us-east-1 -static | ||
- ${other_args[0]} = --region=us-east-1 | ||
- ${other_args[1]} = -static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This fixture tests that setting `conjoined_flag_args` to `false` indeed ignores | ||
this `--flag=value` and `-f=value` patterns. |
17 changes: 17 additions & 0 deletions
17
spec/fixtures/workspaces/no-conjoined-flag-args/src/bashly.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: cli | ||
help: Sample application | ||
version: 0.1.0 | ||
|
||
catch_all: Server Params | ||
|
||
flags: | ||
- long: --user | ||
short: -u | ||
arg: name | ||
required: true | ||
help: Protocol | ||
- long: --password | ||
short: -p | ||
arg: name | ||
required: true | ||
help: User |
3 changes: 3 additions & 0 deletions
3
spec/fixtures/workspaces/no-conjoined-flag-args/src/root_command.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
echo "# this file is located in 'src/root_command.sh'" | ||
echo "# you can edit it freely and regenerate (it will not be overwritten)" | ||
inspect_args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
|
||
bundle exec bashly generate | ||
|
||
set -x | ||
|
||
# good | ||
./cli --user=admin -p=secret -- --region=us-east-1 -static | ||
|
||
# Use ad-hoc setting through an environment variable | ||
BASHLY_CONJOINED_FLAG_ARGS="no" bundle exec bashly generate | ||
|
||
# good - since conjoined flags is disabled | ||
./cli --user admin -p secret --region=us-east-1 | ||
|
||
# good - since using -- | ||
./cli --user admin -p secret -- --region=us-east-1 -static | ||
|
||
# bad - since using --flag=value | ||
./cli --user=admin -p=secret -- --region=us-east-1 -static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This fixture tests that disabling both `conjoined_flag_args` and | ||
`compact_short_flags`, the resulting script uses simple normalization function | ||
(i.e., no normalization at all - just input array assignment). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# When true, the generated script will consider any argument in the form of | ||
# `-abc` as if it is `-a -b -c`. | ||
compact_short_flags: false | ||
|
||
# When true, the generated script will consider any argument in the form of | ||
# `--flag=value` and `-f=value` as if it is `--flag value` and `-f value` | ||
# respectively. | ||
conjoined_flag_args: false |
23 changes: 23 additions & 0 deletions
23
spec/fixtures/workspaces/no-input-normalization/src/bashly.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: cli | ||
help: Sample application to demonstrate the input normalization settings | ||
version: 0.1.0 | ||
|
||
catch_all: Server Params | ||
|
||
flags: | ||
- long: --user | ||
short: -u | ||
arg: name | ||
required: true | ||
help: Protocol | ||
- long: --password | ||
short: -p | ||
arg: name | ||
required: true | ||
help: User | ||
- long: --debug | ||
short: -d | ||
help: Print debug information | ||
- long: --force | ||
short: -f | ||
help: Overwrite existing data |
1 change: 1 addition & 0 deletions
1
spec/fixtures/workspaces/no-input-normalization/src/root_command.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
inspect_args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
bundle exec bashly generate | ||
|
||
set -x | ||
|
||
echo "=== bad - since conjoined_flag_args is disabled" | ||
./cli --user=admin | ||
|
||
echo "=== bad - since compact_short_flags is disabled (-fd will be considered as catch_all)" | ||
./cli --user admin --password secret -fd | ||
|
||
echo "=== good" | ||
./cli --user admin -p secret -f -d --region=us-east-1 -static | ||
|
||
echo "=== good" | ||
./cli --user admin -p secret -- --region=us-east-1 -static |