Skip to content

Commit

Permalink
Merge pull request #513 from DannyBen/add/conjoined-flag-args-setting
Browse files Browse the repository at this point in the history
Add `conjoined_flag_args` to allow disabling the `--flag=arg` normalization
  • Loading branch information
DannyBen authored Mar 29, 2024
2 parents 57217de + d17756d commit 30189c4
Show file tree
Hide file tree
Showing 19 changed files with 248 additions and 34 deletions.
5 changes: 5 additions & 0 deletions lib/bashly/libraries/settings/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ tab_indent: false
# `-abc` as if it is `-a -b -c`.
compact_short_flags: true

# 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: true

# Set to 'production' or 'development':
# env: production Generate a smaller script, without file markers
# env: development Generate with file markers
Expand Down
5 changes: 5 additions & 0 deletions lib/bashly/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class << self
attr_writer(
:commands_dir,
:compact_short_flags,
:conjoined_flag_args,
:config_path,
:lib_dir,
:partials_extension,
Expand All @@ -24,6 +25,10 @@ def compact_short_flags
@compact_short_flags ||= get :compact_short_flags
end

def conjoined_flag_args
@conjoined_flag_args ||= get :conjoined_flag_args
end

def config_path
@config_path ||= get(:config_path) % { source_dir: source_dir }
end
Expand Down
37 changes: 4 additions & 33 deletions lib/bashly/views/command/normalize_input.gtx
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
> }
>
39 changes: 39 additions & 0 deletions lib/bashly/views/command/normalize_input_function.gtx
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
> }
>
6 changes: 6 additions & 0 deletions lib/bashly/views/command/normalize_input_simple.gtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= view_marker

> normalize_input() {
> input=("$@")
> }
>
8 changes: 7 additions & 1 deletion schemas/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@
},
"compact_short_flags": {
"title": "compact short flags",
"description": "Whether to expand short flags of the current script\nhttps://bashly.dannyb.co/usage/settings/#compact_short_flags",
"description": "Whether to expand -abc to -a -b -c in the input line\nhttps://bashly.dannyb.co/usage/settings/#compact_short_flags",
"type": "boolean",
"default": true
},
"conjoined_flag_args": {
"title": "conjoined flag args",
"description": "Whether to expand --flag=value to --flag value in the input line\nhttps://bashly.dannyb.co/usage/settings/#conjoined_flag_args",
"type": "boolean",
"default": true
},
Expand Down
44 changes: 44 additions & 0 deletions spec/approvals/fixtures/no-conjoined-flag-args
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
42 changes: 42 additions & 0 deletions spec/approvals/fixtures/no-input-normalization
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
1 change: 1 addition & 0 deletions spec/fixtures/workspaces/no-conjoined-flag-args/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cli
2 changes: 2 additions & 0 deletions spec/fixtures/workspaces/no-conjoined-flag-args/README.md
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 spec/fixtures/workspaces/no-conjoined-flag-args/src/bashly.yml
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
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
20 changes: 20 additions & 0 deletions spec/fixtures/workspaces/no-conjoined-flag-args/test.sh
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
1 change: 1 addition & 0 deletions spec/fixtures/workspaces/no-input-normalization/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cli
3 changes: 3 additions & 0 deletions spec/fixtures/workspaces/no-input-normalization/README.md
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).
8 changes: 8 additions & 0 deletions spec/fixtures/workspaces/no-input-normalization/settings.yml
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 spec/fixtures/workspaces/no-input-normalization/src/bashly.yml
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inspect_args
17 changes: 17 additions & 0 deletions spec/fixtures/workspaces/no-input-normalization/test.sh
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

0 comments on commit 30189c4

Please sign in to comment.