From a670727a3721d3db44a20c3091454508fdcba2b1 Mon Sep 17 00:00:00 2001 From: Edward Sammut Alessi Date: Thu, 26 Oct 2017 17:56:50 +0200 Subject: [PATCH 1/5] Add typings for leo/args --- types/args/args-tests.ts | 67 +++++++++++++++++++++++++++++++ types/args/index.d.ts | 87 ++++++++++++++++++++++++++++++++++++++++ types/args/tsconfig.json | 22 ++++++++++ types/args/tslint.json | 1 + 4 files changed, 177 insertions(+) create mode 100644 types/args/args-tests.ts create mode 100644 types/args/index.d.ts create mode 100644 types/args/tsconfig.json create mode 100644 types/args/tslint.json diff --git a/types/args/args-tests.ts b/types/args/args-tests.ts new file mode 100644 index 0000000000..3430288cd3 --- /dev/null +++ b/types/args/args-tests.ts @@ -0,0 +1,67 @@ +import * as args from "args"; + +args + .option("opt1", "desc") + .option("opt2", "desc", false, (value: any): any => value) + .options([ + { + name: 'opt3', + description: 'desc', + defaultValue: 1, + init: (value: any) => { }, + }, + { + name: 'opt4', + description: 'desc', + }, + ]) + .command("cm1", "desc") + .command("cm2", "desc", (value: any): void => { }, ['a']) + .example("ex1", "desc") + .examples([ + { + usage: "ex2", + description: "desc", + }, + ]); + +args.parse(['~/bin/node', '~/dir', 'arg', '--param'], { + help: true, + name: "name", + version: true, + usageFilter: (a: any): any => a, + value: "value", + mri: { + args: ['a'], + alias: { + a: "b", + c: ['d'], + }, + boolean: ['wat'], + default: { + foo: 'bar', + }, + string: ['zulu'], + unknown: (param: string): boolean => true, + }, + minimist: { + string: ['string'], + boolean: ['string'], + alias: { + bar: 'foo', + foo: ['bar1', 'bar2'], + }, + default: { + foo: 'bar', + }, + stopEarly: true, + "--": false, + unknown: (param: string): boolean => true, + }, + mainColor: "yellow", + subColor: "dim" +}); + +args.showHelp(); + +const x: string = args.sub[0]; diff --git a/types/args/index.d.ts b/types/args/index.d.ts new file mode 100644 index 0000000000..4e77da737b --- /dev/null +++ b/types/args/index.d.ts @@ -0,0 +1,87 @@ +declare var args: Args.API; +export = args; + +declare namespace Args { + export interface IMriUnknownFunction { + (param: string): boolean + } + + export interface IMinimistUnknownFunction { + (param: string): boolean + } + + export interface IMriArguments { + args?: string[]; + alias?: { + [key: string]: string | string[] + }; + boolean?: string | string[]; + default?: { + [key: string]: any + }; + string?: string | string[]; + unknown?: IMriUnknownFunction; + } + + export interface IMinimistArguments { + string?: string | string[]; + boolean?: boolean | string | string[]; + alias?: { + [key: string]: string | string[] + }; + default?: { + [key: string]: any + }; + stopEarly?: boolean; + "--"?: boolean; + unknown?: IMinimistUnknownFunction; + } + + export interface IOptionInitFunction { + (value: any): any; + } + + export interface ICommandInitFunction { + (name: string, sub: {}[], options: {}[]): void; + } + + export interface IUsageFilterFunction { + (output: any): any; + } + + export interface IConfiguration { + help?: boolean; + name?: string; + version?: boolean; + usageFilter?: IUsageFilterFunction; + value?: string; + mri: IMriArguments; + minimist?: IMinimistArguments; + mainColor: string | string[]; + subColor: string | string[]; + } + + export interface IOption { + name: string; + description: string; + init?: IOptionInitFunction; + defaultValue?: any; + } + + export interface IExample { + usage: string; + description: string; + } + + export interface API { + sub: string[]; + + option(name: string | [string, string], description: string, defaultValue?: any, init?: IOptionInitFunction): API; + options(list: IOption[]): API; + command(name: string, description: string, init?: ICommandInitFunction, aliases?: string[]): API; + example(usage: string, description: string): API; + examples(list: IExample[]): API; + parse(argv: string[], options?: IConfiguration): { [key: string]: any }; + showHelp(): void; + } +} diff --git a/types/args/tsconfig.json b/types/args/tsconfig.json new file mode 100644 index 0000000000..ccc4d54297 --- /dev/null +++ b/types/args/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "args-tests.ts" + ] +} diff --git a/types/args/tslint.json b/types/args/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/args/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 17e36b8576cddb120ed2e5c974c1619a24de6870 Mon Sep 17 00:00:00 2001 From: Edward Sammut Alessi Date: Thu, 26 Oct 2017 18:46:27 +0200 Subject: [PATCH 2/5] Updates to follow guidelines --- types/args/index.d.ts | 156 ++++++++++++++++++--------------------- types/args/tsconfig.json | 3 +- 2 files changed, 75 insertions(+), 84 deletions(-) diff --git a/types/args/index.d.ts b/types/args/index.d.ts index 4e77da737b..b6048d2c5f 100644 --- a/types/args/index.d.ts +++ b/types/args/index.d.ts @@ -1,87 +1,77 @@ -declare var args: Args.API; +// Type definitions for args 3.0 +// Project: https://github.com/leo/args#readme +// Definitions by: Slessi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + export = args; -declare namespace Args { - export interface IMriUnknownFunction { - (param: string): boolean - } +declare namespace args { + let sub: string[]; - export interface IMinimistUnknownFunction { - (param: string): boolean - } - - export interface IMriArguments { - args?: string[]; - alias?: { - [key: string]: string | string[] - }; - boolean?: string | string[]; - default?: { - [key: string]: any - }; - string?: string | string[]; - unknown?: IMriUnknownFunction; - } - - export interface IMinimistArguments { - string?: string | string[]; - boolean?: boolean | string | string[]; - alias?: { - [key: string]: string | string[] - }; - default?: { - [key: string]: any - }; - stopEarly?: boolean; - "--"?: boolean; - unknown?: IMinimistUnknownFunction; - } - - export interface IOptionInitFunction { - (value: any): any; - } - - export interface ICommandInitFunction { - (name: string, sub: {}[], options: {}[]): void; - } - - export interface IUsageFilterFunction { - (output: any): any; - } - - export interface IConfiguration { - help?: boolean; - name?: string; - version?: boolean; - usageFilter?: IUsageFilterFunction; - value?: string; - mri: IMriArguments; - minimist?: IMinimistArguments; - mainColor: string | string[]; - subColor: string | string[]; - } - - export interface IOption { - name: string; - description: string; - init?: IOptionInitFunction; - defaultValue?: any; - } - - export interface IExample { - usage: string; - description: string; - } - - export interface API { - sub: string[]; - - option(name: string | [string, string], description: string, defaultValue?: any, init?: IOptionInitFunction): API; - options(list: IOption[]): API; - command(name: string, description: string, init?: ICommandInitFunction, aliases?: string[]): API; - example(usage: string, description: string): API; - examples(list: IExample[]): API; - parse(argv: string[], options?: IConfiguration): { [key: string]: any }; - showHelp(): void; - } + function option(name: string | [string, string], description: string, defaultValue?: any, init?: OptionInitFunction): typeof args; + function options(list: Option[]): typeof args; + function command(name: string, description: string, init?: CommandInitFunction, aliases?: string[]): typeof args; + function example(usage: string, description: string): typeof args; + function examples(list: Example[]): typeof args; + function parse(argv: string[], options?: ConfigurationOptions): { [key: string]: any }; + function showHelp(): void; +} + +type MriUnknownFunction = (param: string) => boolean; +type MinimistUnknownFunction = (param: string) => boolean; + +type OptionInitFunction = (value: any) => any; +type CommandInitFunction = (name: string, sub: string[], options: ConfigurationOptions) => void; +type UsageFilterFunction = (output: any) => any; + +interface MriOptions { + args?: string[]; + alias?: { + [key: string]: string | string[] + }; + boolean?: string | string[]; + default?: { + [key: string]: any + }; + string?: string | string[]; + unknown?: MriUnknownFunction; +} + +interface MinimistOptions { + string?: string | string[]; + boolean?: boolean | string | string[]; + alias?: { + [key: string]: string | string[] + }; + default?: { + [key: string]: any + }; + stopEarly?: boolean; + "--"?: boolean; + unknown?: MinimistUnknownFunction; +} + +interface ConfigurationOptions { + help?: boolean; + name?: string; + version?: boolean; + usageFilter?: UsageFilterFunction; + value?: string; + mri: MriOptions; + minimist?: MinimistOptions; + mainColor: string | string[]; + subColor: string | string[]; +} + +interface Option { + name: string; + description: string; + init?: OptionInitFunction; + defaultValue?: any; +} + +interface Example { + usage: string; + description: string; } diff --git a/types/args/tsconfig.json b/types/args/tsconfig.json index ccc4d54297..d287417769 100644 --- a/types/args/tsconfig.json +++ b/types/args/tsconfig.json @@ -7,6 +7,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" @@ -19,4 +20,4 @@ "index.d.ts", "args-tests.ts" ] -} +} \ No newline at end of file From a33c9d1eed8c65f2ad47ec120c4387d04084493d Mon Sep 17 00:00:00 2001 From: Edward Sammut Alessi Date: Fri, 27 Oct 2017 10:09:58 +0200 Subject: [PATCH 3/5] Fix lint warnings --- types/args/index.d.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/types/args/index.d.ts b/types/args/index.d.ts index b6048d2c5f..43e1f86e85 100644 --- a/types/args/index.d.ts +++ b/types/args/index.d.ts @@ -2,20 +2,20 @@ // Project: https://github.com/leo/args#readme // Definitions by: Slessi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.2 -export = args; +declare const c: args; +export = c; -declare namespace args { - let sub: string[]; +interface args { + sub: string[]; - function option(name: string | [string, string], description: string, defaultValue?: any, init?: OptionInitFunction): typeof args; - function options(list: Option[]): typeof args; - function command(name: string, description: string, init?: CommandInitFunction, aliases?: string[]): typeof args; - function example(usage: string, description: string): typeof args; - function examples(list: Example[]): typeof args; - function parse(argv: string[], options?: ConfigurationOptions): { [key: string]: any }; - function showHelp(): void; + option(name: string | [string, string], description: string, defaultValue?: any, init?: OptionInitFunction): args; + options(list: Option[]): args; + command(name: string, description: string, init?: CommandInitFunction, aliases?: string[]): args; + example(usage: string, description: string): args; + examples(list: Example[]): args; + parse(argv: string[], options?: ConfigurationOptions): { [key: string]: any }; + showHelp(): void; } type MriUnknownFunction = (param: string) => boolean; From 7758f8bcb995a8d9f841cde4c02500663804c06f Mon Sep 17 00:00:00 2001 From: Edward Sammut Alessi Date: Fri, 27 Oct 2017 15:07:11 +0200 Subject: [PATCH 4/5] Fix name for option type --- types/args/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/args/index.d.ts b/types/args/index.d.ts index 43e1f86e85..2ef8b1eb88 100644 --- a/types/args/index.d.ts +++ b/types/args/index.d.ts @@ -65,7 +65,7 @@ interface ConfigurationOptions { } interface Option { - name: string; + name: [string, string]; description: string; init?: OptionInitFunction; defaultValue?: any; From 8f9739685b4dc06c2b1c85bd8148d2299fead924 Mon Sep 17 00:00:00 2001 From: Edward Sammut Alessi Date: Sat, 28 Oct 2017 20:35:52 +0200 Subject: [PATCH 5/5] Move types only used once inline --- types/args/index.d.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/types/args/index.d.ts b/types/args/index.d.ts index 2ef8b1eb88..70b784564d 100644 --- a/types/args/index.d.ts +++ b/types/args/index.d.ts @@ -11,19 +11,14 @@ interface args { option(name: string | [string, string], description: string, defaultValue?: any, init?: OptionInitFunction): args; options(list: Option[]): args; - command(name: string, description: string, init?: CommandInitFunction, aliases?: string[]): args; + command(name: string, description: string, init?: (name: string, sub: string[], options: ConfigurationOptions) => void, aliases?: string[]): args; example(usage: string, description: string): args; examples(list: Example[]): args; parse(argv: string[], options?: ConfigurationOptions): { [key: string]: any }; showHelp(): void; } -type MriUnknownFunction = (param: string) => boolean; -type MinimistUnknownFunction = (param: string) => boolean; - type OptionInitFunction = (value: any) => any; -type CommandInitFunction = (name: string, sub: string[], options: ConfigurationOptions) => void; -type UsageFilterFunction = (output: any) => any; interface MriOptions { args?: string[]; @@ -35,7 +30,7 @@ interface MriOptions { [key: string]: any }; string?: string | string[]; - unknown?: MriUnknownFunction; + unknown?: (param: string) => boolean; } interface MinimistOptions { @@ -49,14 +44,14 @@ interface MinimistOptions { }; stopEarly?: boolean; "--"?: boolean; - unknown?: MinimistUnknownFunction; + unknown?: (param: string) => boolean; } interface ConfigurationOptions { help?: boolean; name?: string; version?: boolean; - usageFilter?: UsageFilterFunction; + usageFilter?: (output: any) => any; value?: string; mri: MriOptions; minimist?: MinimistOptions; @@ -65,7 +60,7 @@ interface ConfigurationOptions { } interface Option { - name: [string, string]; + name: string | [string, string]; description: string; init?: OptionInitFunction; defaultValue?: any;