getopt.jsx

0.2.1 • Public • Published

getopt.jsx

Build Status

Synopsis

JSX version of node-getopt.

Code Example

import "getopt.jsx";
 
class _Main {
    static function main(argv : string[]) : void
    {
        var parser = new BasicParser('o:(output)h(help)', argv);
 
        var help = false;
        var output = '';
        var source = [] : string[];
 
        var opt = parser.getopt();
        while (opt)
        {
            switch (opt.option)
            {
            case 'o':
                output = opt.optarg;
                break;
            case 'h':
                help = true;
                break;
            default:
                source.push(opt.option);
                break;
            }
            opt = parser.getopt();
        }
        if (help || source.length == 0)
        {
            console.log('''
$ getopt-sample [options] source...
 
options:
  -o file, --output=file : set output file
  -h, --help             : show this message
            ''');
        }
        else
        {
            // do some task here
            console.log("source:", source.join(''));
            console.log("output:", output);
        }
    }
}

Motivation

It provides command line parsing feature to your cool JSX program.

Installation

If you want to use this library from other project, install like the following:

$ npm install getopt.jsx --save-dev

API Reference

class BasicParser(opt : string, argv : string[])

It receives command pattern as first parameter and actual options as second parameter.

BasicParser.getopt() : CommandOption

It returns analysing parameter. If it consumes all parameters, it returns `null`.

class CommandOption

it has `option`, `optarg` parameter. If argument has parameter, option is a short name of command and
parameter is set to `optarg` (see case sentenses of the above sample).

If needed parameter is not provided, `error` flag become `true`.

Extra arguments are set to option (see default block of the above sample).

Option Pattern

s(long)

it become flag option (no parameter). BasicParser can accept `-s` and `--long`.

s:(long)

it become flag option with parameter. BasicParser can accept `-s option` and `--long=option`.

Development

Repository

Run Test

$ grunt test

Build

# Generate API reference 
$ grunt doc
 
# Build application or library for JS project 
$ grunt build

Author

License

MIT

Complete license is written in LICENSE.md.

Original Version

Readme

Keywords

Package Sidebar

Install

npm i getopt.jsx

Weekly Downloads

1

Version

0.2.1

License

MIT

Last publish

Collaborators

  • shibu