Example #1
0
        /// <summary>
        /// Check if the option is a duplicate (2nd, 3rd, etc. declaration) and the option
        /// does not support multiple values/declarations
        /// </summary>
        /// <param name="optDef">The option found</param>
        /// <param name="foundDefinitions">All of the found definitions so far</param>
        /// <param name="dupOptHandleType">How to handle duplicates</param>
        /// <returns>True if the option has already been found, and the type
        /// does not support duplicates</returns>
        private bool CheckIfDup(OptionDefinition optDef, ArrayList foundDefinitions,
                                DupOptHandleType dupOptHandleType)
        {
            if (foundDefinitions.IndexOf(optDef) == -1)
            {
                return(false);
            }

            switch (optDef.Type)
            {
            // types that allow duplicates:
            case OptValType.MultValue:
            case OptValType.IncrementalFlag:
                return(false);

            default:
                switch (dupOptHandleType)
                {
                case DupOptHandleType.Error:
                    throw new ParseException(
                              string.Format("Option {0} does not support duplicate values",
                                            optDef.ID));

                case DupOptHandleType.Warning:
                    if (OptionWarning != null)
                    {
                        OptionWarning(this, new OptionWarningEventArgs(
                                          string.Format("Option {0} does not support duplicate values",
                                                        optDef.ID)));
                    }
                    break;
                }
                return(true);
            }
        }
Example #2
0
        /// <summary>
        /// Parse a concatinated list of short options
        /// </summary>
        /// <param name="args">All the arguments</param>
        /// <param name="options">The options found</param>
        /// <param name="index">The current index in the arguments which can be incremented
        /// by this function</param>
        /// <param name="dupOptHandleType">How to handle un-expected duplicate option
        /// definitions</param>
        /// <param name="unknownOptHandleType">How to handle options that were not
        /// defined</param>
        /// <param name="caseSesitive">True if parsing is case-sensitive</param>
        /// <param name="foundDefinitions">The option definitions already found</param>
        /// <param name="unknownChars">An array of all unknown option characters or null
        /// if all were known</param>
        private void ParseShortOptions(string[] args, char[] options, bool caseSesitive, ref int index,
                                       DupOptHandleType dupOptHandleType, UnknownOptHandleType unknownOptHandleType,
                                       ArrayList foundDefinitions, out char[] unknownChars)
        {
            ArrayList unknownCharsList = new ArrayList();
            bool      wasUnknown;

            foreach (char option in options)
            {
                ParseShortOption(args, option, null, caseSesitive, ref index, dupOptHandleType,
                                 unknownOptHandleType, foundDefinitions, out wasUnknown);

                if (wasUnknown)
                {
                    unknownCharsList.Add(option);
                }
            }

            if (unknownCharsList.Count == 0)
            {
                unknownChars = null;
            }
            else
            {
                unknownChars = (char[])unknownCharsList.ToArray(typeof(char));
            }
        }
Example #3
0
 /// <summary>
 /// Parse a long option
 /// </summary>
 /// <param name="args">All the arguments</param>
 /// <param name="option">The option name found</param>
 /// <param name="value">The value immediately following the option ([=:]value syntax)</param>
 /// <param name="index">The current index in the arguments which can be incremented
 /// by this function</param>
 /// <param name="dupOptHandleType">How to handle un-expected duplicate option
 /// definitions</param>
 /// <param name="unknownOptHandleType">How to handle options that were not
 /// defined</param>
 /// <param name="caseSesitive">True if parsing is case-sensitive</param>
 /// <param name="foundDefinitions">The option definitions already found</param>
 /// <param name="wasUnknown">If the option was unknown and the handle type was not
 /// error, this will be true so that the "option" can be added to the arguments</param>
 private void ParseLongOption(string[] args, string option, string value, bool caseSesitive,
                              ref int index, DupOptHandleType dupOptHandleType,
                              UnknownOptHandleType unknownOptHandleType, ArrayList foundDefinitions, out bool wasUnknown)
 {
     ParseOption(args, option, _resultsHandler.GetOptionDefinition(option, caseSesitive),
                 value, ref index, dupOptHandleType, unknownOptHandleType, foundDefinitions,
                 out wasUnknown);
 }
