jsish
Modules
Not logged in

Overview

A Jsi module is code that:

Here is an example module ./fileSize.jsi:

#!/usr/bin/env jsish
function fileSize(fargs:array|string=void, opts:object=void) {

    var options = { // Count bytes in files
        debug       :0,     // Debugging level
        max         :10     // Max number of files
    };
    var that = {
        package:'fileSize', // Note: "package" is used by Jsi_Opts to support "-h".
        cnt:0
    };
    
    function getLength(fargs:array|string) {
        if (typeof fargs === 'string')
            fargs = [fargs];
            
        for (var i in fargs) {
            if (i>=that.max) break;
            if (that.debug)
                puts('Count: '+fargs[i]);
            that.cnt += File.size(fargs[i]);
        }
        return that.cnt;
    }

    Jsi_Opts(that, options, opts);
    
    if (fargs)
        return getLength(fargs);
        
    that.getLength = getLength; // Return object-command
    return that;
}

provide('fileSize');

if (Info.isMain()) {
    puts(Jsi_Main('fileSize'));
}

Invoked as:

./fileSize.jsi -debug 1 file1 file2 file3

Leading switches are passed in opts with the remaining arguments in fargs.

Note that options coming in from the command-line must be primitives.


Programatic

Calling a module with no arguments causes it to just return that. This allows the it to be called programatically:

require('fileSize');
var fcmd = fileSize();
fcmd.getLength('Makefile');
fcmd.getLength(['a','b']);
puts(fcmd.cnt);

Sections

The 7 parts of a module are:

Section Description
function All code is wrapped in a function with the same name as the file basename, providing bindings for incoming arguments.
options The var options = object itemizes the all options that can come in via the opts parameter.
that Local object state is stored in the that object. All defined and incoming options will be copied into that.
Jsi_OptsThe call Jsi_Opts(that, options, opts) performs option parsing as well as implementing -h help output
provideThe provide statement makes the module available as a package.
Info.isMain The Info.isMain() command returns true if the current file was the one invoked from the command-line.
Jsi_Main The Jsi_Main() command parses command-line options and then invokes the current module.

Help

Help is available for a module by calling it with the single argument -h

/fileSize.jsi -h
/zvfs/lib/JsiUtil.jsi:121: error: throw: 
Count bytes in files

Options/defaults:
    -debug      0       // Debugging level
    -max        10      // Max number of files

ERROR

Help Limitations

Help information gets extracted from options by Jsi_Opts, with the following limitations: