All files args.ts

81.98% Statements 91/111
96.77% Branches 30/31
80% Functions 4/5
81.98% Lines 91/111

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 1121x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                         1x 4x 4x 4x 4x 4x 23x 23x 6x 6x 17x 23x 14x 14x 14x 11x 11x 5x 5x 6x 11x 5x 11x 1x 1x 14x 14x 14x 11x 7x 11x 4x 2x 2x 4x 4x 4x 14x 14x 14x 5x 5x 5x 5x 14x 9x 9x 2x 2x 9x 7x 1x 7x 6x 6x 6x 6x 7x 9x 14x 23x 22x 22x 4x 21x 21x 3x 3x 21x 3x 3x 3x  
import * as _ from 'lodash';
 
export interface ParsedArgs {
    named: { [index: string]: string|number|boolean|undefined|string[]|number[] };
    positional: string[];
}
 
export interface ArgSchema {
    aliases: { [index: string]: string },
    named: {
        [index: string]: {
            isFlag?: boolean;
            parseAs?: 'number' | 'boolean' | 'string[]' | 'number[]';
            description?: string;
        },
    },
    positional: { name: string; }[],
}
 
export function describe(schema: ArgSchema) {
    console.log(`usage: lait <options> ${schema.positional.map(x => `[${x.name}]`).join(' ')}`);
    console.log('options:');

    const pairs = Object.keys(schema.named).map(name => {
        const options = schema.named[name];
        const alias = Object.keys(schema.aliases).find(x => schema.aliases[x] === name);
        return {
            left: `--${name}${alias ? ` (-${alias})` : ''}`,
            right: options.description,
        };
    });

    const targetLength = _.max(pairs.map(x => x.left.length))!;

    for (const option of pairs) {
        const filler = new Array(targetLength - option.left.length).fill(' ').join('');
        console.log(`\t${option.left}${filler}\t${option.right}`);
    }
}
 
export function getArgs(processArgs: string[], schema: ArgSchema) {
    const args: ParsedArgs = { named: {}, positional: [] };
    // First pull out non-positional args
    const rawArgs: (string|undefined)[] = processArgs.slice(2);
    for (let i = 0; i < rawArgs.length; i++) {
        const arg = rawArgs[i];
        if (!arg) {
            continue;
        }
 
        if (arg.startsWith('-')) {
            const strippedDash = arg.replace(/^-{1,2}/, '');
            const getFullName = (name: string) => name in schema.aliases ? schema.aliases[name] : name;
            const getValue = (name: string, value: string) => {
                const parseAs = schema.named[name].parseAs;
                if (!parseAs || parseAs === 'string[]') {
                    return value;
                }
 
                if (parseAs === 'number' || parseAs === 'number[]') {
                    return parseFloat(value);
                } else {
                    return value.toLowerCase() === 'true';
                }
            };
 
            const applyValue = (argName: string, value: string | number | boolean) => {
                if (!(schema.named[argName].parseAs || '').endsWith('[]')) {
                    args.named[argName] = value;
                } else {
                    if (!args.named[argName]) {
                        args.named[argName] = [];
                    }
 
                    (args.named[argName] as any[]).push(value);
                }
            };
 
            if (strippedDash.includes('=')) {
                const parts = strippedDash.split('=');
                const argName = getFullName(parts[0]);
                applyValue(argName, getValue(argName, parts.slice(1).join('=')));
                rawArgs[i] = undefined;
            } else {
                const argName = getFullName(strippedDash);
                if (schema.named[argName]?.isFlag) {
                    args.named[argName] = true;
                    rawArgs[i] = undefined;
                } else {
                    if (i === rawArgs.length - 1 ) {
                        throw new Error(`Missing value for flag ${argName}`);
                    } else {
                        applyValue(argName, getValue(argName, rawArgs[i + 1]!));
                        rawArgs[i] = undefined;
                        rawArgs[i + 1] = undefined;
                    }
                }
            }
        }
    }
 
    // now do positional
    for (let i = 0; i < rawArgs.length; i++) {
        const arg = rawArgs[i];
        if (arg) {
            args.positional.push(arg);
        }
    }
 
    return args;
}