@@ -48,7 +48,7 @@ args_clean() {
48
48
__ARGS[argument.size]=0
49
49
# optional argument
50
50
__ARGS[option.size]=0
51
- # indicate if argument is sorted
51
+ # indicate if arguments are sorted
52
52
__ARGS[sorted]=" false"
53
53
return 0
54
54
}
@@ -1069,6 +1069,8 @@ args_usage_line() {
1069
1069
str=" usage: ${1##*/ } "
1070
1070
fi
1071
1071
local usage_basename_length=" ${# str} "
1072
+ local jump_spaces
1073
+ printf -v jump_spaces " %*s" " ${usage_basename_length} " " "
1072
1074
current_col=" ${usage_basename_length} "
1073
1075
i=0
1074
1076
while [[ " ${i} " -lt " ${__ARGS[option.size]} " ]]; do
@@ -1108,11 +1110,7 @@ args_usage_line() {
1108
1110
if [[ " $(( current_col + ${# option} )) " -gt " ${max_col} " ]]; then
1109
1111
has_max_col=" true"
1110
1112
str+=$' \n '
1111
- j=0
1112
- while [[ " ${j} " -lt " ${usage_basename_length} " ]]; do
1113
- str+=" "
1114
- j=$(( j + 1 ))
1115
- done
1113
+ str+=" ${jump_spaces} "
1116
1114
current_col=" ${usage_basename_length} "
1117
1115
fi
1118
1116
str+=" ${option} "
@@ -1122,18 +1120,10 @@ args_usage_line() {
1122
1120
if [[ " ${__ARGS[argument.size]} " -ne 0 ]]; then
1123
1121
if [[ " true" == " ${has_max_col} " ]] || [[ " $(( current_col + 3 )) " -gt " ${max_col} " ]]; then
1124
1122
str+=$' \n '
1125
- j=0
1126
- while [[ " ${j} " -lt " ${usage_basename_length} " ]]; do
1127
- str+=" "
1128
- j=$(( j + 1 ))
1129
- done
1123
+ str+=" ${jump_spaces} "
1130
1124
str+=" --"
1131
1125
str+=$' \n '
1132
- j=0
1133
- while [[ " ${j} " -lt " ${usage_basename_length} " ]]; do
1134
- str+=" "
1135
- j=$(( j + 1 ))
1136
- done
1126
+ str+=" ${jump_spaces} "
1137
1127
current_col=" ${usage_basename_length} "
1138
1128
else
1139
1129
str+=" --"
@@ -1159,11 +1149,7 @@ args_usage_line() {
1159
1149
fi
1160
1150
if [[ " $(( current_col + ${# option} )) " -gt " ${max_col} " ]]; then
1161
1151
str+=$' \n '
1162
- j=0
1163
- while [[ " ${j} " -lt " ${usage_basename_length} " ]]; do
1164
- str+=" "
1165
- j=$(( j + 1 ))
1166
- done
1152
+ str+=" ${jump_spaces} "
1167
1153
current_col=" ${usage_basename_length} "
1168
1154
fi
1169
1155
str+=" ${option} "
@@ -1189,6 +1175,11 @@ args_usage() {
1189
1175
local max_col
1190
1176
local current_col=0
1191
1177
local has_max_col=" false"
1178
+ local jump_spaces
1179
+ local jump_spaces_padding
1180
+ local jump_spaces_helper
1181
+ printf -v jump_spaces_padding " %*s" " ${__ARGS[usage.width.padding]} " " "
1182
+ printf -v jump_spaces_helper " %*s" " $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1 )) " " "
1192
1183
max_col=" $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} + ${__ARGS[usage.width.help]} )) "
1193
1184
args_usage_line " $1 "
1194
1185
str=" "
@@ -1204,11 +1195,7 @@ args_usage() {
1204
1195
fi
1205
1196
i=0
1206
1197
while [[ " ${i} " -lt " ${__ARGS[argument.size]} " ]]; do
1207
- j=0
1208
- while [[ " ${j} " -lt " ${__ARGS[usage.width.padding]} " ]]; do
1209
- str+=" "
1210
- j=$(( j + 1 ))
1211
- done
1198
+ str+=" ${jump_spaces_padding} "
1212
1199
local option=" "
1213
1200
if [[ -n " ${__ARGS[argument.${i}.choices]} " ]]; then
1214
1201
option+=" {${__ARGS[argument.${i}.choices]// / ,} }"
@@ -1219,19 +1206,12 @@ args_usage() {
1219
1206
if [[ -n " ${__ARGS[argument.${i}.help]} " ]]; then
1220
1207
if [[ " ${# option} " -gt " ${__ARGS[usage.width.argument]} " ]]; then
1221
1208
str+=$' \n '
1222
- j=0
1223
- while [[ " ${j} " -lt " $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1 )) " ]]; do
1224
- str+=" "
1225
- j=$(( j + 1 ))
1226
- done
1209
+ str+=" ${jump_spaces_helper} "
1227
1210
else
1228
- j=0
1229
- while [[ " ${j} " -lt " $(( ${__ARGS[usage.width.argument]} - ${# option} + ${__ARGS[usage.width.separator]} - 1 )) " ]]; do
1230
- str+=" "
1231
- j=$(( j + 1 ))
1232
- done
1211
+ printf -v jump_spaces " %*s" " $(( ${__ARGS[usage.width.argument]} - ${# option} + ${__ARGS[usage.width.separator]} - 1 )) " " "
1212
+ str+=" ${jump_spaces} "
1233
1213
fi
1234
- current_col=" $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1 )) "
1214
+ current_col=" ${ # jump_spaces_helper} "
1235
1215
# save last IFS
1236
1216
local old_ifs
1237
1217
old_ifs=" ${IFS} "
@@ -1240,12 +1220,8 @@ args_usage() {
1240
1220
for word in ${__ARGS[argument.${i}.help]} ; do
1241
1221
if [[ " $(( current_col + ${# word} + 1 )) " -gt " $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} + ${__ARGS[usage.width.help]} )) " ]]; then
1242
1222
str+=$' \n '
1243
- j=0
1244
- while [[ " ${j} " -lt " $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1 )) " ]]; do
1245
- str+=" "
1246
- j=$(( j + 1 ))
1247
- done
1248
- current_col=" $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1 )) "
1223
+ str+=" ${jump_spaces_helper} "
1224
+ current_col=" ${# jump_spaces_helper} "
1249
1225
fi
1250
1226
str+=" ${word} "
1251
1227
current_col=" $(( current_col + ${# word} + 1 )) "
@@ -1262,11 +1238,7 @@ args_usage() {
1262
1238
fi
1263
1239
i=0
1264
1240
while [[ " ${i} " -lt " ${__ARGS[option.size]} " ]]; do
1265
- j=0
1266
- while [[ " ${j} " -lt " ${__ARGS[usage.width.padding]} " ]]; do
1267
- str+=" "
1268
- j=$(( j + 1 ))
1269
- done
1241
+ str+=" ${jump_spaces_padding} "
1270
1242
local option=" "
1271
1243
local type
1272
1244
for type in " short" " long" ; do
@@ -1305,19 +1277,12 @@ args_usage() {
1305
1277
if [[ -n " ${__ARGS[option.${i}.help]} " ]]; then
1306
1278
if [[ " ${# option} " -gt " ${__ARGS[usage.width.argument]} " ]]; then
1307
1279
str+=$' \n '
1308
- j=0
1309
- while [[ " ${j} " -lt " $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1 )) " ]]; do
1310
- str+=" "
1311
- j=$(( j + 1 ))
1312
- done
1280
+ str+=" ${jump_spaces_helper} "
1313
1281
else
1314
- j=0
1315
- while [[ " ${j} " -lt " $(( ${__ARGS[usage.width.argument]} - ${# option} + ${__ARGS[usage.width.separator]} - 1 )) " ]]; do
1316
- str+=" "
1317
- j=$(( j + 1 ))
1318
- done
1282
+ printf -v jump_spaces " %*s" " $(( ${__ARGS[usage.width.argument]} - ${# option} + ${__ARGS[usage.width.separator]} - 1 )) " " "
1283
+ str+=" ${jump_spaces} "
1319
1284
fi
1320
- current_col=" $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1 )) "
1285
+ current_col=" ${ # jump_spaces_helper} "
1321
1286
# save last IFS
1322
1287
local old_ifs
1323
1288
old_ifs=" ${IFS} "
@@ -1326,12 +1291,8 @@ args_usage() {
1326
1291
for word in ${__ARGS[option.${i}.help]} ; do
1327
1292
if [[ " $(( current_col + ${# word} + 1 )) " -gt " $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} + ${__ARGS[usage.width.help]} )) " ]]; then
1328
1293
str+=$' \n '
1329
- j=0
1330
- while [[ " ${j} " -lt " $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1 )) " ]]; do
1331
- str+=" "
1332
- j=$(( j + 1 ))
1333
- done
1334
- current_col=" $(( ${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1 )) "
1294
+ str+=" ${jump_spaces_helper} "
1295
+ current_col=" ${# jump_spaces_helper} "
1335
1296
fi
1336
1297
str+=" ${word} "
1337
1298
current_col=" $(( current_col + ${# word} + 1 )) "
0 commit comments