Example #4
0
        /// <summary>
        /// Parse an option
        /// </summary>
        /// <param name="args">All the arguments</param>
        /// <param name="optName">The char or string that the option was identified with</param>
        /// <param name="optDef">The option found</param>
        /// <param name="value">The value immediately following the option ([=:]value syntax)</param>
        /// <param name="index">The current index in the arguments which can be incremented
        /// by this function</param>
        /// <param name="dupOptHandleType">How to handle un-expected duplicate option
        /// definitions</param>
        /// <param name="unknownOptHandleType">How to handle options that were not
        /// defined</param>
        /// <param name="foundDefinitions">The option definitions already found</param>
        /// <param name="wasUnknown">If the option was unknown and the handle type was not
        /// error, this will be true so that the "option" can be added to the arguments</param>
        private void ParseOption(string[] args, object optName,
                                 OptionDefinition optDef, string value,
                                 ref int index, DupOptHandleType dupOptHandleType,
                                 UnknownOptHandleType unknownOptHandleType, ArrayList foundDefinitions, out bool wasUnknown)
        {
            OptionResult result;

            // make sure the option is found
            if (optDef == null)
            {
                switch (unknownOptHandleType)
                {
                case UnknownOptHandleType.Error:
                    throw new ParseException(
                              string.Format("Option {0} is not recoginzed", optName));

                case UnknownOptHandleType.Warning:
                    if (OptionWarning != null)
                    {
                        OptionWarning(this, new OptionWarningEventArgs(
                                          string.Format("Option {0} is not recoginzed", optName)));
                    }
                    break;
                }

                wasUnknown = true;
                return;
            }
            else
            {
                wasUnknown = false;
            }

            // make sure value is not given went it should not be given
            CheckIfValueless(optDef, value);

            result = _resultsHandler[optDef];

            // make sure value is not duplicate when it should not be
            CheckIfDup(optDef, foundDefinitions, dupOptHandleType);
            foundDefinitions.Add(optDef);

            switch (optDef.Type)
            {
            case OptValType.Flag:
                if (result == null)
                {
                    result = new OptionResult(optDef);
                }

                result.NumDefinitions   = 1;
                result.Value            = true;
                _resultsHandler[optDef] = result;
                break;

            case OptValType.IncrementalFlag:
                if (result == null)
                {
                    result = new OptionResult(optDef);
                }

                result.NumDefinitions++;
                result.Value            = result.NumDefinitions;
                _resultsHandler[optDef] = result;
                break;

            case OptValType.MultValue:
                value = GetValue(args, value, ref index);

                if (value == null)
                {
                    throw new ParseException(
                              string.Format("Option {0} requires a value", optDef.ID));
                }

                if (result == null)
                {
                    result = new OptionResult(optDef);
                }

                result.NumDefinitions++;
                result.AddValue(optDef.ConvertValue(value));
                _resultsHandler[optDef] = result;
                break;

            case OptValType.ValueOpt:
                value = GetValue(args, value, ref index);

                if (result == null)
                {
                    result = new OptionResult(optDef);
                }

                result.NumDefinitions   = 1;
                result.Value            = optDef.ConvertValue(value);
                _resultsHandler[optDef] = result;
                break;

            case OptValType.ValueReq:
                value = GetValue(args, value, ref index);

                if (value == null)
                {
                    throw new ParseException(
                              string.Format("Option {0} requires a value", optDef.ID));
                }

                if (result == null)
                {
                    result = new OptionResult(optDef);
                }

                result.NumDefinitions   = 1;
                result.Value            = optDef.ConvertValue(value);
                _resultsHandler[optDef] = result;
                break;
            }
        }
