File size: 18,116 Bytes
5a600cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     | Website:  https://openfoam.org
#   \\  /    A nd           | Copyright (C) 2020-2026 OpenFOAM Foundation
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# Script
#     foamGenerateBashCompletion
#
# Description
#     Generate the bash_completion file
#
#------------------------------------------------------------------------------

usage() {
    cat<<USAGE

Usage: ${0##*/}

USAGE
}

error() {
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    usage
    exit 1
}

file=$WM_PROJECT_DIR/etc/config.sh/bash_completion

case "$1" in
(-h | -help)
    usage && exit 0
    ;;
(-l | -local)
    file=${file##*/}
    shift 1
    ;;
-*)
    error "$1 is not a valid option/filename"
    ;;
esac

#------------------------------------------------------------------------------

banner () {
    cat<<EOF
#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\\\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\\\    /   O peration     | Website:  https://openfoam.org
#   \\\\  /    A nd           | Copyright (C) 2017-$(date +%Y) OpenFOAM Foundation
#    \\\\/     M anipulation  |
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# File
#     etc/config.sh/bash_completion
#
# Description
#     Bash [TAB] completion file from OpenFOAM
#     Sourced from /etc/bashrc
#
#------------------------------------------------------------------------------
# shellcheck disable=SC2155,SC2207,SC2086

EOF
}

optionFunctions () {
    cat<<EOF
_region ()
{
    find . -maxdepth 3 -name polyMesh -type d 2> /dev/null | \\
        awk -F '/' '{print \$(NF-1)}' | \\
        grep -v constant | \\
        sort -u | xargs
}

_mesh ()
{
    for d in constant system
    do
        find "\$d/meshes" -mindepth 1 -maxdepth 1 -type d 2> /dev/null | \\
        awk -F '/' '{print \$(NF)}'
    done | sort -u | xargs
}

_solver ()
{
    foamToC -table solver | sed '1,/Contents/d' | awk '{print \$1}' | xargs
}

_table ()
{
    [ -z "\${1// }" ] && \\
        foamToC -tables | \\
        sed '1,/Run-time/d' | \\
        sed 's/[[:space:]]\{4\}//' | grep -v ' ' && exit 0
    foamToC -tables | sed '1,/Run-time/d' | tr -d " "
}

_func ()
{
    foamPostProcess -list | sed -n '/^(/,/^)/p' | grep -v '[()]'
}

_optSet () {
    _used="\$1"
    _opts="\$2"

    for _o in \${_opts//-/\\\\-}
    do
        echo "\$_used" | grep -wq "\$_o" && return 0
    done

    return 1
}

_removeOpts () {
    _opts="\$1"
    _removes="\$2"

    for _r in \${_removes//-/\\\\-}
    do
        _opts="\${_opts/\$_r/}"
    done

    echo "\$_opts"
}

_infoArgs ()
{
    foamToC -all | \\
       grep -wx '[^ _]* [^ ]*.so' | \\
       awk '{print \$1}' | \\
       tr '<>,' '\n' | \\
       sort -u | \\
       xargs
}

_findArgs () {
    _dir="\$1"

    [ -d "\$_dir" ] || return 1

    _name="*"
    echo "\$_dir" | grep -wqE "^(\$FOAM_SRC|\$FOAM_APP)" && _name="*.[CHL]"

    find "\$_dir" -name "\$_name" -type f | \\
        awk -F/ '{print \$NF}' | \\
        sort -u | \\
        xargs
}

_dimensions ()
{
    foamUnits | \\
        sed -n -e "/^Fundamental Dimensions/,/^$/p" \\
               -e "/^Derived Dimensions/,/^$/p" | \\
        grep "^[\t ]" | tr -d "\[\]" | xargs
}

_units ()
{
    foamUnits | \\
        sed -n -e "/^Fundamental Units/,/^$/p" \\
               -e "/^Derived Units/,/^$/p" \\
               -e "/^Scaled Units/,/^$/p" \\
               -e "/^Derived-Scaled Units/,/^$/p" | \\
        grep "^[\t ]" | tr -d "\[\]" | xargs
}

EOF
}

declareLocals () {
    cat <<EOF
    local cur="\${COMP_WORDS[COMP_CWORD]}"
    local prev="\${COMP_WORDS[COMP_CWORD-1]}"
    local line=\${COMP_LINE}
    local used=\$(echo "\$line" | grep -oE "\-[a-zA-Z]+ ")
EOF
}

caseStart () {
    cat <<EOF
    [ "\$COMP_CWORD" = 1 ] || \\
    case "\$prev" in
EOF
}

caseEnd () {
    cat <<EOF
    esac
    COMPREPLY=( \$(compgen -W "\${opts}" \$extra -- \${cur}) )
EOF
}

isScript () {
    file "$(command -v "$1")" | grep -q "POSIX shell script"
}

_foamFind ()
{
    cat <<EOF
_foamFind_ ()
{
$(declareLocals)

    opts="\\
        -applications \\
        -coded \\
        -C \\
        -dir \\
        -edit \\
        -files \\
        -help \\
        -H \\
        -isearch \\
        -lnInclude \\
        -modules \\
        -numbers \\
        -options \\
        -print \\
        -search \\
        -tutorials"

    for o in \$used ; do opts="\${opts/\$o/}" ; done

    # use only one of '-C' and '-H'
    local actions="-C -H"
    _optSet "\$used" "\$actions" && opts="\$(_removeOpts "\$opts" "\$actions")"

    # use only one of '-edit', '-isearch', '-search', '-print'
    local actions="-edit -isearch -search -print"
    _optSet "\$used" "\$actions" && opts="\$(_removeOpts "\$opts" "\$actions")"

    # use only one of '-applications', '-dir', '-modules', '-tutorials'
    local paths="-applications -dir -modules -tutorials"
    _optSet "\$used" "\$paths" && opts="\$(_removeOpts "\$opts" "\$paths")"

    local dir=\$FOAM_SRC
    echo "\$used" | grep -wq "\-applications" && dir=\$FOAM_APP
    echo "\$line" | grep -wq "\-dir" && \\
        dir="\$(echo "\$line" | grep -ow "\-dir[\t ]*[^ ]*" | cut -d" " -f2)"
    echo "\$used" | grep -wq "\-modules" && dir=\$FOAM_MODULES
    echo "\$used" | grep -wq "\-tutorials" && dir=\$FOAM_TUTORIALS

    echo "\$line" | grep -wq "[^ ]*.[CHL]" || opts="\$opts \$(_findArgs "\$dir")"
    extra=""

$(caseStart)
        -coded)
            opts="" ; extra="-d -f" ;;
        -dir)
            opts="" ; extra="-d" ;;
       *) ;;
$(caseEnd)
}
complete -o filenames -o nospace -F _foamFind_ foamFind

EOF
}

