|
|
@ -14,8 +14,6 @@ export type Arg<T = any> = {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class ArgParser {
|
|
|
|
export class ArgParser {
|
|
|
|
static readonly helpArg: Arg = {name: 'help', desc: 'Display command\'s help message', flags: ['-h', '--help'], default: false};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
commands: ArgParser[] = [];
|
|
|
|
commands: ArgParser[] = [];
|
|
|
|
args: Arg[] = [];
|
|
|
|
args: Arg[] = [];
|
|
|
|
flags: Arg[] = [];
|
|
|
|
flags: Arg[] = [];
|
|
|
@ -37,7 +35,8 @@ export class ArgParser {
|
|
|
|
// Arguments
|
|
|
|
// Arguments
|
|
|
|
this.commands = argList.filter(arg => arg instanceof ArgParser);
|
|
|
|
this.commands = argList.filter(arg => arg instanceof ArgParser);
|
|
|
|
this.args = <Arg[]>argList.filter(arg => !(arg instanceof ArgParser) && !arg.flags?.length);
|
|
|
|
this.args = <Arg[]>argList.filter(arg => !(arg instanceof ArgParser) && !arg.flags?.length);
|
|
|
|
this.flags = <Arg[]>[...argList.filter(arg => !(arg instanceof ArgParser) && arg.flags && arg.flags.length), ...this.flags];
|
|
|
|
this.flags = <Arg[]>[...argList.filter(arg => !(arg instanceof ArgParser) && arg.flags && arg.flags.length),
|
|
|
|
|
|
|
|
{name: 'help', desc: 'Display command\'s help message', flags: ['-h', '--help'], default: false}];
|
|
|
|
this.defaults = argList.reduce((acc, arg: any) => ({...acc, [arg.name]: arg['extras'] ? [] : (arg.default ?? null)}), {});
|
|
|
|
this.defaults = argList.reduce((acc, arg: any) => ({...acc, [arg.name]: arg['extras'] ? [] : (arg.default ?? null)}), {});
|
|
|
|
|
|
|
|
|
|
|
|
// Examples
|
|
|
|
// Examples
|
|
|
@ -125,18 +124,18 @@ export class ArgParser {
|
|
|
|
// Description
|
|
|
|
// Description
|
|
|
|
let msg = `\n\n${opts.message || this.desc}`;
|
|
|
|
let msg = `\n\n${opts.message || this.desc}`;
|
|
|
|
// Examples
|
|
|
|
// Examples
|
|
|
|
msg += '\n\nUsage:\t' + this.examples.map(ex => `run ${this.name} ${ex}`).join('\n\t\t');
|
|
|
|
msg += '\n\nUsage:\t' + this.examples.map(ex => `${this.name} ${ex}`).join('\n\t');
|
|
|
|
// Arguments
|
|
|
|
// Arguments
|
|
|
|
if(this.args.length) msg += '\n\n\t\t' + this.args.map(arg =>
|
|
|
|
if(this.args.length) msg += '\n\n\t' + this.args.map(arg =>
|
|
|
|
`${arg.name.toUpperCase()}${spacer(arg.name)}${arg.desc}`).join('\n\t\t');
|
|
|
|
`${arg.name.toUpperCase()}${spacer(arg.name)}${arg.desc}`).join('\n\t');
|
|
|
|
// Flags
|
|
|
|
// Flags
|
|
|
|
msg += '\n\nOptions:\n\t\t' + this.flags.map(flag => {
|
|
|
|
msg += '\n\nOptions:\n\t' + this.flags.map(flag => {
|
|
|
|
const flags = flag.flags?.join(', ') || '';
|
|
|
|
const flags = flag.flags?.join(', ') || '';
|
|
|
|
return `${flags}${spacer(flags)}${flag.desc}`;
|
|
|
|
return `${flags}${spacer(flags)}${flag.desc}`;
|
|
|
|
}).join('\n\t\t');
|
|
|
|
}).join('\n\t');
|
|
|
|
// Commands
|
|
|
|
// Commands
|
|
|
|
if(this.commands.length) msg += '\n\nCommands:\n\t\t' + this.commands.map(command =>
|
|
|
|
if(this.commands.length) msg += '\n\nCommands:\n\t' + this.commands.map(command =>
|
|
|
|
`${command.name}${spacer(command.name)}${command.desc}`).join('\n\t\t');
|
|
|
|
`${command.name}${spacer(command.name)}${command.desc}`).join('\n\t');
|
|
|
|
return `${msg}\n\n`;
|
|
|
|
return `${msg}\n\n`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|