Example #5
0
        /// <summary>
        /// Parse the arguments for options using the given settings
        /// </summary>
        /// <param name="optStyle">What type of options to parse</param>
        /// <param name="unixShortOption">How to parse unix short options (ignored
        /// if not unix style parsing)</param>
        /// <param name="dupOptHandleType">How to handle un-expected duplicate option
        /// definitions</param>
        /// <param name="unknownOptHandleType">How to handle options that were not
        /// defined</param>
        /// <param name="caseSesitive">If the parsing of options
        /// should consider the case of the options</param>
        /// <param name="args">The command-line arguments to parse</param>
        /// <returns>Arguments from the command-line that were not options</returns>
        public string[] Parse(OptStyle optStyle, UnixShortOption unixShortOption,
                              DupOptHandleType dupOptHandleType, UnknownOptHandleType unknownOptHandleType,
                              bool caseSesitive, string[] args)
        {
            ArrayList        foundDefinitions = new ArrayList();
            Group            grp;
            Match            m;
            Regex            re;
            StringCollection arguments;
            bool             isShort;
            bool             unknown;
            string           name;
            string           value;

            string[] results;

            if (args == null || args.Length == 0)
            {
                if (this.SearchEnvironment)
                {
                    ParseEnvironment(foundDefinitions, caseSesitive);
                }
                return(new string[0]);
            }

            arguments = new StringCollection();

            re = BuildRegEx(optStyle, unixShortOption);

            for (int i = 0; i < args.Length; i++)
            {
                m = re.Match(args[i]);

                if (m.Success)
                {
                    // see if there is a value
                    grp = m.Groups["shortname"];

                    if (grp != null && grp.Value != string.Empty)
                    {
                        isShort = true;
                        name    = grp.Value;
                        value   = m.Groups["shortvalue"].Value;
                    }
                    else
                    {
                        isShort = false;
                        name    = m.Groups["longname"].Value;
                        value   = m.Groups["longvalue"].Value;

                        // remove the '=' or ':' at the beginning of the value
                        if (value != null && value.Length > 0)
                        {
                            value = value.Substring(1);
                        }
                    }

                    if (optStyle == OptStyle.Unix)
                    {
                        if (isShort == false)
                        {
                            ParseLongOption(args, name, value, caseSesitive,
                                            ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions,
                                            out unknown);
                        }
                        // see if the value is just concatinated short options
                        else if (unixShortOption == UnixShortOption.CollapseShort)
                        {
                            // name is the first option
                            ParseShortOption(args, name[0], null, caseSesitive,
                                             ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions,
                                             out unknown);

                            // parse the rest of the options that lie in the 'value' variable
                            if (value != null)
                            {
                                char[] unknownOptions;

                                ParseShortOptions(args, value.ToLower().ToCharArray(),
                                                  caseSesitive, ref i, dupOptHandleType, unknownOptHandleType,
                                                  foundDefinitions, out unknownOptions);

                                // don't do anything with unknown concatinated short options,
                                // they should not be considered options or arguments
                            }
                        }
                        else
                        {
                            // name is the first option
                            ParseShortOption(args, name[0], value, caseSesitive,
                                             ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions,
                                             out unknown);
                        }
                    }
                    else
                    {
                        if (name.Length == 1)
                        {
                            ParseShortOption(args, name[0], value, caseSesitive,
                                             ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions,
                                             out unknown);
                        }
                        else
                        {
                            ParseLongOption(args, name, value, caseSesitive, ref i,
                                            dupOptHandleType, unknownOptHandleType, foundDefinitions,
                                            out unknown);
                        }
                    }

                    // consider unknown options to be arguments
                    if (unknown)
                    {
                        arguments.Add(args[i]);
                    }
                }
                else
                {
                    arguments.Add(args[i]);
                }
            }


            // parse the environment
            if (this.SearchEnvironment)
            {
                ParseEnvironment(foundDefinitions, caseSesitive);
            }

            results = new string[arguments.Count];
            arguments.CopyTo(results, 0);
            return(results);
        }
Example #6
0
		/// <summary>
		/// Check if the option is a duplicate (2nd, 3rd, etc. declaration) and the option
		/// does not support multiple values/declarations
		/// </summary>
		/// <param name="optDef">The option found</param>
		/// <param name="foundDefinitions">All of the found definitions so far</param>
		/// <param name="dupOptHandleType">How to handle duplicates</param>
		/// <returns>True if the option has already been found, and the type
		/// does not support duplicates</returns>
		private bool CheckIfDup(OptionDefinition optDef, ArrayList foundDefinitions, 
			DupOptHandleType dupOptHandleType)
		{
			if (foundDefinitions.IndexOf(optDef) == -1) return false;

			switch (optDef.Type)
			{
					// types that allow duplicates:
				case OptValType.MultValue:
				case OptValType.IncrementalFlag:
					return false;
				default:
					switch (dupOptHandleType)
					{
						case DupOptHandleType.Error:	
							throw new ParseException(
								string.Format("Option {0} does not support duplicate values",
								optDef.ID));
						case DupOptHandleType.Warning:
							if (OptionWarning != null)
								OptionWarning(this, new OptionWarningEventArgs(
									string.Format("Option {0} does not support duplicate values",
								optDef.ID)));
							break;
					}
					return true;
			}
		}