# shellcheck disable=SC2154
_foamGet () {
    cat <<EOF
_foamGet_ ()
{
$(declareLocals)

    _searchDirs () {
        _dirs="\\
            \${HOME}/.OpenFOAM/\$WM_PROJECT_VERSION \\
            \${HOME}/.OpenFOAM"
        [ -n "\$WM_PROJECT_SITE" ] && _dirs=" \\
                \$WM_PROJECT_SITE/\$WM_PROJECT_VERSION/etc \\
                \$WM_PROJECT_SITE/etc"
        _dirs+=" \\
            \$WM_PROJECT_INST_DIR/site/\$WM_PROJECT_VERSION/etc \\
            \$WM_PROJECT_INST_DIR/site/etc \\
            \$FOAM_ETC/caseDicts"

        _files=""
        for _d in \$_dirs
        do
            [ -d "\$_d" ] && \\
                _files="\$_files \$(find "\$_d" -type f ! -name "*.*" -exec basename {} \;)"
        done
        echo "\${_files}" | xargs -n 1 | sort -u
    }

    opts="-case -ext -help -no-ext -target -region"
    for o in \$used ; do opts="\${opts/\$o/}" ; done
    extra=""
    opts="\${opts} \$(_searchDirs)"

$(caseStart)
        -case|-target)
            opts="" ; extra="-d" ;;
        -ext)
            opts="" ; extra="" ;;
        -region)
            opts="\$(_region)"; extra="" ;;
       -*) ;;
        *)
            case "\${COMP_WORDS[COMP_CWORD-2]}" in
                -case|-ext|-target|-region) ;;
                *) opts=""; extra="" ;;
            esac
            ;;
$(caseEnd)
}
complete -o filenames -o nospace -F _foamGet_ foamGet

EOF
}

# shellcheck disable=SC2154
_foamCloneCase () {
    cat <<EOF
_foamCloneCase_ ()
{
$(declareLocals)

    opts="-add -help -latestTime -no-orig -no-scripts \
          -processor -startFrom -template"
    for o in \$used ; do opts="\${opts/\$o/}" ; done
    extra="-d"

$(caseStart)
$(caseEnd)
}
complete -o filenames -o nospace -F _foamCloneCase_ foamCloneCase

EOF
}

# shellcheck disable=SC2154
_surfaceTransformPoints () {
    cat<<EOF
_surfaceTransformPoints_ ()
{
$(declareLocals)

    opts="-doc -help -srcDoc"
    for o in \$used ; do opts="\${opts/\$o/}" ; done
    extra="-d -f"

$(caseStart)
       -*) ;;
        *)
            case "\${COMP_WORDS[COMP_CWORD-2]}" in
                *) opts="";     extra="-d -f" ;;
            esac
            ;;
