17.1 Arguments

The main module contains, at the root level, a block of code like the following:

declare -i ARGC=0 ARGC1=0 ARG1ST=0
declare -a ARGV ARGV1

for ((ARGC1=0; $# > 0; ++ARGC1))
do
    ARGV1[$ARGC1]=$1
    shift
done

this block is executed when MBFL (and the script that loads it) is evaluated. Its purpose is to store command line arguments in the global array ARGV1 and the number of command line arguments in the global variable ARGC1; the variable ARG1ST references the next argument in ARGV1 that must be processed.

The global array ARGV and the global variable ARGC are predefined and should be used by the mbfl_getopts_* functions to store non–option command line arguments. Processing performed by the getopts module must happen after processing performed by the actions module.

Example:

$ script --gulp wo --gasp=123 wa

if the script makes use of MBFL, the strings wo and wa will go into ARGV and ARGC will be set to 2. The option arguments are processed and some action is performed to register them.

We can access the non–option arguments with the following code:

for ((i=0; $i < $ARGC; ++i))
do
    # do something with ${ARGV[$i]}
done

When using the actions module: the first arguments can be interpreted as special values that select an action to be performed by the script; in such case the first argument is removed from the ARGV array, so that processing the other arguments is not affected. Introduction to action trees.


This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.