public static async parseCommandArguments()

in src/DotnetUtils.ts [15:38]


    public static async parseCommandArguments(args: string): Promise<object> {
        let dictionary = {};

        // Regex matches dotnet build parameters: https://docs.microsoft.com/dotnet/core/tools/dotnet-build
        // \-\-?[A-Za-z\-]+     Parameter name, may have 1 or 2 dashes. Ex: -o, --verbose
        // (?:$|\s+)            End of line or whitespace separating param name and value
        // (?:[^\s"'\-]+        Param value, anything that's not a whitespace, ", ', or -
        //   |"[^"]*"|'[^']*')    ... or anything enclosed in either " or '
        const matches = args.match(/(\-\-?[A-Za-z\-]+(?:$|\s+)(?:[^\s"'\-]+|"[^"]*"|'[^']*')?)/g);
        matches?.forEach(match => {
            const whitespaceIndex = match.trim().indexOf(' ');
            if (whitespaceIndex >= 0) {
                // match is in the format of -paramName "param value"
                const paramName = match.substring(0, whitespaceIndex);
                const paramValue = match.substring(whitespaceIndex).trim();
                dictionary[paramName] = paramValue;
            } else {
                // match is only the param name with no value. Ex: --force, --verbose
                dictionary[match] = undefined;
            }
        });

        return dictionary;
    }