Ejemplo n.º 1
0
        public static Arguments ParseArguments(List <string> commandLineArguments)
        {
            if (commandLineArguments.Count == 0)
            {
                return(new Arguments
                {
                    TargetPath = Environment.CurrentDirectory
                });
            }

            var firstArgument = commandLineArguments.First();

            if (IsHelp(firstArgument))
            {
                return(new Arguments
                {
                    IsHelp = true
                });
            }
            if (IsInit(firstArgument))
            {
                return(new Arguments
                {
                    TargetPath = Environment.CurrentDirectory,
                    Init = true
                });
            }

            if (commandLineArguments.Count == 1 && !(commandLineArguments[0].StartsWith("-") || commandLineArguments[0].StartsWith("/")))
            {
                return(new Arguments
                {
                    TargetPath = firstArgument
                });
            }

            List <string> namedArguments;
            var           arguments = new Arguments();

            if (firstArgument.StartsWith("-") || firstArgument.StartsWith("/"))
            {
                arguments.TargetPath = Environment.CurrentDirectory;
                namedArguments       = commandLineArguments;
            }
            else
            {
                arguments.TargetPath = firstArgument;
                namedArguments       = commandLineArguments.Skip(1).ToList();
            }

            var args = CollectSwitchesAndValuesFromArguments(namedArguments);

            foreach (var name in args.AllKeys)
            {
                var values = args.GetValues(name);

                string value = null;

                if (values != null)
                {
                    //Currently, no arguments use more than one value, so having multiple values is an input error.
                    //In the future, this exception can be removed to support multiple values for a switch.
                    if (values.Length > 1)
                    {
                        throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", values[1]));
                    }

                    value = values.FirstOrDefault();
                }

                if (IsSwitch("l", name))
                {
                    arguments.LogFilePath = value;
                    continue;
                }

                if (IsSwitch("targetpath", name))
                {
                    arguments.TargetPath = value;
                    continue;
                }

                if (IsSwitch("dynamicRepoLocation", name))
                {
                    arguments.DynamicRepositoryLocation = value;
                    continue;
                }

                if (IsSwitch("url", name))
                {
                    arguments.TargetUrl = value;
                    continue;
                }

                if (IsSwitch("b", name))
                {
                    arguments.TargetBranch = value;
                    continue;
                }

                if (IsSwitch("u", name))
                {
                    arguments.Authentication.Username = value;
                    continue;
                }

                if (IsSwitch("p", name))
                {
                    arguments.Authentication.Password = value;
                    continue;
                }

                if (IsSwitch("c", name))
                {
                    arguments.CommitId = value;
                    continue;
                }

                if (IsSwitch("exec", name))
                {
                    arguments.Exec = value;
                    continue;
                }

                if (IsSwitch("execargs", name))
                {
                    arguments.ExecArgs = value;
                    continue;
                }

                if (IsSwitch("proj", name))
                {
                    arguments.Proj = value;
                    continue;
                }

                if (IsSwitch("projargs", name))
                {
                    arguments.ProjArgs = value;
                    continue;
                }

                if (IsSwitch("updateAssemblyInfo", name))
                {
                    if (new[] { "1", "true" }.Contains(value))
                    {
                        arguments.UpdateAssemblyInfo = true;
                    }
                    else if (new[] { "0", "false" }.Contains(value))
                    {
                        arguments.UpdateAssemblyInfo = false;
                    }
                    else if (!IsSwitchArgument(value))
                    {
                        arguments.UpdateAssemblyInfo         = true;
                        arguments.UpdateAssemblyInfoFileName = value;
                    }
                    else
                    {
                        arguments.UpdateAssemblyInfo = true;
                    }
                    continue;
                }

                if (IsSwitch("assemblyversionformat", name))
                {
                    throw new WarningException("assemblyversionformat switch removed, use AssemblyVersioningScheme configuration value instead");
                }

                if ((IsSwitch("v", name)) && VersionParts.Contains(value.ToLower()))
                {
                    arguments.ShowVariable = value.ToLower();
                    continue;
                }

                if (IsSwitch("showConfig", name))
                {
                    if (new[] { "1", "true" }.Contains(value))
                    {
                        arguments.ShowConfig = true;
                    }
                    else if (new[] { "0", "false" }.Contains(value))
                    {
                        arguments.UpdateAssemblyInfo = false;
                    }
                    else
                    {
                        arguments.ShowConfig = true;
                    }
                    continue;
                }

                if (IsSwitch("output", name))
                {
                    OutputType outputType;
                    if (!Enum.TryParse(value, true, out outputType))
                    {
                        throw new WarningException(string.Format("Value '{0}' cannot be parsed as output type, please use 'json' or 'buildserver'", value));
                    }

                    arguments.Output = outputType;
                    continue;
                }

                if (IsSwitch("nofetch", name))
                {
                    arguments.NoFetch = true;
                    continue;
                }

                throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", name));
            }

            return(arguments);
        }