Example #7
0
		/// <summary>
		/// Parse an option
		/// </summary>
		/// <param name="args">All the arguments</param>
		/// <param name="optName">The char or string that the option was identified with</param>
		/// <param name="optDef">The option found</param>
		/// <param name="value">The value immediately following the option ([=:]value syntax)</param>
		/// <param name="index">The current index in the arguments which can be incremented
		/// by this function</param>
		/// <param name="dupOptHandleType">How to handle un-expected duplicate option
		/// definitions</param>
		/// <param name="unknownOptHandleType">How to handle options that were not
		/// defined</param>
		/// <param name="foundDefinitions">The option definitions already found</param>
		/// <param name="wasUnknown">If the option was unknown and the handle type was not
		/// error, this will be true so that the "option" can be added to the arguments</param>
		private void ParseOption(string[] args, object optName,
			OptionDefinition optDef, string value, 
			ref int index, DupOptHandleType dupOptHandleType, 
			UnknownOptHandleType unknownOptHandleType, ArrayList foundDefinitions, out bool wasUnknown)
		{
			OptionResult result;

			// make sure the option is found
			if (optDef == null)
			{
				switch (unknownOptHandleType)
				{
					case UnknownOptHandleType.Error:
						throw new ParseException(
							string.Format("Option {0} is not recoginzed", optName));
					case UnknownOptHandleType.Warning:
						if (OptionWarning != null)
							OptionWarning(this, new OptionWarningEventArgs(
								string.Format("Option {0} is not recoginzed", optName)));
						break;
				}

				wasUnknown = true;
				return;
			}
			else
				wasUnknown = false;

			// make sure value is not given went it should not be given
			CheckIfValueless(optDef, value);

			result = _resultsHandler[optDef];

			// make sure value is not duplicate when it should not be
			CheckIfDup(optDef, foundDefinitions, dupOptHandleType);
			foundDefinitions.Add(optDef);

			switch (optDef.Type)
			{
				case OptValType.Flag:
					if (result == null)
						result = new OptionResult(optDef);

					result.NumDefinitions = 1;
					result.Value = true;
					_resultsHandler[optDef] = result;
					break;

				case OptValType.IncrementalFlag:
					if (result == null)
						result = new OptionResult(optDef);

					result.NumDefinitions++;
					result.Value = result.NumDefinitions;
					_resultsHandler[optDef] = result;
					break;

				case OptValType.MultValue:
					value = GetValue(args, value, ref index);

					if (value == null)
						throw new ParseException(
							string.Format("Option {0} requires a value", optDef.ID));

					if (result == null)
						result = new OptionResult(optDef);

					result.NumDefinitions++;
					result.AddValue(optDef.ConvertValue(value));
					_resultsHandler[optDef] = result;
					break;

				case OptValType.ValueOpt:
					value = GetValue(args, value, ref index);

					if (result == null)
						result = new OptionResult(optDef);

					result.NumDefinitions = 1;
					result.Value = optDef.ConvertValue(value);
					_resultsHandler[optDef] = result;
					break;

				case OptValType.ValueReq:
					value = GetValue(args, value, ref index);

					if (value == null)
						throw new ParseException(
							string.Format("Option {0} requires a value", optDef.ID));
					
					if (result == null)
						result = new OptionResult(optDef);

					result.NumDefinitions = 1;
					result.Value = optDef.ConvertValue(value);
					_resultsHandler[optDef] = result;
					break;
			}
		}
Example #8
0
		/// <summary>
		/// Parse a long option
		/// </summary>
		/// <param name="args">All the arguments</param>
		/// <param name="option">The option name found</param>
		/// <param name="value">The value immediately following the option ([=:]value syntax)</param>
		/// <param name="index">The current index in the arguments which can be incremented
		/// by this function</param>
		/// <param name="dupOptHandleType">How to handle un-expected duplicate option
		/// definitions</param>
		/// <param name="unknownOptHandleType">How to handle options that were not
		/// defined</param>
		/// <param name="caseSesitive">True if parsing is case-sensitive</param>
		/// <param name="foundDefinitions">The option definitions already found</param>
		/// <param name="wasUnknown">If the option was unknown and the handle type was not
		/// error, this will be true so that the "option" can be added to the arguments</param>
		private void ParseLongOption(string[] args, string option, string value, bool caseSesitive, 
			ref int index, DupOptHandleType dupOptHandleType, 
			UnknownOptHandleType unknownOptHandleType, ArrayList foundDefinitions, out bool wasUnknown)
		{
			ParseOption(args, option, _resultsHandler.GetOptionDefinition(option, caseSesitive), 
				value, ref index, dupOptHandleType, unknownOptHandleType, foundDefinitions,
				out wasUnknown);
		}
