Exemple #1
0
        /// <inheritdoc />
        protected override IParsingResult DoApplySwitch(CommandModel model, string[] switchArguments, IValueParsingOptions pOptions)
        {
            if (pOptions == null)
            {
                throw new ArgumentNullException(nameof(pOptions));
            }
            if (switchArguments.Length == 0)
            {
                throw new ArgumentException("Value cannot be an empty collection.", nameof(switchArguments));
            }

            try
            {
                string enumText = switchArguments[0];
                if (string.IsNullOrWhiteSpace(enumText))
                {
                    return(new ParsingFailure("Switch value cannot be empty."));
                }

                object enumValue;
                try
                {
                    enumValue = Enum.Parse(this._enumType, enumText, true); // TODO: is there a better way to parse generic enum?
                }
                catch (ArgumentException)
                {
                    return(new ParsingFailure($"'{enumText}' is not proper expected value from [{this._enumType.Name}] enumeration."));
                }

                this.TargetProperty.SetValue(model, enumValue);

                return(ParsingSuccess.Instance);
            }
            catch (Exception e)
            {
                return(new ParsingFailure(e.Message));
            }
        }
Exemple #2
0
        /// <inheritdoc />
        protected override void DoApplySwitch(CommandModel model, string[] switchArguments, IValueParsingOptions pOptions)
        {
            if (pOptions == null)
            {
                throw new ArgumentNullException(nameof(pOptions));
            }
            if (switchArguments.Length == 0)
            {
                throw new ArgumentException("Value cannot be an empty collection.", nameof(switchArguments));
            }
            string valueText = switchArguments[0];

            object value = this._converter.TryConvert(valueText, pOptions.UiCulture);

            this.TargetProperty.SetValue(model, value);
        }
        /// <inheritdoc />
        protected override void DoApplySwitch(CommandModel model, string[] switchArguments, IValueParsingOptions pOptions)
        {
            if (pOptions == null)
            {
                throw new ArgumentNullException(nameof(pOptions));
            }
            if (switchArguments.Length == 0)
            {
                throw new ArgumentException("Value cannot be an empty collection.", nameof(switchArguments));
            }
            string enumText = switchArguments[0];

            object enumValue = Enum.Parse(this._enumType, enumText, true); // might fail, TODO: Try finding something better than exception during parsing user input...

            this.TargetProperty.SetValue(model, enumValue);
        }
Exemple #4
0
 protected abstract IParsingResult DoApplySwitch(CommandModel model, string[] switchArguments, IValueParsingOptions pOptions);
        /// <inheritdoc />
        protected override IParsingResult DoApplySwitch(CommandModel model, string[] switchArguments, IValueParsingOptions pOptions)
        {
            if (pOptions == null)
            {
                throw new ArgumentNullException(nameof(pOptions));
            }
            if (switchArguments.Length == 0)
            {
                throw new ArgumentException("Value cannot be an empty collection.", nameof(switchArguments));
            }

            try
            {
                string valueText = switchArguments[0];

                object value = this._converter.TryConvert(valueText, pOptions.UiCulture);

                this.TargetProperty.SetValue(model, value);

                return(ParsingSuccess.Instance);
            }
            catch (Exception ex)
            {
                return(new ParsingFailure(ex.Message));
            }
        }