Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const parse = async (
argv: string[],
opts?: Options
): Promise => {
opts = opts || {}
// cli brand name
const brand =
typeof pkg.bin === 'string'
? basename(pkg.bin, '.js')
: Object.keys(pkg.bin)[0]
// parse argv by minimist
const options = minimist(argv, buildOptions(opts))
// row input args
const input = options._
// extract arguments
const [primary, secondary, thirdly, fourthly, ...extras] = input
// excluding aliases
Object.values(opts).forEach(item => {
if (typeof item !== 'string' && item.alias) {
if (typeof item.alias === 'string') {
delete options[item.alias]
} else {
item.alias.forEach(a => delete options[a])
}
}
public get args(): ParsedArgs {
if (this.argCache) {
logger.verbose('Returning cli args from argCache');
return this.argCache.args;
}
const cliArgs = process.argv.slice(2);
const options = buildOptions(this.options);
const args = minimist(cliArgs, options);
const parsedArgs = this.parseArgs(args);
logger.verbose('Building new cli args...');
logger.verbose(` Options: ${JSON.stringify(this.options)}`);
logger.verbose(` process.argv: ${JSON.stringify(cliArgs)}`);
logger.verbose(` args: ${JSON.stringify(args)}`);
logger.verbose(` parsed args: ${JSON.stringify(parsedArgs)}`);
logger.verbose('Saving cli args to cache');
this.argCache = {
args,
options: this.options,
};