@kearisp/cli
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -8,6 +8,6 @@ import { Command } from "./Command"; | ||
command(name: string): Command; | ||
protected process(parts: string[]): Promise<string | void>; | ||
protected process(parts: string[]): Promise<string>; | ||
protected complete(parts: string[]): Promise<string[]>; | ||
run(argv: string[]): Promise<string | void>; | ||
run(argv: string[]): Promise<string>; | ||
} | ||
export { Cli }; |
@@ -31,3 +31,3 @@ import { Option } from "../types"; | ||
parse(parts: string[]): CommandInput; | ||
emit(name: string, input: CommandInput): Promise<string | void>; | ||
emit(name: string, input: CommandInput): Promise<string>; | ||
protected predictCommand(command: string, part: string, input: CommandInput): Promise<string[]>; | ||
@@ -34,0 +34,0 @@ protected predictOption(part: string): Promise<string[]>; |
@@ -134,2 +134,10 @@ "use strict"; | ||
} | ||
else if (parser.isMultipleOptions()) { | ||
parser.parseOptionMultiple().forEach((alias) => { | ||
const option = this.getOptionSettings(undefined, alias); | ||
if (option && option.type === "boolean") { | ||
options[option.name] = true; | ||
} | ||
}); | ||
} | ||
parser.next(); | ||
@@ -169,3 +177,7 @@ } | ||
} | ||
return this._action(input); | ||
const res = await this._action(input); | ||
if (typeof res === "undefined") { | ||
return ""; | ||
} | ||
return res; | ||
} | ||
@@ -172,0 +184,0 @@ async predictCommand(command, part, input) { |
export declare class CommandInput { | ||
protected readonly args: any; | ||
protected readonly options: any; | ||
constructor(args: any, options: any); | ||
protected readonly _arguments: any; | ||
protected readonly _options: any; | ||
constructor(_arguments: any, _options: any); | ||
argument(key: string): null | string; | ||
arguments(): any; | ||
option(key: string): null | string; | ||
option<T extends null | string | boolean>(key: string, defaultValue?: T): T; | ||
options(): any; | ||
} |
@@ -5,9 +5,9 @@ "use strict"; | ||
class CommandInput { | ||
constructor(args, options) { | ||
this.args = args; | ||
this.options = options; | ||
constructor(_arguments, _options) { | ||
this._arguments = _arguments; | ||
this._options = _options; | ||
} | ||
argument(key) { | ||
if (key in this.args) { | ||
return this.args[key]; | ||
if (key in this._arguments) { | ||
return this._arguments[key]; | ||
} | ||
@@ -17,11 +17,14 @@ return null; | ||
arguments() { | ||
return this.args; | ||
return this._arguments; | ||
} | ||
option(key) { | ||
if (key in this.options) { | ||
return this.options[key]; | ||
option(key, defaultValue = null) { | ||
if (key in this._options) { | ||
return this._options[key]; | ||
} | ||
return null; | ||
return defaultValue; | ||
} | ||
options() { | ||
return this._options; | ||
} | ||
} | ||
exports.CommandInput = CommandInput; |
@@ -6,2 +6,3 @@ export declare class Parser { | ||
static readonly optionRegexp: RegExp; | ||
static readonly optionMultipleRegexp: RegExp; | ||
static readonly spreadRequiredRegexp: RegExp; | ||
@@ -20,2 +21,3 @@ static readonly spreadOptionalRegexp: RegExp; | ||
isOptionWithValue(): boolean; | ||
isMultipleOptions(): boolean; | ||
parseOption(): { | ||
@@ -30,2 +32,3 @@ name: string; | ||
}; | ||
parseOptionMultiple(): string[]; | ||
getArguments(command: string): any; | ||
@@ -32,0 +35,0 @@ parseSpreadCommand(command: string): string; |
@@ -65,2 +65,5 @@ "use strict"; | ||
} | ||
isMultipleOptions() { | ||
return Parser.optionMultipleRegexp.test(this.part); | ||
} | ||
parseOption() { | ||
@@ -74,2 +77,6 @@ const [, name, alias] = regOption.exec(this.part) || []; | ||
} | ||
parseOptionMultiple() { | ||
const [, options = ""] = Parser.optionMultipleRegexp.exec(this.part) || []; | ||
return options.split(""); | ||
} | ||
getArguments(command) { | ||
@@ -131,3 +138,4 @@ const { names, regex } = this.parse(command); | ||
Parser.optionRegexp = /^-(?:-(\w[\w\d_-]*)|(\w))$/; | ||
Parser.optionMultipleRegexp = /^-(\w+)$/; | ||
Parser.spreadRequiredRegexp = /^<\.\.\.([0-9\w_-]+)>(.*)?$/; | ||
Parser.spreadOptionalRegexp = /^\[\.\.\.([0-9\w_-]+)](.*)$/; |
{ | ||
"name": "@kearisp/cli", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"license": "MIT", | ||
@@ -23,3 +23,3 @@ "author": "Kris Papercut <krispcut@gmail.com>", | ||
"prepare": "npm run build", | ||
"watch": "KP_LOG=disable tsc --watch", | ||
"watch": "tsc --watch", | ||
"watch:test": "KP_LOG=log jest --colors --watchAll", | ||
@@ -26,0 +26,0 @@ "watch:test-cli": "KP_LOG=log jest --colors --watchAll --runTestsByPath ./src/makes/Cli.spec.ts", |
@@ -50,4 +50,4 @@ # @kearisp/cli | ||
cli.command("foo <foo1> [foo2]") | ||
.action((options, foo1: string, foo2?: string) => { | ||
return `Foo result, with arguments foo1=${foo1} foo2=${foo2}`; | ||
.action((input: CommandInput) => { | ||
return `Foo result, with arguments foo1=${input.argument("foo1")} foo2=${input.argument("foo2")}`; | ||
}); | ||
@@ -58,4 +58,4 @@ ``` | ||
cli.command("bar [...bars]") | ||
.action((options, bars: string[]) => { | ||
return "Bar result, Bars: " + bars.join(", "); | ||
.action((input: CommandInput) => { | ||
return "Bar result, Bars: " + input.argument("bars").join(", "); | ||
}); | ||
@@ -82,7 +82,7 @@ ``` | ||
}) | ||
.action((options) => { | ||
.action((input: CommandInput) => { | ||
const { | ||
bar = "", | ||
init = false | ||
} = options; | ||
} = input.options(); | ||
@@ -104,6 +104,6 @@ return `Foo result, with options bar=${bar} init=${init.toString()}`; | ||
}) | ||
.action((options) => { | ||
.action((input) => { | ||
const { | ||
option = "" | ||
} = options; | ||
} = input.options(); | ||
@@ -132,3 +132,7 @@ return `option=${option}`; | ||
.completion("bar", () => ["value1", "value2", "value3"]) | ||
.action((options, bar: string) => { | ||
.action((input) => { | ||
const { | ||
bar = "" | ||
} = input.arguments(); | ||
return `Foo result, with argument bar=${bar}`; | ||
@@ -135,0 +139,0 @@ }); |
40174
1011
145