Include shell libraries modules
Allow loading of libraries modules which simply are script files containing libraries of functions organized in classes.
A module can contain one or more classes, where a class is a set of homogeneous functions with names prefixed with the name of the class.
For example, the module main contains classes as array
, hash
etc.,
with functions of class array
starting with the prefix array_
.
For a module containing only one class, the name of the class is the name of the module, as in the module trap
where all functions start with trap_
.
Use the command module.doc <function_name>
to see the documentation for a function (see an example)
- _MODULE__CLASS_TO_PATH (Hash): Associate each class defined in modules to the script's path containing it: it is set by the
module_import
function or his aliasmodule.import
- _MODULE__SHDOC_DIR (String): Cache the path of the package github.com/vargiuscuola/shdoc (used by the function
module_doc
and his aliasmodule.doc
- _MODULE__IMPORTED_MODULES (Array): Imported modules
- module_import()
- module_is-imported()
- module_get-class-path_()
- module_list-class-functions()
- module_list-classes()
- module_doc()
Import a module, i.e. a shell library path which is sourced by the current function.
The provided library path can be relative or absolute. If it's relative, the library will be searched in the following paths:
- calling path
- current path (where the current script reside)
- default library path (/lib/sh in Linux or /c/linux-lib/sh in Windows)
If the requested module is correctly
source
-ed, its path is added to the list of imported modules stored in global variable_MODULE__IMPORTED_MODULES
. Also, the classes defined inside the module are linked to the path of the module through the associative array_MODULE__CLASS_TO_PATH
: the classes contained inside a module are declared inside the module itself through the array _${capitalized module name}__CLASSES. If this variable is not defined, is expected that only one class is defined with the same name of the module.
For example, the modulegithub/vargiuscuola/std-lib.bash/args
, which doesn't declare the variable_ARGS_CLASSES
, is supposed to define only one class namedargs
.
The concept of class in this context refers to an homogeneous set of functions all starting with the same prefix<class name>_
as inargs_check-number
andargs_parse
.
- module.import
- $1 (String): Module path. Shell extension
.sh
can be omitted
- -f|--force: Force the import of the module also if already imported
- Standard
$ module.import github/vargiuscuola/std-lib.bash/main
$ module.import --force args
Check whether a module has been imported.
- module.is-imported
- $1 (String): Module name
- 0 if module is imported, 1 otherwise.
Return the path of the provided class.
- module.get-class-path_
- $1 (String): Class name
- Path of the file where the class is defined (see the Overview for an explanation of the concept of class in this context).
$ cd /var/cache
$ module.abs-path_ "../lib"
# return __="/var/lib"
List the functions of the provided class, which must be already loaded with module.import
or at least source
-ed.
- module.list-class-functions
- $1 (String): Class name
- List of functions which are part of the provided class
$ module.list-class-functions args
args.check-number
args.parse
args_check-number
args_parse
args_to_str_
List the classes defined inside a module.
- module.list-classes
- $1 (String): Module name.
- List of classes defined in the provided module
$ module.list-classes main
hash
main
collection
datetime
list
shopt
array
Print the documentation for the provided function name.
- module.doc
- $1 (String): Function or alias name
- Print the documentation for the function
Return normalized absolute path.
- module.abs-path_
- $1 (String): Path
- Normalized absolute path
$ cd /var/cache
$ module.abs-path_ "../lib"
# return __="/var/lib"