コード例 #1
0
        public bool Execute()
        {
            if (AddOption.IsPresent)
            {
                foreach (DevCommandManager.Argument arg in AddOption.Arguments.Where(arg => arg.IsPresent))
                {
                    AddCLA((string)arg.Value);
                }
            }

            if (DeleteOption.IsPresent)
            {
                DeleteMatchingCLAs((string)DeleteOption.Arguments[0].Value);
            }

            if (DeleteAllOption.IsPresent)
            {
                var numDeleted = CommandLineArgs.DeleteAllCommandLineArguments();
                Debug.Log("Deleted all " + numDeleted + " added command line arguments");
            }

            if (ReplaceOption.IsPresent)
            {
                var newCla         = (string)ReplaceOption.Arguments[0].Value;
                var equalSignIndex = newCla.IndexOf('=');
                var oldKey         = equalSignIndex < 0 ? newCla : newCla.Substring(0, equalSignIndex);
                DeleteMatchingCLAs(oldKey);
                AddCLA(newCla);
            }

            if (DumpOption.IsPresent)
            {
                var args = CommandLineArgs.GetCommandLineArguments();

                foreach (var arg in args)
                {
                    Debug.Log(arg);
                }

                Debug.Log(args.Count + " command line arguments total");
            }

            // This is meant for copying from VGS web interface 'parameters' box
            if (AddFromClipboardOption.IsPresent || CopyFromClipboardOption.IsPresent)
            {
                if (CopyFromClipboardOption.IsPresent)
                {
                    CommandLineArgs.DeleteAllCommandLineArguments();
                }

                var allText = Clipboard.GetText();
                if (!string.IsNullOrEmpty(allText))
                {
                    var text = new string(allText.TakeWhile(c => c != '\n').ToArray());    // Just take the first line of text
                    Debug.Log("Parsing this text from clipboard: " + text);
                    //       example:  -ShowPolls=false -title="SCORES BY ROUND" -isVGSJob -foo=Bar
                    // translates to:   ShowPolls=false  title=SCORES BY ROUND    isVGSJob  foo=Bar
                    while (true)
                    {
                        text = text.Trim();
                        var hyphenIndex = text.IndexOf('-');
                        if (hyphenIndex < 0)
                        {
                            if (!string.IsNullOrEmpty(text))
                            {
                                Debug.Log("Warning: Ignoring extra text: " + text);
                            }
                            break;
                        }
                        if (hyphenIndex > 0)
                        {
                            Debug.Log("Warning: Ignoring extra text: " + text.Substring(0, hyphenIndex));
                        }
                        text = text.Substring(hyphenIndex + 1);

                        // Hyphen found; now look for the next equals sign, OR space (for a CLA with no value)
                        var equalsIndex = text.IndexOf('=');
                        var spaceIndex  = text.IndexOf(' ');
                        if (equalsIndex < 0 && spaceIndex < 0)   // Neither
                        {
                            AddCLA(text);
                            break;
                        }
                        if ((equalsIndex < 0) ||                           // Space only
                            (spaceIndex >= 0 && spaceIndex < equalsIndex)) // Both space and equals were found
                        {
                            // Found a CLA with no value (e.g. -isVGSJob)
                            AddCLA(text.Substring(0, spaceIndex));
                            text = text.Substring(spaceIndex + 1);
                            continue;
                        }
                        // There is an equals, and it came before any space.  Now look for quotes in the value
                        var quoteIndex        = text.IndexOf('"');
                        var onePastValueIndex = spaceIndex;
                        if (quoteIndex >= 0 && quoteIndex == equalsIndex + 1)
                        {
                            var nextQuoteIndex = text.IndexOf('"', quoteIndex + 1);
                            if (nextQuoteIndex < 0)
                            {
                                Debug.Log("Error parsing CLAs from clipboard: missing closing quote after " + text);
                                return(false);
                            }
                            // Remote the quotes
                            text = text.Substring(0, quoteIndex) + text.Substring(quoteIndex + 1, (nextQuoteIndex - quoteIndex) - 1) + text.Substring(nextQuoteIndex + 1);
                            onePastValueIndex = nextQuoteIndex - 2 + 1;
                        }
                        if (onePastValueIndex < 0)
                        {
                            AddCLA(text);
                            break;
                        }
                        AddCLA(text.Substring(0, onePastValueIndex));
                        if (onePastValueIndex < text.Length - 1)
                        {
                            text = text.Substring(onePastValueIndex + 1);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    Debug.Log("Error: Clipboard did not contain text");
                    return(false);
                }
            }

            if (DumpToClipboardOption.IsPresent)      // This is meant for copy TO VGS web interface 'parameters' box
            {
                var text = "";
                foreach (var cla in CommandLineArgs.AddedCommandLineArgs)
                {
                    var formattedCla = cla;
                    var spaceIndex   = cla.IndexOf(' ');
                    if (spaceIndex >= 0)
                    {
                        var equalsIndex = cla.IndexOf('=');
                        if (equalsIndex >= 0)
                        {
                            formattedCla = cla.Substring(0, equalsIndex + 1) + '"' + cla.Substring(equalsIndex + 1, (cla.Length - equalsIndex) - 1) + '"';
                        }
                        else
                        {
                            Debug.Log("Error: Command line argument has a space but not an equals sign");   // This should not happen
                        }
                    }
                    text += " -" + formattedCla;
                }
                if (string.IsNullOrEmpty(text))
                {
                    text = "(No parameters)";
                }

                Clipboard.SetText(text);
                Debug.Log("Copied this string to Windows clipboard: " + text);
            }

            if (TestOption.IsPresent)
            {
                var value = CommandLineArgs.GetArgumentValue((string)TestOption.Arguments[0].Value);
                Debug.Log("Value found: " + value);
            }

            return(true);
        }