Ejemplo n.º 1
0
		private static void ProcessArgument(IList<string> args, int i, ICollection<KeyValuePair<string, string>> options, string atFolder, IDictionary<char, string> shortOptions, InvertibleSet<string> twoArgOptions, int argLimit, bool expandEnvVars, bool caseSensitiveLongOpts)
		{
			string s = args[i];
			if (s == null)
				return;
			if (expandEnvVars)
				args[i] = s = Environment.ExpandEnvironmentVariables(s);

			if (s.StartsWith("-")) {
				Match m = CmdLineLongOptRegex.Match(s);
				if (m.Success)
				{
					// it's an --option
					string name = m.Groups[1].ToString();
					string value = m.Groups[3].ToString();
					if (value == "" && m.Groups[2].ToString() == "")
						value = null; // no value present (value=="" means value is present but empty)

					if (!caseSensitiveLongOpts)
						name = name.ToLowerInvariant();

					args[i] = null;
					if (twoArgOptions != null && twoArgOptions.Contains(name))
						MaybeRemoveArg(args, i + 1, expandEnvVars, ref value);
					AddPair(options, s, name, value);
				}
				else if (shortOptions != null)
				{
					// Check if short option(s) are valid
					bool reject = false;
					int div;
					char ch = '\0';
					for (div = 1; div < s.Length; div++) {
						ch = s[div];
						if (!shortOptions.ContainsKey(ch)) {
							if (char.IsLetter(ch))
								reject = true;
							break;
						}
					}

					int afterDiv = (ch == ':' || ch == '=' ? div + 1 : div);
					string value = div < s.Length ? s.Substring(afterDiv) : null;

					if (div > 1 && !reject) // is s entirely valid?
					{
						// detect space-separated argument
						ch = s[div - 1];
						if (twoArgOptions != null && twoArgOptions.Contains(shortOptions[ch] ?? ch.ToString()))
							MaybeRemoveArg(args, i + 1, expandEnvVars, ref value);

						args[i] = null;
						for (int c = 1; c < div; c++) {
							ch = s[c];
							string longName = shortOptions[ch] ?? ch.ToString();
							string curValue = c + 1 == div ? value : null;
							AddPair(options, s, longName, curValue);
						}
					}
				}
			}
			else if (atFolder != null && s.StartsWith("@"))
			{
				// e.g. "@list of options.txt"
				string atFile = s.Substring(1);
				string fileContents = null;
				try {
					string fullpath = Path.Combine(atFolder, atFile);
					if (File.Exists(fullpath))
						fileContents = File.OpenText(fullpath).ReadToEnd();
				} catch (Exception e) {
					MessageSink.Default.Error(s, "Unable to use option file \"{0}\": {1}", atFile, e.Message);
				}
				if (fileContents != null) {
					List<string> list = G.SplitCommandLineArguments(fileContents);

					int maxMore = System.Math.Max(0, argLimit - args.Count);
					if (list.Count > maxMore) {
						// oops, command limit exceeded
						MessageSink.Default.Warning(s, "Limit of {0} commands exceeded", argLimit);
						list.RemoveRange(maxMore, list.Count - maxMore);
					}

					args.InsertRange(i + 1, (IList<string>) list);
				}
			}
		}
Ejemplo n.º 2
0
        private static void ProcessArgument(IList <string> args, int i, ICollection <KeyValuePair <string, string> > options, string atFolder, IDictionary <char, string> shortOptions, InvertibleSet <string> twoArgOptions, int argLimit, bool expandEnvVars, bool caseSensitiveLongOpts)
        {
            string s = args[i];

            if (s == null)
            {
                return;
            }
            if (expandEnvVars)
            {
                args[i] = s = Environment.ExpandEnvironmentVariables(s);
            }

            if (s.StartsWith("-"))
            {
                Match m = CmdLineLongOptRegex.Match(s);
                if (m.Success)
                {
                    // it's an --option
                    string name  = m.Groups[1].ToString();
                    string value = m.Groups[3].ToString();
                    if (value == "" && m.Groups[2].ToString() == "")
                    {
                        value = null;                         // no value present (value=="" means value is present but empty)
                    }
                    if (!caseSensitiveLongOpts)
                    {
                        name = name.ToLowerInvariant();
                    }

                    args[i] = null;
                    if (twoArgOptions != null && twoArgOptions.Contains(name))
                    {
                        MaybeRemoveArg(args, i + 1, expandEnvVars, ref value);
                    }
                    AddPair(options, s, name, value);
                }
                else if (shortOptions != null)
                {
                    // Check if short option(s) are valid
                    bool reject = false;
                    int  div;
                    char ch = '\0';
                    for (div = 1; div < s.Length; div++)
                    {
                        ch = s[div];
                        if (!shortOptions.ContainsKey(ch))
                        {
                            if (char.IsLetter(ch))
                            {
                                reject = true;
                            }
                            break;
                        }
                    }

                    int    afterDiv = (ch == ':' || ch == '=' ? div + 1 : div);
                    string value    = div < s.Length ? s.Substring(afterDiv) : null;

                    if (div > 1 && !reject)                     // is s entirely valid?
                    {
                        // detect space-separated argument
                        ch = s[div - 1];
                        if (twoArgOptions != null && twoArgOptions.Contains(shortOptions[ch] ?? ch.ToString()))
                        {
                            MaybeRemoveArg(args, i + 1, expandEnvVars, ref value);
                        }

                        args[i] = null;
                        for (int c = 1; c < div; c++)
                        {
                            ch = s[c];
                            string longName = shortOptions[ch] ?? ch.ToString();
                            string curValue = c + 1 == div ? value : null;
                            AddPair(options, s, longName, curValue);
                        }
                    }
                }
            }
            else if (atFolder != null && s.StartsWith("@"))
            {
                // e.g. "@list of options.txt"
                string atFile       = s.Substring(1);
                string fileContents = null;
                try {
                    string fullpath = Path.Combine(atFolder, atFile);
                    if (File.Exists(fullpath))
                    {
                        fileContents = File.OpenText(fullpath).ReadToEnd();
                    }
                } catch (Exception e) {
                    MessageSink.Default.Error(s, "Unable to use option file \"{0}\": {1}", atFile, e.Message);
                }
                if (fileContents != null)
                {
                    List <string> list = G.SplitCommandLineArguments(fileContents);

                    int maxMore = System.Math.Max(0, argLimit - args.Count);
                    if (list.Count > maxMore)
                    {
                        // oops, command limit exceeded
                        MessageSink.Default.Warning(s, "Limit of {0} commands exceeded", argLimit);
                        list.RemoveRange(maxMore, list.Count - maxMore);
                    }

                    args.InsertRange(i + 1, (IList <string>)list);
                }
            }
        }