# All files / src/helpers options.helper.ts

97.65% Statements 125/128
83.72% Branches 36/43
100% Functions 9/9
97.65% Lines 125/128

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
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    1291x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    9x
    9x
    9x
    8x
    8x
    9x
    9x
    1x
    2x
    2x
    2x
    2x

    2x
    2x
    2x
    1x
    10x
    10x
    10x
    10x
    10x
    9x
    9x
    1x
    1x
    1x
    1x
    10x
    1x
    1x
    1x
    1x
    1x
    1x
    8x
    8x
    8x
    1x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    1x
    58x
    58x
    55x
    55x
    58x
    58x
    58x
    58x
    1x
    1x
    48x
    48x
    1x
    1x
    48x
    48x
    1x
    1x
    48x
    48x
    48x
    1x
    58x
    58x
    50x
    50x
    58x
    58x
    58x
    58x
    2x
    2x
    48x
    48x
    4x
    4x
    48x
    48x
    48x
    1x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    1x
    196x
    196x
    196x
     import {
        ParseOptions,
        OptionContent,
        CommandLineOption,
        OptionalProperty,
        PropertyOptions,
        Content,
        OptionList,
    } from '../contracts';

    export function getOptionSections(options: ParseOptions<any>): OptionContent[] {
        return (
            options.optionSections || [
                { header: options.optionsHeaderText || 'Options', headerLevel: options.optionsHeaderLevel || 2 },
            ]
        );
    }

    export function getOptionFooterSection<T>(optionList: CommandLineOption<T>[], options: ParseOptions<any>): Content[] {
        const optionsFooter = generateTableFooter(optionList, options);

        if (optionsFooter != null) {
            console.log(`Adding footer: ${optionsFooter}`);
            return [{ content: optionsFooter }];
        }

        return [];
    }

    export function generateTableFooter<T>(
        optionList: CommandLineOption<T>[],
        options: ParseOptions<any>,
    ): string | undefined {
        if (options.addOptionalDefaultExplanatoryFooter != true || options.displayOptionalAndDefault != true) {
            return undefined;
        }

        const optionalProps = optionList.some((option) => (option as unknown as OptionalProperty).optional === true);
        const defaultProps = optionList.some((option) => option.defaultOption === true);

        if (optionalProps || defaultProps) {
            const footerValues = [
                optionalProps != null ? '(O) = optional' : undefined,
                defaultProps != null ? '(D) = default option' : null,
            ];
            return footerValues.filter((v) => v != null).join(', ');
        }

        return undefined;
    }

    export function addOptions<T>(
        content: OptionContent,
        optionList: CommandLineOption<T>[],
        options: ParseOptions<T>,
    ): OptionList {
        optionList = optionList.map((option) => mapDefinitionDetails(option, options));

        return { ...content, optionList };
    }

    /**
     * adds default or optional modifiers to type label or description
     * @param option
     */
    export function mapDefinitionDetails<T>(
        definition: CommandLineOption<T>,
        options: ParseOptions<T>,
    ): CommandLineOption<T> {
        definition = mapOptionTypeLabel(definition, options);
        definition = mapOptionDescription(definition, options);

        return definition;
    }

    function mapOptionDescription<T>(definition: CommandLineOption<T>, options: ParseOptions<T>): CommandLineOption<T> {
        if (options.prependParamOptionsToDescription !== true || isBoolean(definition)) {
            return definition;
        }

        definition.description = definition.description || '';

        if (definition.defaultOption) {
            definition.description = `Default Option. ${definition.description}`;
        }

        if ((definition as unknown as OptionalProperty).optional === true) {
            definition.description = `Optional. ${definition.description}`;
        }

        if (definition.defaultValue != null) {
            definition.description = `Defaults to ${JSON.stringify(definition.defaultValue)}. ${definition.description}`;
        }

        return definition;
    }

    function mapOptionTypeLabel<T>(definition: CommandLineOption<T>, options: ParseOptions<T>): CommandLineOption<T> {
        if (options.displayOptionalAndDefault !== true || isBoolean(definition)) {
            return definition;
        }

        definition.typeLabel = definition.typeLabel || getTypeLabel(definition);

        if (definition.defaultOption) {
            definition.typeLabel = `${definition.typeLabel} (D)`;
        }

        if ((definition as unknown as OptionalProperty).optional === true) {
            definition.typeLabel = `${definition.typeLabel} (O)`;
        }

        return definition;
    }

    function getTypeLabel<T>(definition: CommandLineOption<T>) {
        let typeLabel = definition.type ? definition.type.name.toLowerCase() : 'string';
        const multiple = definition.multiple || definition.lazyMultiple ? '[]' : '';
        if (typeLabel) {
            typeLabel = typeLabel === 'boolean' ? '' : `{underline ${typeLabel}${multiple}}`;
        }

        return typeLabel;
    }

    export function isBoolean<T>(option: PropertyOptions<T>): boolean {
        return option.type.name === 'Boolean';
    }