Example #9
0
		/// <summary>
		/// Parse a concatinated list of short options
		/// </summary>
		/// <param name="args">All the arguments</param>
		/// <param name="options">The options found</param>
		/// <param name="index">The current index in the arguments which can be incremented
		/// by this function</param>
		/// <param name="dupOptHandleType">How to handle un-expected duplicate option
		/// definitions</param>
		/// <param name="unknownOptHandleType">How to handle options that were not
		/// defined</param>
		/// <param name="caseSesitive">True if parsing is case-sensitive</param>
		/// <param name="foundDefinitions">The option definitions already found</param>
		/// <param name="unknownChars">An array of all unknown option characters or null
		/// if all were known</param>
		private void ParseShortOptions(string[] args, char[] options, bool caseSesitive, ref int index,
			DupOptHandleType dupOptHandleType, UnknownOptHandleType unknownOptHandleType,
			ArrayList foundDefinitions, out char[] unknownChars)
		{
			ArrayList unknownCharsList = new ArrayList();
			bool      wasUnknown;

			foreach (char option in options)
			{
				ParseShortOption(args, option, null, caseSesitive, ref index, dupOptHandleType,
					unknownOptHandleType, foundDefinitions, out wasUnknown);

				if (wasUnknown)
					unknownCharsList.Add(option);
			}

			if (unknownCharsList.Count == 0)
				unknownChars = null;
			else
				unknownChars = (char[])unknownCharsList.ToArray(typeof(char));
		}
Example #10
0
		/// <summary>
		/// Parse the arguments for options using the given settings
		/// </summary>
		/// <param name="optStyle">What type of options to parse</param>
		/// <param name="unixShortOption">How to parse unix short options (ignored
		/// if not unix style parsing)</param>
		/// <param name="dupOptHandleType">How to handle un-expected duplicate option
		/// definitions</param>
		/// <param name="unknownOptHandleType">How to handle options that were not
		/// defined</param>
		/// <param name="caseSesitive">If the parsing of options 
		/// should consider the case of the options</param>
		/// <param name="args">The command-line arguments to parse</param>
		/// <returns>Arguments from the command-line that were not options</returns>
		public string[] Parse(OptStyle optStyle, UnixShortOption unixShortOption, 
			DupOptHandleType dupOptHandleType, UnknownOptHandleType unknownOptHandleType,
			bool caseSesitive, string[] args)
		{
			ArrayList        foundDefinitions = new ArrayList();
			Group            grp;
			Match            m;
			Regex            re;
			StringCollection arguments;
			bool             isShort;
			bool             unknown;
			string           name;
			string           value;
			string[]         results;

			if (args == null || args.Length == 0)
			{
				if (this.SearchEnvironment)
					ParseEnvironment(foundDefinitions, caseSesitive);
				return new string[0];
			}

			arguments = new StringCollection();
			
			re = BuildRegEx(optStyle, unixShortOption);

			for (int i = 0; i < args.Length; i++)
			{
				m = re.Match(args[i]);

				if (m.Success)
				{
					// see if there is a value
					grp = m.Groups["shortname"];

					if (grp != null && grp.Value != string.Empty)
					{
						isShort = true;
						name = grp.Value;
						value = m.Groups["shortvalue"].Value;
					}
					else
					{
						isShort = false;
						name = m.Groups["longname"].Value;
						value = m.Groups["longvalue"].Value;

						// remove the '=' or ':' at the beginning of the value
						if (value != null && value.Length > 0)
							value = value.Substring(1);
					}

					if (optStyle == OptStyle.Unix)
					{
						if (isShort == false)
							ParseLongOption(args, name, value, caseSesitive, 
								ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions,
								out unknown);
						// see if the value is just concatinated short options
						else if (unixShortOption == UnixShortOption.CollapseShort)
						{
							// name is the first option
							ParseShortOption(args, name[0], null, caseSesitive, 
								ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions,
								out unknown);

							// parse the rest of the options that lie in the 'value' variable
							if (value != null)
							{
								char[] unknownOptions;

								ParseShortOptions(args, value.ToLower().ToCharArray(), 
									caseSesitive, ref i, dupOptHandleType, unknownOptHandleType,
									foundDefinitions, out unknownOptions);

								// don't do anything with unknown concatinated short options,
								// they should not be considered options or arguments
							}
						}
						else
							// name is the first option
							ParseShortOption(args, name[0], value, caseSesitive,
								ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions,
								out unknown);
					}
					else
					{
						if (name.Length == 1)
							ParseShortOption(args, name[0], value, caseSesitive,
								ref i, dupOptHandleType, unknownOptHandleType, foundDefinitions,
								out unknown);
						else
							ParseLongOption(args, name, value, caseSesitive, ref i, 
								dupOptHandleType, unknownOptHandleType, foundDefinitions,
								out unknown);
					}

					// consider unknown options to be arguments
					if (unknown)
						arguments.Add(args[i]);
				}
				else
					arguments.Add(args[i]);
			}

			
			// parse the environment
			if (this.SearchEnvironment)
				ParseEnvironment(foundDefinitions, caseSesitive);

			results = new string[arguments.Count];
			arguments.CopyTo(results, 0);
			return results;
		}