Skip to content

Commit a789eed

Browse files
authored
Merge pull request #276 from DannyBen/add/configurable-compact-flags
Allow disabling compact flag expansion (-abc to -a -b -c)
2 parents ff65c10 + 8e73959 commit a789eed

File tree

10 files changed

+92
-1
lines changed

10 files changed

+92
-1
lines changed

lib/bashly/settings.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ class Settings
33
class << self
44
include AssetHelper
55

6-
attr_writer :source_dir, :target_dir, :lib_dir, :strict, :tab_indent
6+
attr_writer :compact_short_flags, :source_dir, :target_dir,
7+
:lib_dir, :strict, :tab_indent
78

89
def source_dir
910
@source_dir ||= get :source_dir
@@ -25,6 +26,10 @@ def tab_indent
2526
@tab_indent ||= get :tab_indent
2627
end
2728

29+
def compact_short_flags
30+
@compact_short_flags ||= get :compact_short_flags
31+
end
32+
2833
def env
2934
@env ||= get(:env)&.to_sym
3035
end

lib/bashly/templates/settings.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ strict: false
2222
# (every 2 leading spaces will be converted to a tab character)
2323
tab_indent: false
2424

25+
# When true, the generated script will consider any argument in the form of
26+
# `-abc` as if it is `-a -b -c`.
27+
compact_short_flags: true
28+
2529
# Set to 'production' or 'development':
2630
# - production generate a smaller script, without file markers
2731
# - development generate with file markers

lib/bashly/views/command/normalize_input.gtx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
> elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
1212
> input+=("${BASH_REMATCH[1]}")
1313
> input+=("${BASH_REMATCH[2]}")
14+
15+
if Settings.compact_short_flags
1416
> elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
1517
> flags="${BASH_REMATCH[1]}"
1618
> for (( i=0 ; i < ${#flags} ; i++ )); do
1719
> input+=("-${flags:i:1}")
1820
> done
21+
end
22+
1923
> else
2024
> input+=("$arg")
2125
> fi
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
creating user files in src
2+
skipped src/initialize.sh (exists)
3+
skipped src/root_command.sh (exists)
4+
created ./cli
5+
run ./cli --help to test your bash script
6+
+ ./cli -abc --delta
7+
# this file is located in 'src/root_command.sh'
8+
# you can edit it freely and regenerate (it will not be overwritten)
9+
args:
10+
- ${args[--alpha]} = 1
11+
- ${args[--bravo]} = 1
12+
- ${args[--charlie]} = 1
13+
- ${args[--delta]} = 1
14+
+ BASHLY_COMPACT_SHORT_FLAGS=no
15+
+ bundle exec bashly generate
16+
creating user files in src
17+
skipped src/initialize.sh (exists)
18+
skipped src/root_command.sh (exists)
19+
created ./cli
20+
run ./cli --help to test your bash script
21+
+ ./cli -abc --delta
22+
invalid option: -abc
23+
+ ./cli -a -b -c
24+
# this file is located in 'src/root_command.sh'
25+
# you can edit it freely and regenerate (it will not be overwritten)
26+
args:
27+
- ${args[--alpha]} = 1
28+
- ${args[--bravo]} = 1
29+
- ${args[--charlie]} = 1
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 `compact_short_flags` to `false` indeed ignores
2+
this `-abc` pattern.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
flags:
6+
- long: --alpha
7+
short: -a
8+
help: Alpha
9+
- long: --bravo
10+
short: -b
11+
help: Bravo
12+
- long: --charlie
13+
short: -c
14+
help: Charlie
15+
- long: --delta
16+
short: -d
17+
help: Delta
18+
19+
20+
21+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Code here runs inside the initialize() function
2+
## Use it for anything that you need to run before any other function, like
3+
## setting environment variables:
4+
## CONFIG_FILE=settings.ini
5+
##
6+
## Feel free to empty (but not delete) this file.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
echo "# this file is located in 'src/root_command.sh'"
2+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
3+
inspect_args
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
# This fixture tests that setting `compact_short_flags` to `false` indeed ignores
4+
# this `-abc` pattern.
5+
6+
bundle exec bashly generate
7+
8+
set -x
9+
10+
./cli -abc --delta
11+
12+
# Use ad-hoc setting through an environment variable
13+
BASHLY_COMPACT_SHORT_FLAGS="no" bundle exec bashly generate
14+
15+
./cli -abc --delta
16+
./cli -a -b -c

0 commit comments

Comments
 (0)