@kearisp/cli
Advanced tools
Comparing version 2.0.3 to 2.0.4
import { Command } from "./Command"; | ||
declare class Cli { | ||
export declare class Cli { | ||
protected name: string; | ||
@@ -12,2 +12,1 @@ protected commands: Command[]; | ||
} | ||
export { Cli }; |
@@ -35,3 +35,3 @@ "use strict"; | ||
} | ||
else if (char === " " && !escape && !quotes) { | ||
else if ((char === " ") && !escape && !quotes) { | ||
if (current) { | ||
@@ -43,2 +43,5 @@ parts.push(current); | ||
else { | ||
if (char === "=") { | ||
index--; | ||
} | ||
current += (escape ? "\\".repeat(escape - 1) : "") + char; | ||
@@ -96,3 +99,3 @@ escape = 0; | ||
this.name = Path.basename(scriptPath); | ||
this.command(`complete <index> <prev> <command>`) | ||
this.command(`complete [...args]`) | ||
.help({ | ||
@@ -112,3 +115,4 @@ description: "Generate completion script", | ||
.action(async (input) => { | ||
const parts = this.parseCommand(input.argument("command"), parseInt(input.argument("index"))); | ||
const [index, prev, command] = input.argument("args") || []; | ||
const parts = this.parseCommand(command, parseInt(index)); | ||
const res = await this.complete(parts); | ||
@@ -115,0 +119,0 @@ return res |
@@ -34,5 +34,5 @@ import { Option } from "../types"; | ||
protected predictCommand(command: string, part: string, input: CommandInput): Promise<string[]>; | ||
protected predictOption(part: string): Promise<string[]>; | ||
protected predictOption(part: string, input: CommandInput): Promise<string[]>; | ||
complete(parts: string[]): Promise<string[]>; | ||
} | ||
export {}; |
@@ -8,3 +8,2 @@ "use strict"; | ||
const Parser_1 = require("./Parser"); | ||
const Logger_1 = require("./Logger"); | ||
const CommandInput_1 = require("./CommandInput"); | ||
@@ -280,13 +279,31 @@ class Command { | ||
} | ||
async predictOption(part) { | ||
const [, dash, name] = /^(--?)(\w+)?/.exec(part) || []; | ||
return this._options.reduce((res, option) => { | ||
if (dash === "-") { | ||
res.push(`-${option.alias}`, `--${option.name}`); | ||
async predictOption(part, input) { | ||
const [, dash, name, sign, value] = /^(--?)(\w+)?(=)?(.+)?/.exec(part) || []; | ||
const option = this._options.find((option) => { | ||
if (!name) { | ||
return false; | ||
} | ||
else if (dash === "--") { | ||
res.push(`--${option.name}`); | ||
} | ||
return res; | ||
}, []); | ||
return option.name === name || option.alias === name; | ||
}); | ||
if (!option) { | ||
return this._options.reduce((res, option) => { | ||
if (dash === "-") { | ||
res.push(`-${option.alias}`, `--${option.name}`); | ||
} | ||
else if (dash === "--") { | ||
res.push(`--${option.name}`); | ||
} | ||
return res; | ||
}, []); | ||
} | ||
const completion = this._completions.find((completion) => { | ||
return completion.name === option.name; | ||
}); | ||
if (!completion) { | ||
return []; | ||
} | ||
const predicts = await completion.action(input); | ||
return predicts.map((predict) => { | ||
return ``; | ||
}); | ||
} | ||
@@ -314,3 +331,2 @@ async complete(parts) { | ||
args[name] = value; | ||
Logger_1.Logger.info(args); | ||
return this.predictCommand(command, parser.part, this.getCommandInput(args, options)); | ||
@@ -332,5 +348,53 @@ } | ||
while (parser.isOption(true)) { | ||
if (parser.isLast) { | ||
return this.predictOption(parser.part); | ||
const { dash, name, sign, value } = parser.parseOptionV2(); | ||
const option = name ? this._options.find((option) => { | ||
if (dash === "-") { | ||
return option.alias === name; | ||
} | ||
return option.name === name; | ||
}) : undefined; | ||
if (!option && !sign && parser.isLast) { | ||
return this._options.reduce((res, option) => { | ||
if (dash === "-" && option.alias) { | ||
res.push(`-${option.alias}`); | ||
} | ||
res.push(`--${option.name}`); | ||
return res; | ||
}, []); | ||
} | ||
if (option) { | ||
switch (option.type) { | ||
case "boolean": | ||
options[option.name] = true; | ||
break; | ||
case "string": | ||
case "number": | ||
let v = value; | ||
if (!parser.isLast && sign !== "=") { | ||
parser.next(); | ||
v = parser.part; | ||
} | ||
if (option.type === "number") { | ||
v = parseFloat(v); | ||
} | ||
if (parser.isLast) { | ||
const completion = this._completions.find((completion) => { | ||
return completion.name === option.name; | ||
}); | ||
if (!completion) { | ||
return []; | ||
} | ||
const predicts = await completion.action(this.getCommandInput(args, options)); | ||
return predicts.map((predict) => { | ||
if (sign === "=") { | ||
return `${dash}${name}${sign}${predict}`; | ||
} | ||
return predict; | ||
}); | ||
; | ||
} | ||
options[option.name] = v; | ||
break; | ||
} | ||
} | ||
parser.next(); | ||
@@ -337,0 +401,0 @@ } |
@@ -21,2 +21,8 @@ export declare class Parser { | ||
isMultipleOptions(): boolean; | ||
parseOptionV2(): { | ||
dash: string; | ||
name: string; | ||
sign: string; | ||
value: string; | ||
}; | ||
parseOption(): { | ||
@@ -23,0 +29,0 @@ name: string; |
@@ -29,5 +29,2 @@ "use strict"; | ||
isSpread(command) { | ||
if (this.eol) { | ||
return false; | ||
} | ||
return Parser.spreadOptionalRegexp.test(command) || | ||
@@ -66,2 +63,11 @@ Parser.spreadRequiredRegexp.test(command); | ||
} | ||
parseOptionV2() { | ||
const [, dash, name, sign, value] = /^(--?)([\w-_]+)?(?:(=)(.+)?)?/.exec(this.part || "") || []; | ||
return { | ||
dash, | ||
name, | ||
sign, | ||
value | ||
}; | ||
} | ||
parseOption() { | ||
@@ -121,3 +127,3 @@ const [, name, alias] = regOption.exec(this.part) || []; | ||
if (exitCount++ > 100) { | ||
throw new Error("Emergency exit"); | ||
throw new Error(`Emergency exit. "${part}" "${restCommand}"`); | ||
} | ||
@@ -124,0 +130,0 @@ } |
{ | ||
"name": "@kearisp/cli", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Kris Papercut <krispcut@gmail.com>", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44960
1117