From c35c62df44b9d0d2690e4753be80964a77340e74 Mon Sep 17 00:00:00 2001 From: karlcauchy Date: Mon, 27 Jul 2015 16:46:10 +0800 Subject: [PATCH 1/2] node-getopt was added. --- node-getopt/node-getopt-test.ts | 97 ++++++++++++++++++++++++ node-getopt/node-getopt.d.ts | 129 ++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 node-getopt/node-getopt-test.ts create mode 100644 node-getopt/node-getopt.d.ts diff --git a/node-getopt/node-getopt-test.ts b/node-getopt/node-getopt-test.ts new file mode 100644 index 000000000..83aa05053 --- /dev/null +++ b/node-getopt/node-getopt-test.ts @@ -0,0 +1,97 @@ +/// +/// + +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); + +} diff --git a/node-getopt/node-getopt.d.ts b/node-getopt/node-getopt.d.ts new file mode 100644 index 000000000..74787eed5 --- /dev/null +++ b/node-getopt/node-getopt.d.ts @@ -0,0 +1,129 @@ +// Type definitions for node-getopt 0.2.3 +// Project: https://github.com/jiangmiao/node-getopt +// Definitions by: Karl.M.Cauchy +// 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; +} From fb5315fc749a64f6f7512581fbb5d2a3e244d152 Mon Sep 17 00:00:00 2001 From: karlcauchy Date: Mon, 27 Jul 2015 17:03:15 +0800 Subject: [PATCH 2/2] Changed the definitions for author from email to github homepage. --- node-getopt/node-getopt.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node-getopt/node-getopt.d.ts b/node-getopt/node-getopt.d.ts index 74787eed5..9fec278b5 100644 --- a/node-getopt/node-getopt.d.ts +++ b/node-getopt/node-getopt.d.ts @@ -1,6 +1,6 @@ // Type definitions for node-getopt 0.2.3 // Project: https://github.com/jiangmiao/node-getopt -// Definitions by: Karl.M.Cauchy +// Definitions by: Karl.M.Cauchy // Definitions: https://github.com/borisyankov/DefinitelyTyped /**