Merge pull request #5088 from kcauchy/master

node-getopt was added.
This commit is contained in:
Horiuchi_H
2015-07-28 14:19:28 +09:00
2 changed files with 226 additions and 0 deletions
+97
View File
@@ -0,0 +1,97 @@
/// <reference path="../node/node.d.ts" />
/// <reference path="node-getopt.d.ts" />
import Getopt = require('node-getopt');
function help() {
// examples/help.js
// Works with help
var getopt = new Getopt([
['s' , '' , 'short option.'],
['' , 'long' , 'long option.'],
['S' , 'short-with-arg=ARG' , 'option with argument'],
['L' , 'long-with-arg=ARG' , 'long option with argument'],
['' , 'color[=COLOR]' , 'COLOR is optional'],
['m' , 'multi-with-arg=ARG+' , 'multiple option with argument'],
['' , 'no-comment'],
['h' , 'help' , 'display this help']
]);
// Use custom help template instead of default help
// [[OPTIONS]] is the placeholder for options list
getopt.setHelp(
"Usage: node help.js [OPTION]\n" +
"node-getopt help demo.\n" +
"\n" +
"[[OPTIONS]]\n" +
"\n" +
"Installation: npm install node-getopt\n" +
"Respository: https://github.com/jiangmiao/node-getopt"
);
getopt.showHelp();
}
function onedragon() {
// examples/onedragon.js
var opt = require('node-getopt').create([
['s' , '' , 'short option.'],
['' , 'long' , 'long option.'],
['S' , 'short-with-arg=ARG' , 'option with argument'],
['L' , 'long-with-arg=ARG' , 'long option with argument'],
['' , 'color[=COLOR]' , 'COLOR is optional'],
['m' , 'multi-with-arg=ARG+' , 'multiple option with argument'],
['' , 'no-comment'],
['h' , 'help' , 'display this help']
])
.bindHelp()
.parseSystem();
console.info(opt);
}
function online(){
// node-getopt oneline example.
var opt = require('..').create([
['s' , '' , 'short option.'],
['' , 'long' , 'long option.'],
['S' , 'short-with-arg=ARG' , 'option with argument'],
['L' , 'long-with-arg=ARG' , 'long option with argument'],
['' , 'color[=COLOR]' , 'COLOR is optional'],
['m' , 'multi-with-arg=ARG+' , 'multiple option with argument'],
['' , 'no-comment'],
['h' , 'help' , 'display this help'],
['v' , 'version' , 'show version']
]) // create Getopt instance
.bindHelp() // bind option 'help' to default action
.parseSystem(); // parse command line
console.info(opt);
}
function simple() {
// examples/simple.js
// argv parse
// Getopt = require('node-getopt');
// Getopt arguments options
// '=': has argument
// '[=]': has argument but optional
// '+': multiple option supported
var getopt = new Getopt([
['s'],
['S' , '='],
['' , 'long-with-arg=ARG'],
['m' , '=+'],
['' , 'color[=COLOR]'],
['h' , 'help']
]).bindHelp();
// process.argv needs slice(2) for it starts with 'node' and 'script name'
// parseSystem is alias of parse(process.argv.slice(2))
// opt = getopt.parseSystem();
var opt = getopt.parse(process.argv.slice(2));
console.info(opt);
}
+129
View File
@@ -0,0 +1,129 @@
// Type definitions for node-getopt 0.2.3
// Project: https://github.com/jiangmiao/node-getopt
// Definitions by: Karl.M.Cauchy <https://github.com/kcauchy>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/**
* Type Script Declaration for node-getopt
*/
declare module "node-getopt" {
interface StringMap {
[index: string]: string;
}
/**
* Parsed options.
*/
class ParsedOption {
public argv: string[];
public options: StringMap;
constructor(argv: string[], options: StringMap);
public empty() : boolean;
}
interface EventCallback {
(arguments: string[], options: StringMap) : void;
}
interface ErrorFunc {
(exception: Error): void;
}
interface OptionConfigurationArray {
[index: number]: string[];
}
class Getopt {
static HAS_ARGUMENT : boolean;
static NO_ARGUMENT : boolean;
static MULTI_SUPPORTED : boolean;
static SINGLE_ONLY : boolean;
static VERSION : string;
/**
* options is a set of option. each option contains 3 fields.
* [short_name, long_name_with_definition, comment]
* Definition:
* * '=ARG': has argument
* * '[=ARG]': has argument but optional
* * '+': multiple option supported
*
* ARG can be replaced by any word.
* @param options
*/
public constructor(options: any[]);
/**
* after parsing, trigger the action if optionName is found.
* the 'this' in action will be the instance of Getopt.
* @param name
* @param cb
*/
public on(name: string, cb: EventCallback) : Getopt;
public emit(name: string, cb: EventCallback) : Getopt;
/**
* parse argv
*
* Returns: {argv: '...', options: {...}}
*
*/
public parse(argv: string[]): ParsedOption;
/**
* alias of parse(process.argv.slice(2))
*/
public parse_system(): ParsedOption;
public parseSystem():ParsedOption;
/**
* Set help template. the placeholders will be replaced by getopt.
*
* Placeholders:
* * [[OPTIONS]] - The options list
*
* Returns: String
* @param help
*/
public setHelp(help: string): Getopt;
/**
* console.info(getopt.getHelp());
*/
public showHelp():Getopt;
/**
* Get the help generated.
*/
public getHelp(): string;
/**
* set help template to HELP if HELP is not empty.
* bind 'help' option to default action, show help and exit with 0.
* @param help
*/
public bindHelp(help?:string): Getopt;
public getVersion(): string;
static getVersion():string;
/**
* when parse failed callback will be trigger. default is display error message and exit with 1.
* @param errorFunc
*/
public error(errorFunc: ErrorFunc) : Getopt;
/**
* equals new Getopt(options)
* @param options
*/
static create(options: string[]): Getopt;
}
export = Getopt;
}