Example #1
0
        /// <summary>
        /// Parse all the command line arguments.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public static CommandOptions ParseArguments(
            IList <CommandOption> options, string[] arguments)
        {
            CommandOptions values = new CommandOptions();

            for (var i = 0; i < arguments.Length; i++)
            {
                if (arguments[i].StartsWith("-"))
                {
                    var option = FindCommandOption(options, arguments[i]);
                    if (option != null)
                    {
                        var value = new CommandOptionValue();

                        if (option.ValueType != CommandOption.CommandOptionValueType.NoValue)
                        {
                            if (i + 1 >= arguments.Length)
                            {
                                throw new LambdaToolsException($"Argument {arguments[i]} must be followed by a value", LambdaToolsException.ErrorCode.CommandLineParseError);
                            }

                            switch (option.ValueType)
                            {
                            case CommandOption.CommandOptionValueType.StringValue:
                                value.StringValue = arguments?[i + 1];
                                break;

                            case CommandOption.CommandOptionValueType.CommaDelimitedList:
                                value.StringValues = arguments?[i + 1].SplitByComma();
                                break;

                            case CommandOption.CommandOptionValueType.KeyValuePairs:
                                value.KeyValuePairs = Utilities.ParseKeyValueOption(arguments[i + 1]);
                                break;

                            case CommandOption.CommandOptionValueType.IntValue:
                                int iv;
                                if (!int.TryParse(arguments?[i + 1], out iv))
                                {
                                    throw new Exception($"Argument {arguments?[i]} expects an integer value but received an {arguments?[i + 1]}");
                                }
                                value.IntValue = iv;
                                break;

                            case CommandOption.CommandOptionValueType.BoolValue:
                                bool bv;
                                if (!bool.TryParse(arguments?[i + 1], out bv))
                                {
                                    throw new Exception($"Argument {arguments?[i]} expects either {bool.TrueString} or {bool.FalseString} value but received an {arguments?[i + 1]}");
                                }
                                value.BoolValue = bv;
                                break;
                            }
                            i++;
                        }

                        values.AddOption(option, value);
                    }
                }
                // Arguments starting /p: are msbuild parameters that should be passed into the dotnet package command
                else if (arguments[i].StartsWith("/p:"))
                {
                    if (string.IsNullOrEmpty(values.MSBuildParameters))
                    {
                        values.MSBuildParameters = arguments[i];
                    }
                    else
                    {
                        values.MSBuildParameters += " " + arguments[i];
                    }
                }
                // Collect arguments that are not attached to a switch. This is currently always the function name.
                else
                {
                    values.Arguments.Add(arguments[i]);
                }
            }

            return(values);
        }
Example #2
0
        /// <summary>
        /// Parse all the command line arguments.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public static CommandOptions ParseArguments(
            IList <CommandOption> options, string[] arguments)
        {
            CommandOptions values = new CommandOptions();

            for (int i = 0; i < arguments.Length; i++)
            {
                // Collect arguments that are not attached to a switch. This is currently always the function name.
                if (!arguments[i].StartsWith("-"))
                {
                    values.Arguments.Add(arguments[i]);
                }
                else
                {
                    var option = FindCommandOption(options, arguments[i]);
                    if (option != null)
                    {
                        var value = new CommandOptionValue();

                        if (option.ValueType != CommandOption.CommandOptionValueType.NoValue)
                        {
                            if (i + 1 >= arguments.Length)
                            {
                                throw new LambdaToolsException($"Argument {arguments[i]} must be followed by a value");
                            }

                            switch (option.ValueType)
                            {
                            case CommandOption.CommandOptionValueType.StringValue:
                                value.StringValue = arguments?[i + 1];
                                break;

                            case CommandOption.CommandOptionValueType.CommaDelimitedList:
                                value.StringValues = arguments?[i + 1].SplitByComma();
                                break;

                            case CommandOption.CommandOptionValueType.KeyValuePairs:
                                value.KeyValuePairs = Utilities.ParseKeyValueOption(arguments[i + 1]);
                                break;

                            case CommandOption.CommandOptionValueType.IntValue:
                                int iv;
                                if (!int.TryParse(arguments?[i + 1], out iv))
                                {
                                    throw new Exception($"Argument {arguments?[i]} expects an integer value but received an {arguments?[i + 1]}");
                                }
                                value.IntValue = iv;
                                break;

                            case CommandOption.CommandOptionValueType.BoolValue:
                                bool bv;
                                if (!bool.TryParse(arguments?[i + 1], out bv))
                                {
                                    throw new Exception($"Argument {arguments?[i]} expects either {bool.TrueString} or {bool.FalseString} value but received an {arguments?[i + 1]}");
                                }
                                value.BoolValue = bv;
                                break;
                            }
                            i++;
                        }

                        values.AddOption(option, value);
                    }
                }
            }

            return(values);
        }