示例#1
0
        ///////////////////////////////////////////////////////////////////////

        public static ReturnCode TryResolve(
            OptionDictionary options,
            string name,
            bool strict,
            bool noCase,
            ref IOption option,
            ref Result error
            )
        {
            if (options != null)
            {
                if (name != null)
                {
                    string     exactName = null;
                    StringList list      = new StringList();

                    foreach (KeyValuePair <string, IOption> pair in options)
                    {
                        string  key   = pair.Key;
                        IOption value = pair.Value;
                        bool    match;

                        if (noCase || ((value != null) && value.IsNoCase(options)))
                        {
                            match = (String.Compare(key, 0, name, 0, name.Length,
                                                    StringOps.SystemNoCaseStringComparisonType) == 0);
                        }
                        else
                        {
                            match = (String.Compare(key, 0, name, 0, name.Length,
                                                    StringOps.SystemStringComparisonType) == 0);
                        }

                        if (match)
                        {
                            //
                            // NOTE: Was the key valid (this should always succeed).
                            //
                            if (key != null)
                            {
                                //
                                // NOTE: It was a match; however, was it an exact match?
                                //
                                if (key.Length == name.Length)
                                {
                                    //
                                    // NOTE: Preserve match, it may differ in case.
                                    //
                                    exactName = key;
                                }

                                //
                                // NOTE: Was it an exact match or did we match at least one
                                //       character in a partial match?
                                //
                                if ((key.Length == name.Length) || (name.Length > 0))
                                {
                                    //
                                    // NOTE: Store the exact or partial match in the results
                                    //       dictionary.
                                    //
                                    list.Add(key);
                                }
                            }
                        }
                    }

                    //
                    // NOTE: If there was an exact match, just use it.
                    //
                    if (exactName != null)
                    {
                        //
                        // NOTE: Normal case, an exact option match was found.
                        //
                        option = options[exactName];

                        return(ReturnCode.Ok);
                    }
                    else if (list.Count == 1)
                    {
                        //
                        // NOTE: Normal case, exactly one option partially matched.
                        //
                        option = options[list[0]];

                        return(ReturnCode.Ok);
                    }
                    else if (list.Count > 1)
                    {
                        //
                        // NOTE: They specified an ambiguous option.
                        //
                        error = AmbiguousOption(options, name, list);
                    }
                    else if (strict)
                    {
                        //
                        // NOTE: They specified a non-existent option.
                        //
                        error = BadOption(options, name);
                    }
                    else
                    {
                        //
                        // NOTE: Non-strict mode, leave the original option value
                        //       unchanged and let the caller deal with it.
                        //
                        return(ReturnCode.Ok);
                    }
                }
                else
                {
                    error = "invalid option name";
                }
            }
            else
            {
                error = "invalid options";
            }

            return(ReturnCode.Error);
        }