$(caseEnd)
}
complete -o filenames -o nospace -F _surfaceTransformPoints_ surfaceTransformPoints

EOF
}

# shellcheck disable=SC2154
_foamInfo () {
    cat<<EOF
_foamInfo_ ()
{
$(declareLocals)

    opts="-all -browser -help -keyword -web"
    for o in \$used ; do opts="\${opts/\$o/}" ; done
    opts="\$opts \$(_infoArgs) \$(ls \$FOAM_APPBIN)"
    extra=""

$(caseStart)
        -browser)
            opts="" ; extra="" ;;
       *) ;;
$(caseEnd)
}
complete -o filenames -o nospace -F _foamInfo_ foamInfo

EOF
}

# shellcheck disable=SC2154
_foamUnits () {
    cat<<EOF
_foamUnits_ ()
{
    # Note: 'used' is redundant here
$(declareLocals)

    # Default: '-help', '-all', units and dimensions
    opts="-help -all \$(_units) \$(_dimensions)"

    # '-help' or '-all' used: no further options
    echo "\$line" | grep -qE -- "-(help|all)" && opts=""

    # one dimension used: no further options
    _dimensions | grep -wq "\$prev" && opts=""

    # one unit used: return units and '-value'
    _units | grep -wq "\$prev" && opts="\$(_units) -value"

    # one unit used: return units and '-value'
    _units | grep -wq "\$prev" && [ "\$COMP_CWORD" = 3 ] && opts="-value"

    extra=""

    COMPREPLY=( \$(compgen -W "\${opts}" \$extra -- \${cur}) )
}
complete -o filenames -o nospace -F _foamUnits_ foamUnits

EOF
}

# shellcheck disable=SC2154
_foamNewSolver () {
    cat<<EOF
_foamNewSolver_ ()
{
$(declareLocals)

    opts="-help \$(_solver)"
    for o in \$used ; do opts="\${opts/\$o/}" ; done
    extra=""

$(caseStart)
        -*) ;;
        *) opts="" ;;
$(caseEnd)
}
complete -o filenames -o nospace -F _foamNewSolver_ foamNewSolver

EOF
}

#------------------------------------------------------------------------------

apps="$(ls "$FOAM_APPBIN") \
      $(find "$WM_PROJECT_DIR/bin" -maxdepth 1 -type f | sort) \
      $(find "$WM_PROJECT_DIR/wmake" -maxdepth 1 -type f | sort)"

specialApps="\
    foamFind \
    foamGet \
    foamCloneCase \
    foamInfo \
    foamUnits \
    foamNewSolver \
    surfaceTransformPoints"

banner > "$file"
optionFunctions >> "$file"

