Beispiel #1
0
        /// <summary>
        /// Construct a Getopt instance with given input data that is capable
        /// of parsing long options and short options.  Contrary to what you
        /// might think, the flag <paramref name="longOnly"/> does not
        /// determine whether or not we scan for only long arguments. Instead,
        /// a value of true here allows long arguments to start with a
        /// '<c>-</c>' instead of "<c>--</c>" unless there is a conflict with
        /// a short option name.
        /// </summary>
        /// <param name="progname">
        /// The name to display as the program name when printing errors
        /// </param>
        /// <param name="argv">
        /// The string array passed as the command ilne to the program.
        /// </param>
        /// <param name="optstring">
        /// A string containing a description of the valid short args for this
        /// program.
        /// </param>
        /// <param name="longOptions">
        /// An array of <see cref="LongOpt"/> objects that describes the valid
        /// long args for this program.
        /// </param>
        /// <param name="longOnly">
        /// true if long options that do not conflict with short options can
        /// start with a '<c>-</c>' as well as "<c>--</c>".
        /// </param>
        public Getopt(string progname, string[] argv, string optstring,
            LongOpt[] longOptions, bool longOnly)
        {
            if (optstring.Length == 0)
                optstring = " ";

            // This function is essentially _getopt_initialize from GNU getopt
            this.progname = progname;
            this.argv = argv;
            this.optstring = optstring;
            this.longOptions = longOptions;
            this.longOnly = longOnly;

            // Check for application setting "Gnu.PosixlyCorrect" to determine
            // whether to strictly follow the POSIX standard. This replaces the
            // "POSIXLY_CORRECT" environment variable in the C version
            try
            {
                if((bool) new AppSettingsReader().GetValue(
                    "Gnu.PosixlyCorrect", typeof(bool)))
                {
                    this.posixlyCorrect = true;
                    this.cultureInfo = new CultureInfo("en-US");
                }
                else
                    this.posixlyCorrect = false;
            }
            catch(Exception)
            {
                this.posixlyCorrect = false;
            }

            // Determine how to handle the ordering of options and non-options
            if (optstring[0] == '-')
            {
                this.ordering = Order.ReturnInOrder;
                if (optstring.Length > 1)
                    this.optstring = optstring.Substring(1);
            }
            else if (optstring[0] == '+')
            {
                this.ordering = Order.RequireOrder;
                if (optstring.Length > 1)
                    this.optstring = optstring.Substring(1);
            }
            else if (this.posixlyCorrect)
            {
                this.ordering = Order.RequireOrder;
            }
            else
            {
                this.ordering = Order.Permute; // The normal default case
            }
        }
Beispiel #2
0
        /// <summary>
        /// Construct a Getopt instance with given input data that is capable
        /// of parsing long options as well as short.
        /// </summary>
        /// <param name="progname">
        /// The name to display as the program name when printing errors.
        /// </param>
        /// <param name="argv">
        /// The string array passed as the command ilne to the program.
        /// </param>
        /// <param name="optstring">
        /// A string containing a description of the valid short args for this
        /// program.
        /// </param>
        /// <param name="longOptions">
        /// An array of <see cref="LongOpt"/> objects that describes the valid
        /// long args for this program.
        /// </param>
        public Getopt(string progname, string[] argv, string optstring,
            LongOpt[] longOptions)
            : this(progname, argv, optstring,
			longOptions, false)
        {
        }