Ejemplo n.º 2
0
        public static Arguments ParseArguments(List <string> commandLineArguments)
        {
            if (commandLineArguments.Count == 0)
            {
                return(new Arguments
                {
                    TargetPath = Environment.CurrentDirectory
                });
            }

            var firstArgument = commandLineArguments.First();

            if (IsHelp(firstArgument))
            {
                return(new Arguments
                {
                    IsHelp = true
                });
            }
            if (IsInit(firstArgument))
            {
                return(new Arguments
                {
                    TargetPath = Environment.CurrentDirectory,
                    Init = true
                });
            }

            if (commandLineArguments.Count == 1 && !(commandLineArguments[0].StartsWith("-") || commandLineArguments[0].StartsWith("/")))
            {
                return(new Arguments
                {
                    TargetPath = firstArgument
                });
            }

            List <string> namedArguments;
            var           arguments = new Arguments();

            if (firstArgument.StartsWith("-") || firstArgument.StartsWith("/"))
            {
                arguments.TargetPath = Environment.CurrentDirectory;
                namedArguments       = commandLineArguments;
            }
            else
            {
                arguments.TargetPath = firstArgument;
                namedArguments       = commandLineArguments.Skip(1).ToList();
            }

            for (var index = 0; index < namedArguments.Count; index = index + 2)
            {
                var name  = namedArguments[index];
                var value = namedArguments.Count > index + 1 ? namedArguments[index + 1] : null;

                if (IsSwitch("l", name))
                {
                    arguments.LogFilePath = value;
                    continue;
                }

                if (IsSwitch("targetpath", name))
                {
                    arguments.TargetPath = value;
                    continue;
                }

                if (IsSwitch("url", name))
                {
                    arguments.TargetUrl = value;
                    continue;
                }

                if (IsSwitch("b", name))
                {
                    arguments.TargetBranch = value;
                    continue;
                }

                if (IsSwitch("u", name))
                {
                    arguments.Authentication.Username = value;
                    continue;
                }

                if (IsSwitch("p", name))
                {
                    arguments.Authentication.Password = value;
                    continue;
                }

                if (IsSwitch("c", name))
                {
                    arguments.CommitId = value;
                    continue;
                }

                if (IsSwitch("exec", name))
                {
                    arguments.Exec = value;
                    continue;
                }

                if (IsSwitch("execargs", name))
                {
                    arguments.ExecArgs = value;
                    continue;
                }

                if (IsSwitch("proj", name))
                {
                    arguments.Proj = value;
                    continue;
                }

                if (IsSwitch("projargs", name))
                {
                    arguments.ProjArgs = value;
                    continue;
                }

                if (IsSwitch("updateAssemblyInfo", name))
                {
                    if (new[] { "1", "true" }.Contains(value))
                    {
                        arguments.UpdateAssemblyInfo = true;
                    }
                    else if (new[] { "0", "false" }.Contains(value))
                    {
                        arguments.UpdateAssemblyInfo = false;
                    }
                    else if (!IsSwitchArgument(value))
                    {
                        arguments.UpdateAssemblyInfo         = true;
                        arguments.UpdateAssemblyInfoFileName = value;
                    }
                    else
                    {
                        arguments.UpdateAssemblyInfo = true;
                        index--;
                    }
                    continue;
                }

                if (IsSwitch("assemblyversionformat", name))
                {
                    throw new WarningException("assemblyversionformat switch removed, use AssemblyVersioningScheme configuration value instead");
                }

                if ((IsSwitch("v", name)) && VersionParts.Contains(value.ToLower()))
                {
                    arguments.ShowVariable = value.ToLower();
                    continue;
                }

                if (IsSwitch("showConfig", name))
                {
                    if (new[] { "1", "true" }.Contains(value))
                    {
                        arguments.ShowConfig = true;
                    }
                    else if (new[] { "0", "false" }.Contains(value))
                    {
                        arguments.UpdateAssemblyInfo = false;
                    }
                    else
                    {
                        arguments.ShowConfig = true;
                        index--;
                    }
                    continue;
                }

                if (IsSwitch("output", name))
                {
                    OutputType outputType;
                    if (!Enum.TryParse(value, true, out outputType))
                    {
                        throw new WarningException(string.Format("Value '{0}' cannot be parsed as output type, please use 'json' or 'buildserver'", value));
                    }

                    arguments.Output = outputType;
                    continue;
                }

                throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", name));
            }

            return(arguments);
        }
