snips-sam
Version:
The Snips Assistant Manager
35 lines (28 loc) • 900 B
text/typescript
const omit = require('lodash.omit');
import * as yargs from 'yargs';
export class ArgParser {
args: string[];
argd: {[key: string]: string};
constructor(argv: yargs.Argv) {
// Hack as argv.argv._ does not work in TypeScript
this.args = JSON.parse(JSON.stringify(argv))['_'];
this.argd = ArgParser.getKeyValues(this.args);
}
static getKeyValues(args: string[]): {[key: string]: string} {
if (!args) return {};
const argd: {[key: string]: string} = {};
for (const arg of args) {
const s = arg.split('=');
if (s.length >= 2) {
argd[s[0]] = s[1];
}
}
return argd;
}
getValue(key: string): string|undefined {
return this.argd[key];
}
getArgd(omitKeys: string[]): {[key: string]: string} {
return omit(this.argd, omitKeys);
}
}