示例#1
0
        private static bool TryParseArgs(string[] args, out Arguments arguments)
        {
            arguments = null;

            int argsLength = args != null ? args.Length : 0;

            if (argsLength == 0)
                return false;

            int i = 0;
            string flag;
            string value;

            #region get values from command line arguments
            while (i < argsLength)
            {
                if (!TryGetArgValue(ref i, args, i == 0, out flag, out value))
                    return false;

                arguments = arguments ?? new Arguments();

                switch(flag)
                {
                    case "--repoFolder":
                    case "-rf":
                    case "":
                        arguments.LocalRepoFolder = value;
                        PrintArgumentSetFromCmdLine("LocalRepoFolder", arguments.LocalRepoFolder);
                        break;

                    case "--developBranch":
                    case "-db":
                        arguments.DevelopLocalBranchName = value;
                        PrintArgumentSetFromCmdLine("DevelopLocalBranchName", arguments.DevelopLocalBranchName);
                        break;

                    case "--remoteName":
                    case "-rn":
                        arguments.RemoteName = value;
                        PrintArgumentSetFromCmdLine("RemoteName", arguments.RemoteName);
                        break;

                    case "--prFolder":
                    case "-pf":
                        arguments.LocalPRRepoFolder = value;
                        PrintArgumentSetFromCmdLine("LocalPRRepoFolder", arguments.LocalPRRepoFolder);
                        break;

                    case "--forkUrl":
                    case "-fu":
                        arguments.ForkUrl = value;
                        PrintArgumentSetFromCmdLine("ForkUrl", arguments.ForkUrl);
                        break;

                    case "--forkName":
                    case "-fn":
                        arguments.ForkName = value;
                        PrintArgumentSetFromCmdLine("ForkName", arguments.ForkName);
                        break;

                    case "--featureName":
                    case "-f":
                        arguments.FeatureName = value;
                        PrintArgumentSetFromCmdLine("FeatureName", arguments.FeatureName);
                        break;
                }
            }
            #endregion

            if (arguments == null || arguments.MissingValues())
            {
                #region get missing values from clipboard

                if (!Clipboard.ContainsText())
                    return false;

                string clipboardContent = Clipboard.GetText(TextDataFormat.UnicodeText);
                clipboardContent = clipboardContent.Trim();

                string folderSuffix;

                using (var sr = new StringReader(clipboardContent))
                {
                    string line = sr.ReadLine();

                    if (String.IsNullOrEmpty(line))
                        return false;

                    string[] words = line.Split(' ');

                    if (words.Length != 5)
                        return false;

                    folderSuffix = words[3];

                    arguments = arguments ?? new Arguments();

                    if (String.IsNullOrEmpty(arguments.DevelopLocalBranchName))
                    {
                        arguments.DevelopLocalBranchName = words[4];
                        PrintArgumentSetFromClipboard("DevelopLocalBranchName", arguments.DevelopLocalBranchName);
                    }

                    line = sr.ReadLine();

                    if (String.IsNullOrEmpty(line))
                        return false;

                    words = line.Split(' ');

                    if (words.Length != 4)
                        return false;

                    // override
                    arguments.ForkUrl = words[2];
                    PrintArgumentSetFromClipboard("ForkUrl", arguments.ForkUrl);

                    // override
                    arguments.FeatureName = words[3];
                    PrintArgumentSetFromClipboard("FeatureName", arguments.FeatureName);
                }

                if (String.IsNullOrEmpty(arguments.LocalPRRepoFolder))
                {
                    folderSuffix = folderSuffix.Replace('/', '_');

                    arguments.LocalPRRepoFolder = arguments.LocalRepoFolder + "-" + folderSuffix;
                    PrintArgumentSetFromClipboard("LocalPRRepoFolder", arguments.LocalPRRepoFolder);
                }

                // override
                {
                    var uri = new Uri(arguments.ForkUrl);
                    string baseUri = uri.GetLeftPart(System.UriPartial.Authority);

                    Uri forkRelUri = (new Uri(baseUri)).MakeRelativeUri(uri);

                    string[] parts = forkRelUri.ToString().Split('/');

                    arguments.ForkName = parts[0];
                    PrintArgumentSetFromClipboard("ForkName", arguments.ForkName);
                }
                #endregion
            }

            if (arguments == null || arguments.MissingValues())
            {
                #region get missing values from clipboard
                arguments = arguments ?? new Arguments();

                if (string.IsNullOrEmpty(arguments.LocalRepoFolder))
                    arguments.LocalRepoFolder = GetValueFromInput("LocalRepoFolder", Directory.GetCurrentDirectory());

                if (string.IsNullOrEmpty(arguments.DevelopLocalBranchName))
                    arguments.DevelopLocalBranchName = GetValueFromInput("DevelopLocalBranchName", "develop");

                if (string.IsNullOrEmpty(arguments.RemoteName))
                    arguments.RemoteName = GetValueFromInput("RemoteName", "origin");

                if (string.IsNullOrEmpty(arguments.LocalPRRepoFolder))
                    arguments.LocalPRRepoFolder = GetValueFromInput("LocalPRRepoFolder", Path.Combine(arguments.LocalRepoFolder, "-PR"));

                if (string.IsNullOrEmpty(arguments.ForkUrl))
                    arguments.ForkUrl = GetValueFromInput("ForkUrl");

                if (string.IsNullOrEmpty(arguments.ForkName))
                    arguments.ForkName = GetValueFromInput("ForkName");

                if (string.IsNullOrEmpty(arguments.FeatureName))
                    arguments.FeatureName = GetValueFromInput("FeatureName");

                #endregion
            }

            return arguments != null && !arguments.MissingValues();
        }