Gnu.Getopt.Getopt.checkLongOption C# (CSharp) Method

checkLongOption() private method

Check to see if an option is a valid long option. Called by getopt. Put in a separate method because this needs to be done twice. (The C getopt authors just copy-pasted the code!).
private checkLongOption ( ) : int
return int
        private int checkLongOption()
        {
            LongOpt pfound = null;
            int nameend;
            bool ambig;
            bool exact;

            this.longoptHandled = true;
            ambig = false;
            exact = false;
            this.longind = - 1;

            nameend = this.nextchar.IndexOf("=");
            if (nameend == - 1)
                nameend = this.nextchar.Length;

            // Test all long options for either exact match or abbreviated
            // matches
            for (int i = 0; i < this.longOptions.Length; i++)
            {
                if (this.longOptions[i].Name.StartsWith(
                    this.nextchar.Substring(0, nameend)))
                {
                    if (this.longOptions[i].Name.Equals(
                        this.nextchar.Substring(0, nameend)))
                    {
                        // Exact match found
                        pfound = this.longOptions[i];
                        this.longind = i;
                        exact = true;
                        break;
                    }
                    else if (pfound == null)
                    {
                        // First nonexact match found
                        pfound = this.longOptions[i];
                        this.longind = i;
                    }
                    else
                    {
                        // Second or later nonexact match found
                        ambig = true;
                    }
                }
            } // for

            // Print out an error if the option specified was ambiguous
            if (ambig && !exact)
            {
                if (this.opterr)
                {
                    object[] msgArgs = new object[]{
                        this.progname, this.argv[optind] };
                    System.Console.Error.WriteLine(
                        this.resManager.GetString("getopt.ambigious",
                        this.cultureInfo), msgArgs);
                }

                this.nextchar = "";
                this.optopt = 0;
                ++this.optind;

                return '?';
            }

            if (pfound != null)
            {
                ++this.optind;

                if (nameend != this.nextchar.Length)
                {
                    if (pfound.HasArg != Argument.No)
                    {
                        if (this.nextchar.Substring(nameend).Length > 1)
                            this.optarg = this.nextchar.Substring(nameend + 1);
                        else
                            this.optarg = "";
                    }
                    else
                    {
                        if (this.opterr)
                        {
                            // -- option
                            if (argv[this.optind - 1].StartsWith("--"))
                            {
                                object[] msgArgs = new object[]{
                                    this.progname, pfound.Name };
                                System.Console.Error.WriteLine(
                                    this.resManager.GetString(
                                    "getopt.arguments1", this.cultureInfo),
                                    msgArgs);
                            }
                            // +option or -option
                            else
                            {
                                object[] msgArgs = new object[]{ this.progname,
                                    this.argv[optind - 1][0], pfound.Name};
                                System.Console.Error.WriteLine(
                                    this.resManager.GetString(
                                    "getopt.arguments2", this.cultureInfo),
                                    msgArgs);
                            }
                        }

                        this.nextchar = "";
                        this.optopt = pfound.Val;

                        return '?';
                    }
                } // if (nameend)
                else if (pfound.HasArg == Argument.Required)
                {
                    if (this.optind < this.argv.Length)
                    {
                        this.optarg = this.argv[this.optind];
                        ++this.optind;
                    }
                    else
                    {
                        if (this.opterr)
                        {
                            object[] msgArgs = new object[]{
                                this.progname, this.argv[this.optind - 1]};
                            System.Console.Error.WriteLine(
                                this.resManager.GetString("getopt.requires",
                                this.cultureInfo), msgArgs);
                        }

                        this.nextchar = "";
                        this.optopt = pfound.Val;
                        if (this.optstring[0] == ':')
                            return ':';
                        else
                            return '?';
                    }
                } // else if (pfound)

                this.nextchar = "";

                if (pfound.Flag != null)
                {
                    pfound.Flag.Length = 0;
                    pfound.Flag.Append(pfound.Val);

                    return 0;
                }

                return pfound.Val;
            } // if (pfound != null)

            this.longoptHandled = false;

            return 0;
        }