Ejemplo n.º 3
0
        public static Arguments ParseArguments(List <string> commandLineArguments)
        {
            if (commandLineArguments.Count == 0)
            {
                return(new Arguments
                {
                    TargetPath = Environment.CurrentDirectory
                });
            }

            var firstArgument = commandLineArguments.First();

            if (IsHelp(firstArgument))
            {
                return(new Arguments
                {
                    IsHelp = true
                });
            }

            if (commandLineArguments.Count == 1)
            {
                return(new Arguments
                {
                    TargetPath = firstArgument
                });
            }

            List <string> namedArguments;
            var           arguments = new Arguments();

            if (firstArgument.StartsWith("-") || firstArgument.StartsWith("/"))
            {
                arguments.TargetPath = Environment.CurrentDirectory;
                namedArguments       = commandLineArguments;
            }
            else
            {
                arguments.TargetPath = firstArgument;
                namedArguments       = commandLineArguments.Skip(1).ToList();
            }

            EnsureArgumentsEvenCount(commandLineArguments, namedArguments);

            for (var index = 0; index < namedArguments.Count; index = index + 2)
            {
                var name  = namedArguments[index];
                var value = namedArguments[index + 1];

                if (IsSwitch("l", name))
                {
                    arguments.LogFilePath = value;
                    continue;
                }

                if (IsSwitch("url", name))
                {
                    arguments.TargetUrl = value;
                    continue;
                }

                if (IsSwitch("b", name))
                {
                    arguments.TargetBranch = value;
                    continue;
                }

                if (IsSwitch("u", name))
                {
                    arguments.Username = value;
                    continue;
                }

                if (IsSwitch("p", name))
                {
                    arguments.Password = value;
                    continue;
                }

                if ((IsSwitch("v", name)) && VersionParts.Contains(value.ToLower()))
                {
                    arguments.VersionPart = value.ToLower();
                    continue;
                }

                if (IsSwitch("output", name))
                {
                    var outputType = OutputType.Json;
                    if (!Enum.TryParse(value, true, out outputType))
                    {
                        throw new ErrorException(string.Format("Value '{0}' cannot be parsed as output type, please use 'json' or 'buildserver'", value));
                    }

                    arguments.Output = outputType;
                    continue;
                }

                throw new ErrorException(string.Format("Could not parse command line parameter '{0}'.", name));
            }
            return(arguments);
        }