Skip to content

Commit 30189c4

Browse files
authored
Merge pull request #513 from DannyBen/add/conjoined-flag-args-setting
Add `conjoined_flag_args` to allow disabling the `--flag=arg` normalization
2 parents 57217de + d17756d commit 30189c4

File tree

19 files changed

+248
-34
lines changed

19 files changed

+248
-34
lines changed

lib/bashly/libraries/settings/settings.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ tab_indent: false
4242
# `-abc` as if it is `-a -b -c`.
4343
compact_short_flags: true
4444

45+
# When true, the generated script will consider any argument in the form of
46+
# `--flag=value` and `-f=value` as if it is `--flag value` and `-f value`
47+
# respectively.
48+
conjoined_flag_args: true
49+
4550
# Set to 'production' or 'development':
4651
# env: production Generate a smaller script, without file markers
4752
# env: development Generate with file markers

lib/bashly/settings.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class << self
66
attr_writer(
77
:commands_dir,
88
:compact_short_flags,
9+
:conjoined_flag_args,
910
:config_path,
1011
:lib_dir,
1112
:partials_extension,
@@ -24,6 +25,10 @@ def compact_short_flags
2425
@compact_short_flags ||= get :compact_short_flags
2526
end
2627

28+
def conjoined_flag_args
29+
@conjoined_flag_args ||= get :conjoined_flag_args
30+
end
31+
2732
def config_path
2833
@config_path ||= get(:config_path) % { source_dir: source_dir }
2934
end
Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,7 @@
11
= view_marker
22

3-
> normalize_input() {
4-
> local arg flags passthru
5-
> passthru=false
6-
>
7-
> while [[ $# -gt 0 ]]; do
8-
> arg="$1"
9-
> if [[ $passthru == true ]]; then
10-
> input+=("$arg")
11-
> elif [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
12-
> input+=("${BASH_REMATCH[1]}")
13-
> input+=("${BASH_REMATCH[2]}")
14-
> elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
15-
> input+=("${BASH_REMATCH[1]}")
16-
> input+=("${BASH_REMATCH[2]}")
17-
18-
if Settings.compact_short_flags
19-
> elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
20-
> flags="${BASH_REMATCH[1]}"
21-
> for ((i = 0; i < ${#flags}; i++)); do
22-
> input+=("-${flags:i:1}")
23-
> done
3+
if Settings.compact_short_flags || Settings.conjoined_flag_args
4+
= render :normalize_input_function
5+
else
6+
= render :normalize_input_simple
247
end
25-
26-
> elif [[ "$arg" == "--" ]]; then
27-
> passthru=true
28-
> input+=("$arg")
29-
> else
30-
> input+=("$arg")
31-
> fi
32-
>
33-
> shift
34-
> done
35-
> }
36-
>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
= view_marker
2+
3+
> normalize_input() {
4+
> local arg flags passthru
5+
> passthru=false
6+
>
7+
> while [[ $# -gt 0 ]]; do
8+
> arg="$1"
9+
> if [[ $passthru == true ]]; then
10+
> input+=("$arg")
11+
12+
if Settings.conjoined_flag_args
13+
> elif [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
14+
> input+=("${BASH_REMATCH[1]}")
15+
> input+=("${BASH_REMATCH[2]}")
16+
> elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
17+
> input+=("${BASH_REMATCH[1]}")
18+
> input+=("${BASH_REMATCH[2]}")
19+
end
20+
21+
if Settings.compact_short_flags
22+
> elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
23+
> flags="${BASH_REMATCH[1]}"
24+
> for ((i = 0; i < ${#flags}; i++)); do
25+
> input+=("-${flags:i:1}")
26+
> done
27+
end
28+
29+
> elif [[ "$arg" == "--" ]]; then
30+
> passthru=true
31+
> input+=("$arg")
32+
> else
33+
> input+=("$arg")
34+
> fi
35+
>
36+
> shift
37+
> done
38+
> }
39+
>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
= view_marker
2+
3+
> normalize_input() {
4+
> input=("$@")
5+
> }
6+
>

schemas/settings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@
103103
},
104104
"compact_short_flags": {
105105
"title": "compact short flags",
106-
"description": "Whether to expand short flags of the current script\nhttps://bashly.dannyb.co/usage/settings/#compact_short_flags",
106+
"description": "Whether to expand -abc to -a -b -c in the input line\nhttps://bashly.dannyb.co/usage/settings/#compact_short_flags",
107+
"type": "boolean",
108+
"default": true
109+
},
110+
"conjoined_flag_args": {
111+
"title": "conjoined flag args",
112+
"description": "Whether to expand --flag=value to --flag value in the input line\nhttps://bashly.dannyb.co/usage/settings/#conjoined_flag_args",
107113
"type": "boolean",
108114
"default": true
109115
},
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
creating user files in src
2+
skipped src/root_command.sh (exists)
3+
created ./cli
4+
run ./cli --help to test your bash script
5+
+ ./cli --user=admin -p=secret -- --region=us-east-1 -static
6+
# this file is located in 'src/root_command.sh'
7+
# you can edit it freely and regenerate (it will not be overwritten)
8+
args:
9+
- ${args[--password]} = secret
10+
- ${args[--user]} = admin
11+
12+
other_args:
13+
- ${other_args[*]} = --region=us-east-1 -static
14+
- ${other_args[0]} = --region=us-east-1
15+
- ${other_args[1]} = -static
16+
+ BASHLY_CONJOINED_FLAG_ARGS=no
17+
+ bundle exec bashly generate
18+
creating user files in src
19+
skipped src/root_command.sh (exists)
20+
created ./cli
21+
run ./cli --help to test your bash script
22+
+ ./cli --user admin -p secret --region=us-east-1
23+
# this file is located in 'src/root_command.sh'
24+
# you can edit it freely and regenerate (it will not be overwritten)
25+
args:
26+
- ${args[--password]} = secret
27+
- ${args[--user]} = admin
28+
29+
other_args:
30+
- ${other_args[*]} = --region=us-east-1
31+
- ${other_args[0]} = --region=us-east-1
32+
+ ./cli --user admin -p secret -- --region=us-east-1 -static
33+
# this file is located in 'src/root_command.sh'
34+
# you can edit it freely and regenerate (it will not be overwritten)
35+
args:
36+
- ${args[--password]} = secret
37+
- ${args[--user]} = admin
38+
39+
other_args:
40+
- ${other_args[*]} = --region=us-east-1 -static
41+
- ${other_args[0]} = --region=us-east-1
42+
- ${other_args[1]} = -static
43+
+ ./cli --user=admin -p=secret -- --region=us-east-1 -static
44+
missing required flag: --user, -u NAME
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
creating user files in src
2+
skipped src/root_command.sh (exists)
3+
created ./cli
4+
run ./cli --help to test your bash script
5+
+ echo '=== bad - since conjoined_flag_args is disabled'
6+
=== bad - since conjoined_flag_args is disabled
7+
+ ./cli --user=admin
8+
missing required flag: --user, -u NAME
9+
+ echo '=== bad - since compact_short_flags is disabled (-fd will be considered as catch_all)'
10+
=== bad - since compact_short_flags is disabled (-fd will be considered as catch_all)
11+
+ ./cli --user admin --password secret -fd
12+
args:
13+
- ${args[--password]} = secret
14+
- ${args[--user]} = admin
15+
16+
other_args:
17+
- ${other_args[*]} = -fd
18+
- ${other_args[0]} = -fd
19+
+ echo '=== good'
20+
=== good
21+
+ ./cli --user admin -p secret -f -d --region=us-east-1 -static
22+
args:
23+
- ${args[--debug]} = 1
24+
- ${args[--force]} = 1
25+
- ${args[--password]} = secret
26+
- ${args[--user]} = admin
27+
28+
other_args:
29+
- ${other_args[*]} = --region=us-east-1 -static
30+
- ${other_args[0]} = --region=us-east-1
31+
- ${other_args[1]} = -static
32+
+ echo '=== good'
33+
=== good
34+
+ ./cli --user admin -p secret -- --region=us-east-1 -static
35+
args:
36+
- ${args[--password]} = secret
37+
- ${args[--user]} = admin
38+
39+
other_args:
40+
- ${other_args[*]} = --region=us-east-1 -static
41+
- ${other_args[0]} = --region=us-east-1
42+
- ${other_args[1]} = -static
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This fixture tests that setting `conjoined_flag_args` to `false` indeed ignores
2+
this `--flag=value` and `-f=value` patterns.

0 commit comments

Comments
 (0)