sinotron
Version:
Simple framework for Typescript Electron projects
44 lines (43 loc) • 1.15 kB
JavaScript
import { Command } from 'commander';
export const CliHelper = {
/**
* Creates a primary cli program which can have sub-commands under it.
* @param name
* @param description
* @param version
*/
program: (name, description, version = '0.0.0') => {
const program = new Command();
program.name(name).description(description);
if (version) {
program.version(version);
}
return program;
},
getContext(fnArgs) {
const ctx = {
args: [],
options: {}
};
for (let arg of fnArgs) {
if (!arg)
continue;
if (arg.constructor === Command) {
ctx.command = arg;
}
else if (Array.isArray(arg)) {
ctx.args = Array.from(arg);
}
else if (typeof arg === 'object') {
ctx.options = arg;
}
else {
ctx.args.push(arg);
}
}
if (Array.isArray(ctx.args)) {
ctx.arg0 = ctx.args[0]?.trim();
}
return ctx;
}
};