-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparser
More file actions
executable file
·10473 lines (9577 loc) · 425 KB
/
Copy pathargparser
File metadata and controls
executable file
·10473 lines (9577 loc) · 425 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
###############################################################################
# #
# Copyright 2025-2026 Simon Brandt #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
###############################################################################
# Author: Simon Brandt
# E-Mail: simon.brandt@uni-greifswald.de
# Last Modification: 2026-03-25
# TODO: Output a warning message when options in the configuration file
# are set that don't correspond to Argparser environment
# variables.
# TODO: Remove need for message parsing after argument parsing by using
# nonlocal variables.
# Usage: Source this script with 'source argparser -- "$@"' inside the
# script whose arguments need to be parsed. If ${ARGPARSER_READ_ARGS}
# is set to "true" (the default), the arguments will be parsed upon
# sourcing. If ${ARGPARSER_SET_ARGS} is set to "true" (the default),
# the arguments will also be set to variables upon sourcing, else, the
# associative array which ${ARGPARSER_ARG_ARRAY_NAME} points to needs to
# be accessed. Refer to the documentation for details.
# Purpose: Parse a script's arguments, giving proper error and warning
# messages for wrongly set arguments, assigning the values to the
# respective variables, as well as creating and printing a help, usage,
# and version message. Refer to the documentation for details.
###############################################################################
# Define the Argparser functions to check that the Argparser is run by
# Bash 4.4 or higher, and execute them immediately, such that the rest
# of the script doesn't get parsed (which would create errors since it's
# written in Bash, not POSIX compliantly, and using Bash syntax not
# available prior Bash 4.4).
argparser_check_shell() {
# Check that the current shell is a Bash instance. Else, print an
# error message.
#
# In order to have the function interpretable by shells other than
# Bash, all commands are written in a POSIX-conformant form.
# shellcheck disable=SC2292 # Intentional POSIX conformance.
if [ -z "${BASH}" ]; then
printf '%s: %s\n' "$0" \
"Error: The Argparser requires Bash as executing shell." >&2
exit 1
fi
}
argparser_check_shell
function argparser_check_bash_version() {
# Check that version of the current Bash instance is at least 4.4.
# Else, print an error message.
if (( BASH_VERSINFO[0] < 4 \
|| (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 4) ))
then
printf '%s: %s\n' "$0" \
"Error: The Argparser requires Bash 4.4 or higher." >&2
exit 1
fi
}
argparser_check_bash_version
# Define the Argparser function used for debugging.
function argparser_toggle_debugger() {
# Start or stop the Argparser debugger.
#
# Arguments:
# - $1: whether to start or stop the debugger ("start" or "stop")
# Define the local variables.
local status
local trap_action
status="$1"
# shellcheck disable=SC2016 # Intentional single quotes around variables.
# shellcheck disable=SC2064 # Intentional double-quoted trap action.
if [[ "${status}" == "start" ]]; then
# Print the current 4-digit line number, the currently executing
# function's name, and the command to be executed, for any
# command.
trap_action='printf "Line %4d in %s: %s\n" "${LINENO}" '
trap_action+='"${FUNCNAME[0]:-main}" "${BASH_COMMAND}" >&2'
trap "${trap_action}" DEBUG
set -o functrace
else
# Reset the trap action.
trap - DEBUG
fi
}
# Define the Argparser functions used for general tests.
function argparser_in_array() {
# Check if an element occurs in an array.
#
# Arguments:
# - $1: the element to search for
# - $@: the array to search through
#
# Return value:
# - 0, if the element exists in the array
# - 1, else
# Define the local variables.
local element
local query
# Read the query element and shift the arguments such that only the
# array to search through remains.
query="$1"
shift
# Iterate through the array and compare each element to the query.
# Return 0 on success, else 1.
for element in "$@"; do
if [[ "${element}" == "${query}" ]]; then
return 0
fi
done
return 1
}
function argparser_is_alpha() {
# Check if a variable's value is an alphabetical character.
#
# Arguments:
# - $1: the variable whose value to check
#
# Return value:
# - 0, if the variable's value is an alphabetical character
# - 1, else
# Define the local variable.
local var
# Read the argument.
var="$1"
# Check the variable's value.
if [[ "${var}" == [[:alpha:]] ]]; then
return 0
else
return 1
fi
}
function argparser_is_bool() {
# Check if a variable's value is a Boolean.
#
# Arguments:
# - $1: the variable whose value to check
#
# Return value:
# - 0, if the variable's value is a Boolean
# - 1, else
# Define the local variable.
local var
# Read the argument.
var="$1"
# Check the variable's value.
if [[ "${var}" == true || "${var}" == false ]]; then
return 0
else
return 1
fi
}
function argparser_is_char() {
# Check if a variable's value is a (printable) character.
#
# Arguments:
# - $1: the variable whose value to check
#
# Return value:
# - 0, if the variable's value is a character
# - 1, else
# Define the local variable.
local var
# Read the argument.
var="$1"
# Check the variable's value.
if [[ "${var}" == [[:print:]] ]]; then
return 0
else
return 1
fi
}
function argparser_is_float() {
# Check if a variable's value is a floating-point number.
#
# Arguments:
# - $1: the variable whose value to check
#
# Return value:
# - 0, if the variable's value is a floating-point number
# - 1, else
# Define the local variable.
local var
# Read the argument.
var="$1"
# Check the variable's value.
if [[ "${var}" == ?([+-])+([[:digit:]]).*([[:digit:]])
|| "${var}" == ?([+-])*([[:digit:]]).+([[:digit:]]) ]]
then
return 0
else
return 1
fi
}
function argparser_is_identifier() {
# Check if a variable's value is usable as Bash variable identifier.
#
# Arguments:
# - $1: the variable whose value to check
#
# Return value:
# - 0, if the variable's value is usable as identifier
# - 1, else
# Define the local variable.
local var
# Read the argument.
var="$1"
# Check the variable's value.
if [[ "${var}" == [[:alpha:]_]*([[:word:]]) ]]; then
return 0
else
return 1
fi
}
function argparser_is_int() {
# Check if a variable's value is an integer.
#
# Arguments:
# - $1: the variable whose value to check
#
# Return value:
# - 0, if the variable's value is an integer
# - 1, else
# Define the local variable.
local var
# Read the argument.
var="$1"
# Check the variable's value.
if [[ "${var}" == ?([+-])+([[:digit:]]) ]]; then
return 0
else
return 1
fi
}
function argparser_is_uint() {
# Check if a variable's value is an unsigned integer.
#
# Arguments:
# - $1: the variable whose value to check
#
# Return value:
# - 0, if the variable's value is an unsigned integer
# - 1, else
# Define the local variable.
local var
# Read the argument.
var="$1"
# Check the variable's value.
if [[ "${var}" == +([[:digit:]]) ]]; then
return 0
else
return 1
fi
}
function argparser_check_styles() {
# Check if the specified colors, text styles, and/or frame styles
# are implemented for argparser_colorize.
#
# Arguments:
# - $1: the array(s) to check ("colors", "text styles",
# "frame styles", "colors, text styles", or "colors, frame
# styles")
# - $2: the colors and/or styles to use as
# ${ARGPARSER_ARG_DELIMITER_2}-separated list
#
# Environment:
# - ARGPARSER_ARG_DELIMITER_2 (read-only)
#
# Return value:
# - 0, if all colors and/or styles are implemented
# - 1, else
# Define the local variables.
local check_type
local -a colors
local -a corner_styles
local -a line_styles
local -a line_widths
local request
local -a requests
local -a text_styles
# Read the arguments.
check_type="$1"
requests="$2"
# Define the arrays of implemented colors, text styles, and frame
# styles (corner and line styles, as well as line widths).
colors=(
"black"
"red"
"green"
"yellow"
"blue"
"magenta"
"cyan"
"white"
)
text_styles=(
"normal"
"bold"
"faint"
"italic"
"underline"
"double"
"overline"
"crossed-out"
"blink"
"reverse"
)
corner_styles=(
"angular"
"rounded"
"double"
)
line_styles=(
"solid"
"double-dashed"
"triple-dashed"
"quadruple-dashed"
)
line_widths=(
"thin"
"thick"
"double"
)
# Split the requested colors and/or styles on
# ${ARGPARSER_ARG_DELIMITER_2} characters and check any.
IFS="${ARGPARSER_ARG_DELIMITER_2}" read -r -a requests <<< "${requests}"
if [[ "${check_type}" == "colors" ]]; then
if (( "${#requests[@]}" == 0 )); then
return 1
fi
for request in "${requests[@]}"; do
request="${request#bright_}"
request="${request%@(_bg|_fg)}"
if ! argparser_in_array "${request}" "${colors[@]}"; then
return 1
fi
done
elif [[ "${check_type}" == "text styles" ]]; then
if (( "${#requests[@]}" == 0 )); then
return 1
fi
for request in "${requests[@]}"; do
if ! argparser_in_array "${request}" "${text_styles[@]}"; then
return 1
fi
done
elif [[ "${check_type}" == "frame styles" ]]; then
for request in "${requests[@]}"; do
if ! argparser_in_array "${request}" "${corner_styles[@]}" \
"${line_styles[@]}" "${line_widths[@]}"
then
return 1
fi
done
elif [[ "${check_type}" == "colors, text styles" ]]; then
if (( "${#requests[@]}" == 0 )); then
return 1
fi
for request in "${requests[@]}"; do
if ! argparser_in_array "${request}" "${colors[@]}" \
"${text_styles[@]}"
then
request="${request#bright_}"
request="${request%@(_bg|_fg)}"
if ! argparser_in_array "${request}" "${colors[@]}"; then
return 1
fi
fi
done
elif [[ "${check_type}" == "colors, frame styles" ]]; then
for request in "${requests[@]}"; do
if ! argparser_in_array "${request}" "${colors[@]}" \
"${corner_styles[@]}" "${line_styles[@]}" "${line_widths[@]}"
then
request="${request#bright_}"
request="${request%@(_bg|_fg)}"
if ! argparser_in_array "${request}" "${colors[@]}"; then
return 1
fi
fi
done
fi
return 0
}
function argparser_check_file() {
# Check if a file exists, is a regular file, is readable, and has a
# size greater than 0 bytes.
#
# Arguments:
# - $1: the file to check
#
# Nonlocals:
# - file_attributes: a bit mask with four bits set to 0 ("true") if
# the file exists (first bit), is a regular file (second bit), is
# readable (third bit), and/or has a size greater than 0 bytes
# (fourth bit), else set to 1 ("false")
# Define the local variable.
local file
# Read the argument.
file="$1"
# Check the variable's value.
if [[ -e "${file}" ]]; then
file_attributes="0"
else
file_attributes="1"
fi
if [[ -f "${file}" ]]; then
file_attributes+="0"
else
file_attributes+="1"
fi
if [[ -r "${file}" ]]; then
file_attributes+="0"
else
file_attributes+="1"
fi
if [[ -s "${file}" ]]; then
file_attributes+="0"
else
file_attributes+="1"
fi
}
# Define the general Argparser utility functions.
function argparser_pad() {
# Pad all elements of an array to the left or right to have the same
# width.
#
# Arguments:
# - $1: the pad side ("left" or "right")
# - $2: the width to pad the array's elements to (an integer, or
# "auto" to compute the maximum width of the array's elements)
# - $@: the array whose elements to pad
#
# Nonlocals:
# - padded_array: the array with padded elements.
# Define the local variables.
local i
local max_width
local side
local -a uncolorized_array
local whitespace
# Read the arguments.
side="$1"
max_width="$2"
shift 2
padded_array=("$@")
# Possibly, get the maximum width of the array's elements.
if [[ "${max_width}" == "auto" ]]; then
argparser_get_max_width "${padded_array[@]}"
fi
# Pad the array's elements in-place.
argparser_uncolorize "array" "${padded_array[@]}"
if [[ "${side}" == "left" ]]; then
for i in "${!padded_array[@]}"; do
printf -v whitespace '%*s' \
"$(( max_width - "${#uncolorized_array[i]}" ))" ""
padded_array[i]="${whitespace}${padded_array[i]}"
done
else
for i in "${!padded_array[@]}"; do
printf -v whitespace '%*s' \
"$(( max_width - "${#uncolorized_array[i]}" ))" ""
padded_array[i]="${padded_array[i]}${whitespace}"
done
fi
}
function argparser_trim() {
# Trim leading and trailing space characters from a string.
#
# Arguments:
# - $1: the string to trim
#
# Nonlocals:
# - trimmed_string: the trimmed string
# Define the local variable.
local string
# Read the argument.
string="$1"
# Trim the string and set the nonlocal ${trimmed_string} to it.
string="${string##+( )}"
string="${string%%+( )}"
trimmed_string="${string}"
}
function argparser_fold() {
# Limit the width of an array's elements to the maximum width by
# spltting them into separate elements. Upon joining the array with
# newline characters, these effectively become inserted line breaks.
#
# Arguments:
# - $1: the characters to use as delimiters, joined to a single
# string
# - $2: the maximum width a column may have
# - $3: the amount of indentation after each line break
# - $@: the array in which to insert line breaks
#
# Nonlocals:
# - folded_array: the array with introduced line breaks
# Define the local variables.
local -a array
local delimiters
local element
local i
local indentation
local is_style
local max_width
local pattern
local style
local -a uncolorized_array
local whitespace
local width
local word
local word_length
local -a words
# Read the arguments.
delimiters="$1"
max_width="$2"
indentation="$3"
shift 3
array=("$@")
# Empty ${folded_array} iff argparser_fold is not recursing due
# to using secondary delimiters. Iff in the first recursion level,
# increment the indentation level.
if (( "${#FUNCNAME[@]}" >= 1 )) \
&& [[ "${FUNCNAME[0]}" != "${FUNCNAME[1]}" ]]
then
folded_array=( )
elif (( "${#FUNCNAME[@]}" >= 2 )) \
&& [[ "${FUNCNAME[0]}" != "${FUNCNAME[2]}" ]]
then
(( indentation += 4 ))
fi
# Set the indentation whitespace.
printf -v whitespace '%*s' "${indentation}" ""
# Split each (uncolorized) element (line) in the array word by word
# on delimiters, such that line breaks aren't inserted into entire
# words.
pattern=$'(\e\[0[0-9;]+m)[^\e]*(\e\[0m)?$'
for element in "${array[@]}"; do
width=0
# Add a new line iff the last line is non-empty, or the current
# line is the first. By this, multiple recursion levels don't
# introduce a new line each whenever multiple subsequent
# delimiters don't yield a short enough word.
if (( "${#folded_array[@]}" == 0 )) || [[ -n "${folded_array[-1]}" ]]
then
folded_array+=("")
fi
# Split the element on the first (primary) delimiter, and
# uncolorize the words.
mapfile -d "${delimiters::1}" -t words <<< "${element}"
words[-1]="${words[-1]%$'\n'}"
argparser_uncolorize "array" "${words[@]}"
# Wrap the lines.
for i in "${!words[@]}"; do
word="${words[i]}"
word_length="${#uncolorized_array[i]}"
if (( width == 0 )); then
# For the first word in the line, add the word only and
# set the width.
if (( word_length > max_width )); then
# The word is too long to fit a single line, so try
# splitting it on secondary delimiters or put as
# many characters as possible on the line and the
# indented remainder as next array element.
if (( "${#delimiters}" > 1 )); then
# There are still secondary delimiters left, so
# split the word on them prior attempting to
# hard-wrap it.
argparser_fold "${delimiters:1}" "${max_width}" \
"${indentation}" "${word}"
elif [[ "${word}" == *$'\e'* ]]; then
# The word contains a style, whose length must
# not be taken into account for gauging the word
# length. Thus, append one character at a time
# to the line, only increasing the word length
# when no style is being processed. As soon as
# the word length approaches the maximum width,
# end the current line with the ANSI escape
# sequence for the end of colors and styles,
# append a backslash, and add the word's last
# pattern to the next line. Finally, add the
# word's remainder to the next line.
word_length=0
is_style=false
style=""
for (( j = 0; j < "${#word}"; j++ )); do
folded_array[-1]+="${word:j:1}"
if [[ "${is_style}" == true ]]; then
if [[ "${word:j:1}" == "m" ]]; then
is_style=false
fi
style+="${word:j:1}"
continue
fi
if [[ "${word:j:1}" == $'\e' ]]; then
is_style=true
style=$'\e'
continue
fi
(( word_length++ ))
if (( word_length == max_width - 1 )); then
if [[ "${style}" == *"0m" ]]; then
folded_array[-1]+="\\"
folded_array+=("${whitespace}")
else
folded_array[-1]+=$'\e[0m\\'
folded_array+=("${whitespace}${style}")
fi
break
fi
done
folded_array[-1]+="${word:j+1}"
else
# The word doesn't contain any style, so string
# slicing works correctly.
folded_array[-1]+="${word::max_width-1}\\"
folded_array+=("${whitespace}${word:max_width-1}")
fi
(( width = word_length - max_width + indentation ))
else
# The word is short enough to fit the line, so add
# it directly.
folded_array[-1]+="${word}"
width="${word_length}"
fi
elif (( width + word_length + 1 > max_width )); then
# As the line with the appended word (and the preceding
# delimiter) would be too long, put the word as indented
# next array element and set the width. If the style
# pattern matches, and the style doesn't end with the
# line, end the current line with the ANSI escape
# sequence for the end of colors and styles and add the
# line's last pattern to the next line. In both cases,
# end the current line with the delimiter, if it isn't a
# space.
if [[ "${folded_array[-1]}" =~ ${pattern} \
&& "${folded_array[-1]}" != *"0m" ]]
then
style="${BASH_REMATCH[1]}"
if [[ "${delimiters::1}" == " " ]]; then
folded_array[-1]+=$'\e[0m'
else
folded_array[-1]+=$'\e[0m'"${delimiters::1}"
fi
folded_array+=("${whitespace}${style}${word}")
else
if [[ "${delimiters::1}" != " " ]]; then
folded_array[-1]+="${delimiters::1}"
fi
folded_array+=("${whitespace}${word}")
fi
(( width = word_length + indentation ))
else
# For any other word, add the word and a leading
# delimiter (otherwise, the words would be concatenated
# without separation since the splitting removed any
# delimiters). Increase the width appropriately.
folded_array[-1]+="${delimiters::1}${word}"
(( width += word_length + 1 ))
fi
done
done
}
function argparser_join() {
# Join an array by delimiting characters.
#
# Arguments:
# - $1: the separator used upon joining as delimiter
# - $@: the array to join
#
# Nonlocals:
# - joined_array: the joined array
# Define the local variables.
local array
local delimiter
# Read the arguments.
delimiter="$1"
shift
array=("$@")
# Join the array.
printf -v joined_array '%s' "${array[@]/#/"${delimiter}"}"
joined_array="${joined_array#"${delimiter}"}"
}
function argparser_seq() {
# Create a sequence of integer or string values, ranging from the
# start to the stop (inclusive), with a given step size.
#
# Arguments:
# - $1: the argument identifier
# - $2: the start value (as integer or string)
# - $3: the stop value (as integer or string)
# - $4: the step size (as integer, default: 1)
#
# Nonlocals:
# - error_messages: the error messages, starting with "Error: "
# (translated), as indexed array
# - sequence: the created sequence
# Define the local variables.
local arg_key
local char
local decimals
local decimals_start
local decimals_step
local decimals_stop
local error_message
local i
local number
local offset
local start
local stop
local step
local translation
# Read the arguments.
arg_key="$1"
start="$2"
stop="$3"
step="${4:-1}"
if argparser_is_alpha "${start}" \
&& argparser_is_alpha "${stop}" \
&& argparser_is_int "${step}"
then
# For alphabetical character sequences, get the decimal value in
# the ASCII table for the start and stop.
printf -v start '%d' "'${start}"
printf -v stop '%d' "'${stop}"
elif argparser_is_int "${start}" \
&& argparser_is_int "${stop}" \
&& argparser_is_int "${step}"
then
# For entirely integer sequences, do nothing. This is required
# to simplify the subsequent comparisons.
:
elif (argparser_is_float "${start}" || argparser_is_int "${start}") \
&& (argparser_is_float "${stop}" || argparser_is_int "${stop}") \
&& (argparser_is_float "${step}" || argparser_is_int "${step}")
then
# For at least partially float sequences, get the decimal part
# (after the period) and count its length.
if [[ "${start}" == *.* ]]; then
decimals_start="${start#*.}"
decimals_start="${#decimals_start}"
else
decimals_start=0
fi
if [[ "${stop}" == *.* ]]; then
decimals_stop="${stop#*.}"
decimals_stop="${#decimals_stop}"
else
decimals_stop=0
fi
if [[ "${step}" == *.* ]]; then
decimals_step="${step#*.}"
decimals_step="${#decimals_step}"
else
decimals_step=0
fi
# Remove the period from the start, stop, and step values, as
# well as any leading 0, to prevent the numbers being
# interpreted as octal, instead of decimal. These leading zeros
# may result from the removal of the period, like "0.01" being
# transformed to "001", instead of "1".
start="${start/.}"
start="${start##+(0)}"
stop="${stop/.}"
stop="${stop##+(0)}"
step="${step/.}"
step="${step##+(0)}"
# Add as many trailing zeros to the start, stop, and step values
# as required to make them the same length, in terms of decimal
# digits. By this, the sequence can be created as integer
# sequence, below, and afterwards, by re-introducing the periods
# as appropriate, the numbers can be converted back to floats.
if (( decimals_start < decimals_step \
&& decimals_stop < decimals_step ))
then
(( decimals = decimals_step ))
while (( decimals_start < decimals_step )); do
start+="0"
(( decimals_start++ ))
done
while (( decimals_stop < decimals_step )); do
stop+="0"
(( decimals_stop++ ))
done
elif (( decimals_start < decimals_stop )); then
(( decimals = decimals_stop ))
while (( decimals_start < decimals_stop )); do
start+="0"
(( decimals_start++ ))
done
while (( decimals_step < decimals_stop )); do
step+="0"
(( decimals_step++ ))
done
else
(( decimals = decimals_start ))
while (( decimals_stop < decimals_start )); do
stop+="0"
(( decimals_stop++ ))
done
while (( decimals_step < decimals_start )); do
step+="0"
(( decimals_step++ ))
done
fi
else
# For any other data type, output an error message.
error_message="Error: The argument with the identifier \"\$1\" has "
error_message+="choice values given with \"\$2\" as start, \"\$3\" as "
error_message+="stop, and \"\$4\" as step size, while only integer, "
error_message+="float, or letter sequences are supported."
argparser_translate "Error arg def choice range" \
"${error_message}" "${arg_key}" "${start}" "${stop}" "${step}"
error_message="${translation}"
error_messages+=("${error_message}")
return
fi
# Create the actual sequence by iterating from the start to the stop
# value, incrementing or decrementing by the given step size as
# appropriate for the numerical order.
sequence=( )
if (( start <= stop )); then
for (( i = start; i <= stop; i += step )); do
sequence+=("${i}")
done
else
for (( i = start; i >= stop; i -= step )); do
sequence+=("${i}")
done
fi
if argparser_is_alpha "$2"; then
# If the start value (and, by this, also the stop and step
# values) had been given as alphabetical character, convert each
# number within the sequence to its hexadecimal representation
# and subsequently to the respective character in the ASCII
# table.
for i in "${!sequence[@]}"; do
char="${sequence[i]}"
printf -v char '%x' "${char}"
# shellcheck disable=SC2059 # Escape sequence in variable.
printf -v char "\x${char}"
sequence[i]="${char}"
done
elif argparser_is_float "$2" \
|| argparser_is_float "$3" \
|| argparser_is_float "$4"
then
# If either of the start, stop, and step value had been given as
# float, convert the integers in the sequence to floats by
# introducing a period at the decimal offset the most precise
# number had (like two digits for start=1.0, stop=1.5, and
# step=0.05).
for i in "${!sequence[@]}"; do
number="${sequence[i]}"
(( offset = "${#number}" - decimals ))
sequence[i]="${number::offset}.${number:offset}"
done
fi
}
function argparser_get_max() {
# Get the maximum value of an array's elements.
#
# Arguments:
# - $@: the array whose maximum value to find
#
# Nonlocals:
# - max_value: the array's element with the maximum value
# Define the local variables.
local -a array
local element
# Read the arguments.
array=("$@")
# Get the maximum value of the array's elements.
max_value="$1"
for element in "${array[@]:1}"; do
if (( element > max_value )); then
max_value="${element}"
fi
done
}