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

getopt() public method

This method returns a char that is the current option that has been parsed from the command line. If the option takes an argument, then the internal variable optarg is set which is a string representing the the value of the argument. This value can be retrieved by the caller using the Optarg property. If an invalid option is found, an error message is printed and a '?' is returned. The name of the invalid option character can be retrieved by calling the Optopt property. When there are no more options to be scanned, this method returns -1. The index of first non-option element in argv can be retrieved with the Optind property.
public getopt ( ) : int
return int
        public int getopt()
        {
            this.optarg = null;

            if (this.endparse == true)
                return -1;

            if ((this.nextchar == null) || (this.nextchar.Length == 0))
            {
                // If we have just processed some options following some
                // non-options, exchange them so that the options come first.
                if (this.lastNonopt > this.optind)
                    this.lastNonopt = this.optind;
                if (this.firstNonopt > this.optind)
                    this.firstNonopt = this.optind;

                if (this.ordering == Order.Permute)
                {
                    // If we have just processed some options following some
                    // non-options, exchange them so that the options come
                    // first.
                    if ((this.firstNonopt != this.lastNonopt) &&
                        (this.lastNonopt != this.optind))
                        this.exchange(this.argv);
                    else if (this.lastNonopt != this.optind)
                        this.firstNonopt = this.optind;

                    // Skip any additional non-options
                    // and extend the range of non-options previously skipped.
                    while ((this.optind < this.argv.Length) &&
                        ((this.argv[optind].Length == 0) ||
                        (this.argv[this.optind][0] != '-') ||
                        this.argv[optind].Equals("-")))
                        this.optind++;

                    this.lastNonopt = this.optind;
                }

                // The special ARGV-element "--" means premature end of
                // options. Skip it like a null option, then exchange with
                // previous non-options as if it were an option, then skip
                // everything else like a non-option.
                if ((this.optind != this.argv.Length) &&
                    this.argv[this.optind].Equals("--"))
                {
                    this.optind++;

                    if ((this.firstNonopt != this.lastNonopt) &&
                        (this.lastNonopt != this.optind))
                        this.exchange(this.argv);
                    else if (this.firstNonopt == this.lastNonopt)
                        this.firstNonopt = this.optind;

                    this.lastNonopt = this.argv.Length;

                    this.optind = this.argv.Length;
                }

                // If we have done all the ARGV-elements, stop the scan
                // and back over any non-options that we skipped and permuted.
                if (this.optind == this.argv.Length)
                {
                    // Set the next-arg-index to point at the non-options that
                    // we previously skipped, so the caller will digest them.
                    if (this.firstNonopt != this.lastNonopt)
                        this.optind = this.firstNonopt;

                    return -1;
                }

                // If we have come to a non-option and did not permute it,
                // either stop the scan or describe it to the caller and pass
                // it by.
                if ((this.argv[this.optind].Length == 0) ||
                    (this.argv[this.optind][0] != '-') ||
                    this.argv[this.optind].Equals("-"))
                {
                    if (this.ordering == Order.RequireOrder)
                        return -1;

                    this.optarg = this.argv[optind++];
                    return 1;
                }

                // We have found another option-ARGV-element.
                // Skip the initial punctuation.
                if (this.argv[optind].StartsWith("--"))
                    this.nextchar = this.argv[this.optind].Substring(2);
                else
                    this.nextchar = this.argv[this.optind].Substring(1);
            }

            // Decode the current option-ARGV-element.

            /*	Check whether the ARGV-element is a long option.

                If longOnly and the ARGV-element has the form "-f", where f is
                a valid short option, don't consider it an abbreviated form of
                a long option that starts with f. Otherwise there would be no
                way to give the -f short option.

                On the other hand, if there's a long option "fubar" and	the
                ARGV-element is "-fu", do consider that an abbreviation of the
                long option, just like "--fu", and not "-f" with arg "u".

                This distinction seems to be the most useful approach.
            */
            if ((this.longOptions != null) &&
                (this.argv[this.optind].StartsWith("--") || (this.longOnly &&
                ((this.argv[this.optind].Length > 2) ||
                (this.optstring.IndexOf(this.argv[this.optind][1]) == -1)))))
            {
                int c = this.checkLongOption();

                if (this.longoptHandled)
                    return c;

                // Can't find it as a long option. If this is not
                // getopt_long_only, or the option starts with "--" or is not a
                // valid short option, then it's an error. Otherwise interpret
                // it as a short option.
                if (!this.longOnly || this.argv[this.optind].StartsWith("--")
                    || (this.optstring.IndexOf(this.nextchar[0]) == - 1))
                {
                    if (this.opterr)
                    {
                        if (this.argv[this.optind].StartsWith("--"))
                        {
                            object[] msgArgs = new object[]{
                                this.progname, this.nextchar };
                            System.Console.Error.WriteLine(
                                this.resManager.GetString("getopt.unrecognized",
                                this.cultureInfo), msgArgs);
                        }
                        else
                        {
                            object[] msgArgs = new object[]{ this.progname,
                                this.argv[optind][0], this.nextchar};
                            System.Console.Error.WriteLine(
                                this.resManager.GetString("getopt.unrecognized2",
                                this.cultureInfo), msgArgs);
                        }
                    }

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

                    return '?';
                }
            } // if (longopts)

            // Look at and handle the next short option-character */
            int c2 = this.nextchar[0]; //**** Do we need to check for empty str?
            if (this.nextchar.Length > 1)
                this.nextchar = this.nextchar.Substring(1);
            else
                this.nextchar = "";

            string temp = null;
            if (this.optstring.IndexOf((char) c2) != - 1)
                temp = this.optstring.Substring(
                    this.optstring.IndexOf((char) c2));

            if (this.nextchar.Length == 0)
                ++this.optind;

            if ((temp == null) || (c2 == ':'))
            {
                if (this.opterr)
                {
                    if (this.posixlyCorrect)
                    {
                        // 1003.2 specifies the format of this message
                        object[] msgArgs = new object[]{
                            this.progname, (char) c2 };
                        System.Console.Error.WriteLine(
                            this.resManager.GetString("getopt.illegal",
                            this.cultureInfo), msgArgs);
                    }
                    else
                    {
                        object[] msgArgs = new object[]{
                            this.progname, (char) c2 };
                        System.Console.Error.WriteLine(
                            this.resManager.GetString("getopt.invalid",
                            this.cultureInfo), msgArgs);
                    }
                }

                this.optopt = c2;

                return '?';
            }

            // Convenience. Treat POSIX -W foo same as long option --foo
            if ((temp[0] == 'W') && (temp.Length > 1) && (temp[1] == ';'))
            {
                if (this.nextchar.Length != 0)
                {
                    this.optarg = this.nextchar;
                }
                // No further cars in this argv element and no more argv
                // elements
                else if (this.optind == this.argv.Length)
                {
                    if (this.opterr)
                    {
                        // 1003.2 specifies the format of this message.
                        object[] msgArgs = new object[]{
                            this.progname, (char) c2 };
                        System.Console.Error.WriteLine(
                            this.resManager.GetString("getopt.requires2",
                            this.cultureInfo), msgArgs);
                    }

                    this.optopt = c2;
                    if (this.optstring[0] == ':')
                        return ':';
                    else
                        return '?';
                }
                else
                {
                    // We already incremented `optind' once; increment it again
                    // when taking next ARGV-elt as argument.
                    this.nextchar = this.argv[this.optind];
                    this.optarg = this.argv[this.optind];
                }

                c2 = this.checkLongOption();

                if (this.longoptHandled)
                    return c2;
                // Let the application handle it
                else
                {
                    this.nextchar = null;
                    ++this.optind;
                    return 'W';
                }
            }

            if ((temp.Length > 1) && (temp[1] == ':'))
            {
                if ((temp.Length > 2) && (temp[2] == ':'))
                // This is an option that accepts and argument optionally
                {
                    if (this.nextchar.Length != 0)
                    {
                        this.optarg = this.nextchar;
                        ++this.optind;
                    }
                    else
                    {
                        this.optarg = null;
                    }

                    this.nextchar = null;
                }
                else
                {
                    if (this.nextchar.Length != 0)
                    {
                        this.optarg = this.nextchar;
                        ++this.optind;
                    }
                    else if (this.optind == this.argv.Length)
                    {
                        if (this.opterr)
                        {
                            // 1003.2 specifies the format of this message
                            object[] msgArgs = new object[]{
                                this.progname, (char) c2};
                            System.Console.Error.WriteLine(
                                this.resManager.GetString("getopt.requires2",
                                this.cultureInfo), msgArgs);
                        }

                        this.optopt = c2;

                        if (this.optstring[0] == ':')
                            return ':';
                        else
                            return '?';
                    }
                    else
                    {
                        this.optarg = this.argv[this.optind];
                        ++this.optind;

                        // Ok, here's an obscure Posix case.  If we have o:,
                        // and we get -o -- foo, then we're supposed to skip
                        // the --, end parsing of options, and make foo an
                        // operand to -o. Only do this in Posix mode.
                        if (this.posixlyCorrect && this.optarg.Equals("--"))
                        {
                            // If end of argv, error out
                            if (this.optind == this.argv.Length)
                            {
                                if (this.opterr)
                                {
                                    // 1003.2 specifies the format of this
                                    // message
                                    object[] msgArgs = new object[]{
                                        this.progname, (char) c2};
                                    System.Console.Error.WriteLine(
                                        this.resManager.GetString(
                                        "getopt.requires2", this.cultureInfo),
                                        msgArgs);
                                }

                                this.optopt = c2;

                                if (this.optstring[0] == ':')
                                    return ':';
                                else
                                    return '?';
                            }

                            // Set new optarg and set to end. Don't permute as
                            // we do on -- up above since we know we aren't in
                            // permute mode because of Posix.
                            this.optarg = this.argv[this.optind];
                            ++this.optind;
                            this.firstNonopt = this.optind;
                            this.lastNonopt = this.argv.Length;
                            this.endparse = true;
                        }
                    }

                    this.nextchar = null;
                }
            }

            return c2;
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            var getopt = new Getopt(Assembly.GetExecutingAssembly().GetName().Name, args, "f:r:s:tv") { Opterr = false };

            string funcname = null;
            string resourcesPath = null;
            string filesPath = null;
            bool topOnly = false;
            bool verbose = false;

            int option;
            while (-1 != (option = getopt.getopt()))
            {
                switch (option)
                {
                    case 'f': funcname = getopt.Optarg; break;
                    case 'r': resourcesPath = getopt.Optarg; break;
                    case 's': filesPath = getopt.Optarg; break;
                    case 't': topOnly = true; break;
                    case 'v': verbose = true; break;

                    default: PrintUsage(); return;
                }
            }

            if (resourcesPath == null || filesPath == null)
            {
                PrintUsage();
                return;
            }

            var replacer = new Replacer(filesPath, resourcesPath, funcname, topOnly, verbose);
            replacer.Run();
        }
All Usage Examples Of Gnu.Getopt.Getopt::getopt