Skip to content

Commit fec522e

Browse files
authored
Merge pull request #105 from DannyBen/add/flag-enhancements
Add support for -abc, -a=arg and --flag=arg
2 parents 7592d5e + ad958cc commit fec522e

File tree

33 files changed

+799
-26
lines changed

33 files changed

+799
-26
lines changed

examples/catch-all-advanced/cli

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,30 @@ cli_upload_usage() {
133133
fi
134134
}
135135

136+
# :command.normalize_input
137+
normalize_input() {
138+
local arg flags
139+
140+
while [[ $# -gt 0 ]]; do
141+
arg="$1"
142+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
143+
input+=("${BASH_REMATCH[1]}")
144+
input+=("${BASH_REMATCH[2]}")
145+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
146+
input+=("${BASH_REMATCH[1]}")
147+
input+=("${BASH_REMATCH[2]}")
148+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
149+
flags="${BASH_REMATCH[1]}"
150+
for (( i=0 ; i < ${#flags} ; i++ )); do
151+
input+=("-${flags:i:1}")
152+
done
153+
else
154+
input+=("$arg")
155+
fi
156+
157+
shift
158+
done
159+
}
136160
# :command.inspect_args
137161
inspect_args() {
138162
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
@@ -382,7 +406,9 @@ initialize() {
382406
run() {
383407
declare -A args
384408
declare -a other_args
385-
parse_requirements "$@"
409+
declare -a input
410+
normalize_input "$@"
411+
parse_requirements "${input[@]}"
386412

387413
if [[ $action == "download" ]]; then
388414
if [[ ${args[--help]} ]]; then

examples/catch-all/download

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ download_usage() {
5252
fi
5353
}
5454

55+
# :command.normalize_input
56+
normalize_input() {
57+
local arg flags
58+
59+
while [[ $# -gt 0 ]]; do
60+
arg="$1"
61+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
62+
input+=("${BASH_REMATCH[1]}")
63+
input+=("${BASH_REMATCH[2]}")
64+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
65+
input+=("${BASH_REMATCH[1]}")
66+
input+=("${BASH_REMATCH[2]}")
67+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
68+
flags="${BASH_REMATCH[1]}"
69+
for (( i=0 ; i < ${#flags} ; i++ )); do
70+
input+=("-${flags:i:1}")
71+
done
72+
else
73+
input+=("$arg")
74+
fi
75+
76+
shift
77+
done
78+
}
5579
# :command.inspect_args
5680
inspect_args() {
5781
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
@@ -155,7 +179,9 @@ initialize() {
155179
run() {
156180
declare -A args
157181
declare -a other_args
158-
parse_requirements "$@"
182+
declare -a input
183+
normalize_input "$@"
184+
parse_requirements "${input[@]}"
159185

160186
if [[ $action == "root" ]]; then
161187
root_command

examples/colors/colorly

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ colorly_usage() {
5757
fi
5858
}
5959

60+
# :command.normalize_input
61+
normalize_input() {
62+
local arg flags
63+
64+
while [[ $# -gt 0 ]]; do
65+
arg="$1"
66+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
67+
input+=("${BASH_REMATCH[1]}")
68+
input+=("${BASH_REMATCH[2]}")
69+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
70+
input+=("${BASH_REMATCH[1]}")
71+
input+=("${BASH_REMATCH[2]}")
72+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
73+
flags="${BASH_REMATCH[1]}"
74+
for (( i=0 ; i < ${#flags} ; i++ )); do
75+
input+=("-${flags:i:1}")
76+
done
77+
else
78+
input+=("$arg")
79+
fi
80+
81+
shift
82+
done
83+
}
6084
# :command.inspect_args
6185
inspect_args() {
6286
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
@@ -188,7 +212,9 @@ initialize() {
188212
run() {
189213
declare -A args
190214
declare -a other_args
191-
parse_requirements "$@"
215+
declare -a input
216+
normalize_input "$@"
217+
parse_requirements "${input[@]}"
192218

193219
if [[ $action == "root" ]]; then
194220
root_command

examples/command-default/ftp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ ftp_download_usage() {
113113
fi
114114
}
115115

116+
# :command.normalize_input
117+
normalize_input() {
118+
local arg flags
119+
120+
while [[ $# -gt 0 ]]; do
121+
arg="$1"
122+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
123+
input+=("${BASH_REMATCH[1]}")
124+
input+=("${BASH_REMATCH[2]}")
125+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
126+
input+=("${BASH_REMATCH[1]}")
127+
input+=("${BASH_REMATCH[2]}")
128+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
129+
flags="${BASH_REMATCH[1]}"
130+
for (( i=0 ; i < ${#flags} ; i++ )); do
131+
input+=("-${flags:i:1}")
132+
done
133+
else
134+
input+=("$arg")
135+
fi
136+
137+
shift
138+
done
139+
}
116140
# :command.inspect_args
117141
inspect_args() {
118142
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
@@ -367,7 +391,9 @@ initialize() {
367391
run() {
368392
declare -A args
369393
declare -a other_args
370-
parse_requirements "$@"
394+
declare -a input
395+
normalize_input "$@"
396+
parse_requirements "${input[@]}"
371397

372398
if [[ $action == "upload" ]]; then
373399
if [[ ${args[--help]} ]]; then

examples/command-groups/ftp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ ftp_logout_usage() {
160160
fi
161161
}
162162

163+
# :command.normalize_input
164+
normalize_input() {
165+
local arg flags
166+
167+
while [[ $# -gt 0 ]]; do
168+
arg="$1"
169+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
170+
input+=("${BASH_REMATCH[1]}")
171+
input+=("${BASH_REMATCH[2]}")
172+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
173+
input+=("${BASH_REMATCH[1]}")
174+
input+=("${BASH_REMATCH[2]}")
175+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
176+
flags="${BASH_REMATCH[1]}"
177+
for (( i=0 ; i < ${#flags} ; i++ )); do
178+
input+=("-${flags:i:1}")
179+
done
180+
else
181+
input+=("$arg")
182+
fi
183+
184+
shift
185+
done
186+
}
163187
# :command.inspect_args
164188
inspect_args() {
165189
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
@@ -530,7 +554,9 @@ initialize() {
530554
run() {
531555
declare -A args
532556
declare -a other_args
533-
parse_requirements "$@"
557+
declare -a input
558+
normalize_input "$@"
559+
parse_requirements "${input[@]}"
534560

535561
if [[ $action == "download" ]]; then
536562
if [[ ${args[--help]} ]]; then

examples/commands-nested/cli

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,30 @@ cli_file_edit_usage() {
243243
fi
244244
}
245245

246+
# :command.normalize_input
247+
normalize_input() {
248+
local arg flags
249+
250+
while [[ $# -gt 0 ]]; do
251+
arg="$1"
252+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
253+
input+=("${BASH_REMATCH[1]}")
254+
input+=("${BASH_REMATCH[2]}")
255+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
256+
input+=("${BASH_REMATCH[1]}")
257+
input+=("${BASH_REMATCH[2]}")
258+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
259+
flags="${BASH_REMATCH[1]}"
260+
for (( i=0 ; i < ${#flags} ; i++ )); do
261+
input+=("-${flags:i:1}")
262+
done
263+
else
264+
input+=("$arg")
265+
fi
266+
267+
shift
268+
done
269+
}
246270
# :command.inspect_args
247271
inspect_args() {
248272
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
@@ -772,7 +796,9 @@ initialize() {
772796
run() {
773797
declare -A args
774798
declare -a other_args
775-
parse_requirements "$@"
799+
declare -a input
800+
normalize_input "$@"
801+
parse_requirements "${input[@]}"
776802

777803
if [[ $action == "dir" ]]; then
778804
if [[ ${args[--help]} ]]; then

examples/commands/cli

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,30 @@ cli_upload_usage() {
152152
fi
153153
}
154154

155+
# :command.normalize_input
156+
normalize_input() {
157+
local arg flags
158+
159+
while [[ $# -gt 0 ]]; do
160+
arg="$1"
161+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
162+
input+=("${BASH_REMATCH[1]}")
163+
input+=("${BASH_REMATCH[2]}")
164+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
165+
input+=("${BASH_REMATCH[1]}")
166+
input+=("${BASH_REMATCH[2]}")
167+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
168+
flags="${BASH_REMATCH[1]}"
169+
for (( i=0 ; i < ${#flags} ; i++ )); do
170+
input+=("-${flags:i:1}")
171+
done
172+
else
173+
input+=("$arg")
174+
fi
175+
176+
shift
177+
done
178+
}
155179
# :command.inspect_args
156180
inspect_args() {
157181
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
@@ -438,7 +462,9 @@ initialize() {
438462
run() {
439463
declare -A args
440464
declare -a other_args
441-
parse_requirements "$@"
465+
declare -a input
466+
normalize_input "$@"
467+
parse_requirements "${input[@]}"
442468

443469
if [[ $action == "download" ]]; then
444470
if [[ ${args[--help]} ]]; then

examples/completions/cli

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,30 @@ cli_upload_usage() {
172172
fi
173173
}
174174

175+
# :command.normalize_input
176+
normalize_input() {
177+
local arg flags
178+
179+
while [[ $# -gt 0 ]]; do
180+
arg="$1"
181+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
182+
input+=("${BASH_REMATCH[1]}")
183+
input+=("${BASH_REMATCH[2]}")
184+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
185+
input+=("${BASH_REMATCH[1]}")
186+
input+=("${BASH_REMATCH[2]}")
187+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
188+
flags="${BASH_REMATCH[1]}"
189+
for (( i=0 ; i < ${#flags} ; i++ )); do
190+
input+=("-${flags:i:1}")
191+
done
192+
else
193+
input+=("$arg")
194+
fi
195+
196+
shift
197+
done
198+
}
175199
# :command.inspect_args
176200
inspect_args() {
177201
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
@@ -547,7 +571,9 @@ initialize() {
547571
run() {
548572
declare -A args
549573
declare -a other_args
550-
parse_requirements "$@"
574+
declare -a input
575+
normalize_input "$@"
576+
parse_requirements "${input[@]}"
551577

552578
if [[ $action == "completions" ]]; then
553579
if [[ ${args[--help]} ]]; then

examples/config-ini/configly

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ configly_list_usage() {
159159
fi
160160
}
161161

162+
# :command.normalize_input
163+
normalize_input() {
164+
local arg flags
165+
166+
while [[ $# -gt 0 ]]; do
167+
arg="$1"
168+
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
169+
input+=("${BASH_REMATCH[1]}")
170+
input+=("${BASH_REMATCH[2]}")
171+
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
172+
input+=("${BASH_REMATCH[1]}")
173+
input+=("${BASH_REMATCH[2]}")
174+
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
175+
flags="${BASH_REMATCH[1]}"
176+
for (( i=0 ; i < ${#flags} ; i++ )); do
177+
input+=("-${flags:i:1}")
178+
done
179+
else
180+
input+=("$arg")
181+
fi
182+
183+
shift
184+
done
185+
}
162186
# :command.inspect_args
163187
inspect_args() {
164188
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
@@ -618,7 +642,9 @@ initialize() {
618642
run() {
619643
declare -A args
620644
declare -a other_args
621-
parse_requirements "$@"
645+
declare -a input
646+
normalize_input "$@"
647+
parse_requirements "${input[@]}"
622648

623649
if [[ $action == "set" ]]; then
624650
if [[ ${args[--help]} ]]; then

0 commit comments

Comments
 (0)