From e24d68612a0048da55539684f5cd3da4f3ed85b0 Mon Sep 17 00:00:00 2001 From: Martin Poelstra Date: Wed, 22 Oct 2014 12:20:07 +0200 Subject: [PATCH] Add typings for 'yargs'. --- CONTRIBUTORS.md | 1 + yargs/yargs-tests.ts | 157 +++++++++++++++++++++++++++++++++++++++++++ yargs/yargs.d.ts | 116 ++++++++++++++++++++++++++++++++ 3 files changed, 274 insertions(+) create mode 100644 yargs/yargs-tests.ts create mode 100644 yargs/yargs.d.ts diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 84f0c9190f..ab6dd4d37f 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -421,6 +421,7 @@ All definitions files include a header with the author and editors, so at some p * [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js) (by [Michel Salib](https://github.com/michelsalib)) * [xpath](https://github.com/goto100/xpath) (by [Andrew Bradley](https://github.com/cspotcode)) * [XRegExp](http://xregexp.com/) (by [Bart van der Schoor](https://github.com/Bartvds)) +* [yargs](https://github.com/chevex/yargs) (by [Martin Poelstra](https://github.com/poelstra)) * [YouTube](https://developers.google.com/youtube/) (by [Daz Wilkin](https://github.com/DazWilkin/)) * [YouTube Analytics API](https://developers.google.com/youtube/analytics/) (by [Frank M](https://github.com/sgtfrankieboy)) * [YouTube Data API](https://developers.google.com/youtube/v3/) (by [Frank M](https://github.com/sgtfrankieboy/)) diff --git a/yargs/yargs-tests.ts b/yargs/yargs-tests.ts new file mode 100644 index 0000000000..4e6d742a20 --- /dev/null +++ b/yargs/yargs-tests.ts @@ -0,0 +1,157 @@ +// Type definition tests for yargs +// Project: https://github.com/chevex/yargs +// Definitions by: Martin Poelstra +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +import yargs = require('yargs'); + +// Examples taken from yargs website +// https://github.com/chevex/yargs + +// With yargs, the options be just a hash! +function xup() { + var argv = yargs.argv; + + if (argv.rif - 5 * argv.xup > 7.138) { + console.log('Plunder more riffiwobbles!'); + } + else { + console.log('Drop the xupptumblers!'); + } +} + +// And non-hyphenated options too! Just use argv._! +function nonopt() { + var argv = yargs.argv; + console.log('(%d,%d)', argv.x, argv.y); + console.log(argv._); +} + +// Yargs even counts your booleans! +function count() { + var argv = yargs + .count('verbose') + .alias('v', 'verbose') + .argv; + + var VERBOSE_LEVEL: number = argv.verbose; + + function WARN() { VERBOSE_LEVEL >= 0 && console.log.apply(console, arguments); } + function INFO() { VERBOSE_LEVEL >= 1 && console.log.apply(console, arguments); } + function DEBUG() { VERBOSE_LEVEL >= 2 && console.log.apply(console, arguments); } +} + +// Tell users how to use yer options and make demands. +function divide() { + var argv = yargs + .usage('Usage: $0 -x [num] -y [num]') + .demand(['x', 'y']) + .argv; + + console.log(argv.x / argv.y); +} + +// After yer demands have been met, demand more! Ask for non-hypenated arguments! +function demand_count() { + var argv = yargs + .demand(2) + .argv; + console.dir(argv); +} + +// EVEN MORE SHIVER ME TIMBERS! +function default_singles() { + var argv = yargs + .default('x', 10) + .default('y', 10) + .argv + ; + console.log(argv.x + argv.y); +} +function default_hash() { + var argv = yargs + .default({ x: 10, y: 10 }) + .argv + ; + console.log(argv.x + argv.y); +} + +// And if you really want to get all descriptive about it... +function boolean_single() { + var argv = yargs + .boolean('v') + .argv + ; + console.dir(argv.v); + console.dir(argv._); +} +function boolean_double() { + var argv = yargs + .boolean(['x', 'y', 'z']) + .argv + ; + console.dir([argv.x, argv.y, argv.z]); + console.dir(argv._); +} + +// Yargs is here to help you... +function line_count() { + var argv = yargs + .usage('Count the lines in a file.\nUsage: $0') + .example('$0 -f', 'count the lines in the given file') + .demand('f') + .alias('f', 'file') + .describe('f', 'Load a file') + .argv + ; +} + +// Below are tests for individual methods. +// Not all methods are covered yet, and neither are all possible invocations of methods. + +function Argv_parsing() { + var argv1 = yargs.argv; + var argv2 = yargs(['-x', '1', '-y', '2']).argv; + var argv3 = yargs.parse(['-x', '1', '-y', '2']); + console.log(argv1.x, argv2.x, argv3.x); +} + +function Argv$options() { + var argv1 = yargs + .options('f', { + alias: 'file', + default: '/etc/passwd', + }) + .argv + ; + + var argv2 = yargs + .alias('f', 'file') + .default('f', '/etc/passwd') + .argv + ; +} + +function Argv$help() { + var yargs1 = yargs + .usage("$0 -operand1 number -operand2 number -operation [add|subtract]"); + var s: string = yargs1.help(); +} + +function Argv$showHelpOnFail() { + var argv = yargs + .usage('Count the lines in a file.\nUsage: $0') + .demand('f') + .alias('f', 'file') + .describe('f', 'Load a file') + .showHelpOnFail(false, "Specify --help for available options") + .argv; +} + +function Argv$showHelp() { + var yargs1 = yargs + .usage("$0 -operand1 number -operand2 number -operation [add|subtract]"); + yargs1.showHelp(); +} diff --git a/yargs/yargs.d.ts b/yargs/yargs.d.ts new file mode 100644 index 0000000000..c095835261 --- /dev/null +++ b/yargs/yargs.d.ts @@ -0,0 +1,116 @@ +// Type definitions for yargs +// Project: https://github.com/chevex/yargs +// Definitions by: Martin Poelstra +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "yargs" { + + module yargs { + interface Argv { + argv: any; + (...args: any[]): any; + parse(...args: any[]): any; + + alias(shortName: string, longName: string): Argv; + alias(aliases: { [shortName: string]: string }): Argv; + alias(aliases: { [shortName: string]: string[] }): Argv; + + default(key: string, value: any): Argv; + default(defaults: { [key: string]: any}): Argv; + + demand(key: string, msg: string): Argv; + demand(key: string, required?: boolean): Argv; + demand(keys: string[], msg: string): Argv; + demand(keys: string[], required?: boolean): Argv; + demand(positionals: number, required?: boolean): Argv; + demand(positionals: number, msg: string): Argv; + + require(key: string, msg: string): Argv; + require(key: string, required: boolean): Argv; + require(keys: number[], msg: string): Argv; + require(keys: number[], required: boolean): Argv; + require(positionals: number, required: boolean): Argv; + require(positionals: number, msg: string): Argv; + + required(key: string, msg: string): Argv; + required(key: string, required: boolean): Argv; + required(keys: number[], msg: string): Argv; + required(keys: number[], required: boolean): Argv; + required(positionals: number, required: boolean): Argv; + required(positionals: number, msg: string): Argv; + + requiresArg(key: string): Argv; + requiresArg(keys: string[]): Argv; + + describe(key: string, description: string): Argv; + describe(descriptions: { [key: string]: string }): Argv; + + option(key: string, options: Options): Argv; + option(options: { [key: string]: Options }): Argv; + options(key: string, options: Options): Argv; + options(options: { [key: string]: Options }): Argv; + + usage(message: string, options?: { [key: string]: Options }): Argv; + usage(options?: { [key: string]: Options }): Argv; + + example(command: string, description: string): Argv; + + check(func: (argv: { [key: string]: any }, aliases: { [alias: string]: string }) => boolean): Argv; + check(func: (argv: { [key: string]: any }, aliases: { [alias: string]: string }) => string): Argv; + + boolean(key: string): Argv; + boolean(keys: string[]): Argv; + + string(key: string): Argv; + string(keys: string[]): Argv; + + config(key: string): Argv; + config(keys: string[]): Argv; + + wrap(columns: number): Argv; + + strict(): Argv; + + help(): string; + help(option: string, description?: string): Argv; + + version(version: string, option: string, description?: string): Argv; + + showHelpOnFail(enable: boolean, message?: string): Argv; + + showHelp(func?: (message: string) => any): Argv; + + /* Undocumented */ + + normalize(key: string): Argv; + normalize(keys: string[]): Argv; + + implies(key: string, value: string): Argv; + implies(implies: { [key: string]: string }): Argv; + + count(key: string): Argv; + count(keys: string[]): Argv; + + fail(func: (msg: string) => any): void; + } + + interface Options { + type?: string; + alias?: any; + demand?: any; + required?: any; + require?: any; + default?: any; + boolean?: any; + string?: any; + count?: any; + describe?: any; + description?: any; + desc?: any; + requiresArg?: any; + } + } + + var yargs: yargs.Argv; + export = yargs; +}