for app in $apps
do
    appName="${app##*/}"

    echo "Configuring $appName"

    # If a special configurations is defined for this app, use it and continue
    echo "$specialApps" | \
        grep -qsw "$appName" && "_$appName" >> "$file" && continue

    # Get arguments. Anything on the usage line in <> or [] brackets.
    argList=$($app -help | \
           grep ^Usage | \
           awk -F '[]>]' '{for(i=1;i<=NF;++i) print $i}' | \
           awk -F ' [<[]' '{print $2}')

    # Categorise arguments ...

    # File arguments. Entries without "output", ending in "file".
    inputFileArgs=$(echo "$argList" | while read -r line
                    do
                        echo "$line" | grep -v output | grep -E "file$"
                    done)
    nInputFileArgs=$(echo "$inputFileArgs" | sed '/^$/d' | wc -l)

    # Directory arguments. Entries without "output", including "case" or "dir*".
    inputDirArgs=$(echo "$argList" | while read -r line
                   do
                       echo "$line" | grep -v output | grep -Ei "(case|dir).*"
                   done)
    nInputDirArgs=$(echo "$inputDirArgs" | sed '/^$/d' | wc -l)

    # Options. Anything in the between "options" and the next empty line.
    optList=$($app -help | \
              sed -n '/^options/,/^$/p' | \
              grep -E "^[\t ]*-" | \
              tr -s " ")
    [ -z "$optList" ] && continue

    # Categorise options ...

    arglessOpts="" # options without arguments
    dirOpts=""     # options with directory arguments
    fileOpts=""    # options with file arguments
    handlerOpts="" # file handler options -fileHandler
    rangesOpts=""  # ranges options -time
    argOpts=""     # options with unspecified arguments
    specialOpts="" # -solver, -table, -func, -region, -mesh

    while read -r line
    do
        # Clear the variable
        unset next

        # Get the option
        opt=$(echo "$line" | cut -d " " -f1 | tr -d " ")

        # Ignore "-solver" if app is a script
        [ "$opt" = "-solver" ] && isScript "$appName" && continue

        # Deal with quoted option arguments for -libs
        [ "$opt" = "-libs" ] && next=libList

        case "$opt" in
            -solver|-table|-func|-region|-mesh)
                specialOpts="$specialOpts $opt"
                continue
                ;;
        esac

        # Get the adjacent string in <> brackets
        [ "$next" ] || \
        next=$(echo "$line" | \
            sed 's/ \<.*\>.*//g' | \
            awk -F '>' '{print $1}' | \
            awk -F ' <' '{print $2}' |
            tr -d \) | \
            tr -d \()

        # Categorise by the the last word in the string
        case "${next##* }" in
            "")      arglessOpts="$arglessOpts $opt" ;;
            dir)     dirOpts="$dirOpts $opt" ;;
            file)    fileOpts="$fileOpts $opt";;
            handler) handlerOpts="$handlerOpts $opt";;
            ranges)  rangesOpts="$rangesOpts $opt";;
            *)       argOpts="$argOpts $opt";;
        esac
    done<<< "$optList"

    # Combine options into a single list
    # shellcheck disable=SC2086
    allOpts=$(printf '%s\n' \
        $arglessOpts $dirOpts $fileOpts $handlerOpts $rangesOpts $argOpts $specialOpts | \
        sort -u)

    # Write the completion function ...
    # shellcheck disable=SC2086
    {
        echo "_${appName}_ ()"
        echo "{"
        declareLocals
        echo ""

        # shellcheck disable=SC2027,SC2086
        echo "    opts=\""$allOpts"\""
        echo "    for o in \$used ; do opts=\"\${opts/\$o/}\" ; done"

        if [ ! "$nInputFileArgs" = 0 ]
        then
            echo "    extra=\"-d -f\""
        elif [ ! "$nInputDirArgs" = 0 ]
        then
             echo "    extra=\"-d\""
        else
             echo "    extra=\"\""
        fi

        echo ""
        caseStart

        if [ -n "$dirOpts" ]
        then
            printf "        %s)\n" "$(echo $dirOpts | tr " " "|")"
            echo "            opts=\"\" ; extra=\"-d\" ;;"
        fi

        if [ -n "$fileOpts" ]
        then
            printf "        %s)\n" "$(echo $fileOpts | tr " " "|")"
            echo "            opts=\"\" ; extra=\"-d -f\" ;;"
        fi

        if [ -n "$handlerOpts" ]
        then
            printf "        %s)\n" "$(echo $handlerOpts | tr " " "|")"
            echo "            opts=\"uncollated collated masterUncollated\" ; extra=\"\" ;;"
        fi

        if [ -n "$rangesOpts" ]
        then
            printf "        %s)\n" "$(echo $rangesOpts | tr " " "|")"
            echo "            opts=\"\$(foamListTimes -withZero 2> /dev/null)\" ; extra=\"\" ;;"
        fi

        if [ -n "$argOpts" ]
        then
            printf "        %s)\n" "$(echo $argOpts | tr " " "|")"
            echo "            opts=\"\" ; extra=\"\" ;;"
        fi

        for sopt in $specialOpts
        do
            printf "        %s) opts=\"\$(_%s \${cur})\" ;;\n" "$sopt" "${sopt#-}"
        done

        # Set argOpts to all options with arguments
        argOpts="$argOpts $dirOpts $fileOpts $handlerOpts $rangesOpts"

        # Get the max of nInputFileArgs and nInputDirArgs
        nMaxFileDirArgs=$nInputFileArgs
        [ "$nInputDirArgs" -gt "$nMaxFileDirArgs" ] && \
            nMaxFileDirArgs=$nInputDirArgs

        # Stop optional arguments once mandatory arguments are entered
        case "$nMaxFileDirArgs" in
            0) echo "       *) ;;"  ;;
            1)
                echo "       -*) ;;"
                echo "        *)"
                if [ -z "${argOpts// }" ]
                then
                    echo "            opts=\"\"; extra=\"\""
                else
                    echo "            case \"\${COMP_WORDS[COMP_CWORD-2]}\" in"
                    printf "                %s) ;;\n" \
                        "$(echo $argOpts | tr " " "|")"
                    echo "                *) opts=\"\"; extra=\"\" ;;"
                    echo "            esac"
                fi
                echo "            ;;"
                ;;
            *)
                echo "       -*) ;;"
                echo "        *) opts=\"\";;"
                ;;
        esac

        caseEnd
        echo "}"

        echo "complete -o filenames -o nospace -F _${appName}_ ${appName}"
        echo ""

    } >> "$file"
done

cat<<\EOF >> "$file"
#------------------------------------------------------------------------------
EOF


#------------------------------------------------------------------------------