Comparing version 2.1.0 to 2.1.1
@@ -104,3 +104,3 @@ "use strict"; | ||
this.registerDefinition(prototype, command => { | ||
command.addPositional({ required: descriptor.required }); | ||
command.addPositional({ name: propertyName, required: descriptor.required }); | ||
}); | ||
@@ -140,3 +140,3 @@ this.registerTransformer(prototype, (state, command) => { | ||
this.registerDefinition(prototype, command => { | ||
command.addRest({ required }); | ||
command.addRest({ name: propertyName, required }); | ||
}); | ||
@@ -143,0 +143,0 @@ this.registerTransformer(prototype, (state, command) => { |
@@ -280,6 +280,7 @@ export declare const NODE_INITIAL = 0; | ||
}; | ||
export declare const NoLimits: unique symbol; | ||
export declare type ArityDefinition = { | ||
leading: number; | ||
extra: number; | ||
trailing: number; | ||
leading: string[]; | ||
extra: string[] | typeof NoLimits; | ||
trailing: string[]; | ||
proxy: boolean; | ||
@@ -303,6 +304,8 @@ }; | ||
setArity({ leading, trailing, extra, proxy }: Partial<ArityDefinition>): void; | ||
addPositional({ required }?: { | ||
addPositional({ name, required }?: { | ||
name?: string; | ||
required?: boolean; | ||
}): void; | ||
addRest({ required }?: { | ||
addRest({ name, required }?: { | ||
name?: string; | ||
required?: number; | ||
@@ -309,0 +312,0 @@ }): void; |
@@ -376,6 +376,8 @@ "use strict"; | ||
}; | ||
// ------------------------------------------------------------------------ | ||
exports.NoLimits = Symbol(); | ||
class CommandBuilder { | ||
constructor(cliIndex, cliOpts) { | ||
this.allOptionNames = []; | ||
this.arity = { leading: 0, trailing: 0, extra: 0, proxy: false }; | ||
this.arity = { leading: [], trailing: [], extra: [], proxy: false }; | ||
this.options = []; | ||
@@ -392,25 +394,25 @@ this.paths = []; | ||
} | ||
addPositional({ required = true } = {}) { | ||
if (!required && this.arity.extra === Infinity) | ||
addPositional({ name = 'arg', required = true } = {}) { | ||
if (!required && this.arity.extra === exports.NoLimits) | ||
throw new Error(`Optional parameters cannot be declared when using .rest() or .proxy()`); | ||
if (!required && this.arity.trailing > 0) | ||
if (!required && this.arity.trailing.length > 0) | ||
throw new Error(`Optional parameters cannot be declared after the required trailing positional arguments`); | ||
if (!required) { | ||
this.arity.extra += 1; | ||
if (!required && this.arity.extra !== exports.NoLimits) { | ||
this.arity.extra.push(name); | ||
} | ||
else if (this.arity.extra === 0) { | ||
this.arity.leading += 1; | ||
else if (this.arity.extra !== exports.NoLimits && this.arity.extra.length === 0) { | ||
this.arity.leading.push(name); | ||
} | ||
else { | ||
this.arity.trailing += 1; | ||
this.arity.trailing.push(name); | ||
} | ||
} | ||
addRest({ required = 0 } = {}) { | ||
if (this.arity.extra === Infinity) | ||
addRest({ name = 'arg', required = 0 } = {}) { | ||
if (this.arity.extra === exports.NoLimits) | ||
throw new Error(`Infinite lists cannot be declared multiple times in the same command`); | ||
if (this.arity.trailing > 0) | ||
if (this.arity.trailing.length > 0) | ||
throw new Error(`Infinite lists cannot be declared after the required trailing positional arguments`); | ||
for (let t = 0; t < required; ++t) | ||
this.addPositional(); | ||
this.arity.extra = Infinity; | ||
this.addPositional({ name }); | ||
this.arity.extra = exports.NoLimits; | ||
} | ||
@@ -441,12 +443,8 @@ addProxy() { | ||
} | ||
for (let t = 0; t < this.arity.leading; ++t) | ||
segments.push(`<arg>`); | ||
if (this.arity.extra === Infinity) | ||
segments.push(...this.arity.leading.map(name => `<${name}>`)); | ||
if (this.arity.extra === exports.NoLimits) | ||
segments.push(`...`); | ||
else | ||
for (let t = 0; t < this.arity.extra; ++t) | ||
segments.push(`[arg]`); | ||
for (let t = 0; t < this.arity.trailing; ++t) { | ||
segments.push(`<arg>`); | ||
} | ||
segments.push(...this.arity.extra.map(name => `[${name}]`)); | ||
segments.push(...this.arity.trailing.map(name => `<${name}>`)); | ||
} | ||
@@ -475,3 +473,3 @@ return segments.join(` `); | ||
} | ||
if (this.arity.leading > 0 || !this.arity.proxy) { | ||
if (this.arity.leading.length > 0 || !this.arity.proxy) { | ||
const helpNode = injectNode(machine, makeNode()); | ||
@@ -482,9 +480,9 @@ registerDynamic(machine, lastPathNode, `isHelp`, helpNode, [`useHelp`, this.cliIndex]); | ||
this.registerOptions(machine, lastPathNode); | ||
if (this.arity.leading > 0) | ||
if (this.arity.leading.length > 0) | ||
registerStatic(machine, lastPathNode, exports.END_OF_INPUT, exports.NODE_ERRORED, [`setError`, `Not enough positional arguments`]); | ||
let lastLeadingNode = lastPathNode; | ||
for (let t = 0; t < this.arity.leading; ++t) { | ||
for (let t = 0; t < this.arity.leading.length; ++t) { | ||
const nextLeadingNode = injectNode(machine, makeNode()); | ||
this.registerOptions(machine, nextLeadingNode); | ||
if (this.arity.trailing > 0 || t + 1 !== this.arity.leading) | ||
if (this.arity.trailing.length > 0 || t + 1 !== this.arity.leading.length) | ||
registerStatic(machine, nextLeadingNode, exports.END_OF_INPUT, exports.NODE_ERRORED, [`setError`, `Not enough positional arguments`]); | ||
@@ -495,6 +493,6 @@ registerDynamic(machine, lastLeadingNode, `isNotOptionLike`, nextLeadingNode, `pushPositional`); | ||
let lastExtraNode = lastLeadingNode; | ||
if (this.arity.extra > 0) { | ||
if (this.arity.extra === exports.NoLimits || this.arity.extra.length > 0) { | ||
const extraShortcutNode = injectNode(machine, makeNode()); | ||
registerShortcut(machine, lastLeadingNode, extraShortcutNode); | ||
if (this.arity.extra === Infinity) { | ||
if (this.arity.extra === exports.NoLimits) { | ||
const extraNode = injectNode(machine, makeNode()); | ||
@@ -507,3 +505,3 @@ this.registerOptions(machine, extraNode); | ||
else { | ||
for (let t = 0; t < this.arity.extra; ++t) { | ||
for (let t = 0; t < this.arity.extra.length; ++t) { | ||
const nextExtraNode = injectNode(machine, makeNode()); | ||
@@ -518,9 +516,9 @@ this.registerOptions(machine, nextExtraNode); | ||
} | ||
if (this.arity.trailing > 0) | ||
if (this.arity.trailing.length > 0) | ||
registerStatic(machine, lastExtraNode, exports.END_OF_INPUT, exports.NODE_ERRORED, [`setError`, `Not enough positional arguments`]); | ||
let lastTrailingNode = lastExtraNode; | ||
for (let t = 0; t < this.arity.trailing; ++t) { | ||
for (let t = 0; t < this.arity.trailing.length; ++t) { | ||
const nextTrailingNode = injectNode(machine, makeNode()); | ||
this.registerOptions(machine, nextTrailingNode); | ||
if (t + 1 < this.arity.trailing) | ||
if (t + 1 < this.arity.trailing.length) | ||
registerStatic(machine, nextTrailingNode, exports.END_OF_INPUT, exports.NODE_ERRORED, [`setError`, `Not enough positional arguments`]); | ||
@@ -527,0 +525,0 @@ registerDynamic(machine, lastTrailingNode, `isNotOptionLike`, nextTrailingNode, `pushPositional`); |
{ | ||
"name": "clipanion", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"main": "lib/advanced", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
72619
1726