Beispiel #1
0
        protected virtual void SetValue(object optionObject, object key, string value, EventHandler <CommandOptionsSerializerEventArgs> errorEventHandler)
        {
            object convertedValue;
            CommandOptionsSerializerEventArgs eventArgs;

            var optionInfo = (OptionInfo)this.keyTypeMap[key];

            if (optionInfo == null)
            {
                if (key is int)
                {
                    eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.TooManyOptions, value);

                    if (errorEventHandler != null)
                    {
                        errorEventHandler(this, eventArgs);
                    }

                    this.OnError(eventArgs);

                    return;
                }
                else
                {
                    eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.UnknownOption, key.ToString(), value);

                    if (errorEventHandler != null)
                    {
                        errorEventHandler(this, eventArgs);
                    }

                    this.OnError(eventArgs);

                    return;
                }
            }

            if (optionInfo.ChoicesAttribute != null)
            {
                var error = true;

                foreach (string s in optionInfo.ChoicesAttribute.Choices)
                {
                    if (value.EqualsIgnoreCase(s))
                    {
                        error = false;
                        break;
                    }
                }

                if (error)
                {
                    eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.InvalidValue, optionInfo.Name, value);

                    if (errorEventHandler != null)
                    {
                        errorEventHandler(this, eventArgs);
                    }

                    this.OnError(eventArgs);
                }
            }

            try
            {
                convertedValue = Convert.ChangeType(value, optionInfo.Type);
            }
            catch (InvalidCastException)
            {
                eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.InvalidValue, optionInfo.Name, value);

                if (errorEventHandler != null)
                {
                    errorEventHandler(this, eventArgs);
                }

                this.OnError(eventArgs);

                return;
            }

            optionInfo.SetValue(optionObject, convertedValue);
        }
Beispiel #2
0
        public virtual object Parse(string commandLine, EventHandler <CommandOptionsSerializerEventArgs> errorEventHandler)
        {
            Group group;

            var unkeyedIndex = 0;
            var match        = regex.Match(commandLine);
            var retval       = Activator.CreateInstance(this.SupportedType);

            var optionsFound = System.Collections.Specialized.CollectionsUtil.CreateCaseInsensitiveHashtable();

            while (match.Success)
            {
                group = match.Groups["other"];

                var s = group.Value;

                if (group.Captures.Count > 0)
                {
                    if (group.Length > 0)
                    {
                        optionsFound[unkeyedIndex] = "";

                        this.SetValue(retval, unkeyedIndex, s, errorEventHandler);

                        match = match.NextMatch();

                        unkeyedIndex++;

                        continue;
                    }
                    else
                    {
                        match = match.NextMatch();

                        continue;
                    }
                }

                var key   = match.Groups["key"].Value;
                var value = match.Groups["value"].Value;

                OptionInfo optionInfo;

                if ((optionInfo = (OptionInfo)this.keyTypeMap[key]) != null)
                {
                    if (optionInfo.Type == typeof(bool))
                    {
                        if (!match.Groups["value"].Success)
                        {
                            value = "true";
                        }
                    }
                }

                this.SetValue(retval, key, value, errorEventHandler);

                optionsFound[key] = value;

                match = match.NextMatch();
            }

            foreach (OptionInfo optionInfo in this.keyTypeMap.Values)
            {
                if (optionInfo.Attribute.Required)
                {
                    if (!optionsFound.Contains(optionInfo.Key))
                    {
                        CommandOptionsSerializerEventArgs eventArgs;

                        eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.MissingOption, optionInfo.Name);

                        if (errorEventHandler != null)
                        {
                            errorEventHandler(this, eventArgs);
                        }

                        this.OnError(eventArgs);
                    }
                }
            }

            return(retval);
        }
Beispiel #3
0
 protected virtual void OnError(CommandOptionsSerializerEventArgs eventArgs)
 {
     this.Error?.Invoke(this, eventArgs);
 }
		protected virtual void SetValue(object optionObject, object key, string value, EventHandler<CommandOptionsSerializerEventArgs> errorEventHandler)
		{
			object convertedValue;
			CommandOptionsSerializerEventArgs eventArgs;

			var optionInfo = (OptionInfo)this.keyTypeMap[key];

			if (optionInfo == null)
			{
				if (key is int)
				{
					eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.TooManyOptions, value);

					if (errorEventHandler != null)
					{
						errorEventHandler(this, eventArgs);
					}

					this.OnError(eventArgs);

					return;
				}
				else
				{
					eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.UnknownOption, key.ToString(), value);

					if (errorEventHandler != null)
					{
						errorEventHandler(this, eventArgs);
					}

					this.OnError(eventArgs);

					return;
				}
			}

			if (optionInfo.ChoicesAttribute != null)
			{
				var error = true;

				foreach (string s in optionInfo.ChoicesAttribute.Choices)
				{
					if (value.EqualsIgnoreCase(s))
					{
						error = false;
						break;
					}
				}

				if (error)
				{
					eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.InvalidValue, optionInfo.Name, value);

					if (errorEventHandler != null)
					{
						errorEventHandler(this, eventArgs);
					}

					this.OnError(eventArgs);
				}
			}

			try
			{
				convertedValue = Convert.ChangeType(value, optionInfo.Type);
			}
			catch (InvalidCastException)
			{
				eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.InvalidValue, optionInfo.Name, value);

				if (errorEventHandler != null)
				{
					errorEventHandler(this, eventArgs);
				}

				this.OnError(eventArgs);

				return;
			}

			optionInfo.SetValue(optionObject, convertedValue);
		}
		protected virtual void OnError(CommandOptionsSerializerEventArgs eventArgs)
		{
			this.Error?.Invoke(this, eventArgs);
		}
		public virtual object Parse(string commandLine, EventHandler<CommandOptionsSerializerEventArgs> errorEventHandler)
		{
			Group group;

			var unkeyedIndex = 0;
			var match = regex.Match(commandLine);
			var retval = Activator.CreateInstance(this.SupportedType);

			var optionsFound = System.Collections.Specialized.CollectionsUtil.CreateCaseInsensitiveHashtable();

			while (match.Success)
			{
				group = match.Groups["other"];

				var s = group.Value;

				if (group.Captures.Count > 0)
				{
					if (group.Length > 0)
					{
						optionsFound[unkeyedIndex] = "";

						this.SetValue(retval, unkeyedIndex, s, errorEventHandler);

						match = match.NextMatch();

						unkeyedIndex++;

						continue;
					}
					else
					{
						match = match.NextMatch();

						continue;
					}
				}

				var key = match.Groups["key"].Value;
				var value = match.Groups["value"].Value;

				OptionInfo optionInfo;

				if ((optionInfo = (OptionInfo)this.keyTypeMap[key]) != null)
				{
					if (optionInfo.Type == typeof(bool))
					{
						if (!match.Groups["value"].Success)
						{
							value = "true";
						}
					}
				}

				this.SetValue(retval, key, value, errorEventHandler);

				optionsFound[key] = value;

				match = match.NextMatch();
			}

			foreach (OptionInfo optionInfo in this.keyTypeMap.Values)
			{
				if (optionInfo.Attribute.Required)
				{
					if (!optionsFound.Contains(optionInfo.Key))
					{
						CommandOptionsSerializerEventArgs eventArgs;

						eventArgs = new CommandOptionsSerializerEventArgs(CommandLineError.MissingOption, optionInfo.Name);

						if (errorEventHandler != null)
						{
							errorEventHandler(this, eventArgs);
						}

						this.OnError(eventArgs);
					}
				}
			}

			return retval;
		}