2
>
import {parseCommand, ParsedCommand} from './parse-command.js';
3
>
4
>
const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'];
5
>
export function argsert(callerArguments: any[], length?: number): void;
6
>
export function argsert(
7
>
expected: string,
8
>
callerArguments: any[],
9
>
length?: number
10
>
): void;
11
>
export function argsert(
13
>
arg2?: any[] | number,
14
>
arg3?: number
15
>
): void {
16
>
function parseArgs(): [
17
>
Pick<ParsedCommand, 'demanded' | 'optional'>,
18
>
any[],
19
>
number?,
20
>
] {
21
>
return typeof arg1 === 'object'
22
>
? [{demanded: [], optional: []}, arg1, arg2 as number | undefined]
23
>
: [
25
>
arg2 as any[],
26
>
arg3 as number | undefined,
27
>
];
29
>
// TODO: should this eventually raise an exception.
30
>
try {
31
>
// preface the argument description with "cmd", so
32
>
// that we can run it through yargs' command parser.
33
>
let position = 0;
34
>
const [parsed, callerArguments, _length] = parseArgs();
35
>
const args = [].slice.call(callerArguments);
36
>
37
>
while (args.length && args[args.length - 1] === undefined) args.pop();
38
>
const length = _length || args.length;
39
>
40
>
if (length < parsed.demanded.length) {
42
>
`Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`
43
>
);
44
>
}
45
46
const totalCommands = parsed.demanded.length + parsed.optional.length;