Ejemplo n.º 1
0
        public Argument <T> DefineOption <T>(string name, T defaultValue, Func <string, T> valueConverter, bool isRequired)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(Strings.NameMissing, "name");
            }

            if (DefinedParameters.Any())
            {
                throw new InvalidOperationException(Strings.OptionsMustBeDefinedBeforeParameters);
            }

            var names  = ParseOptionNameList(name);
            var option = new Argument <T>(_definedCommand, names, defaultValue, isRequired);

            _options.Add(option);

            if (_activeCommand != _definedCommand)
            {
                return(option);
            }

            try
            {
                if (Parser.TryParseOption(option.GetDisplayName(), option.Names, valueConverter, isRequired, out T value, out bool specified))
                {
                    option.SetValue(value);
                }
                else if (specified)
                {
                    // No value was provided, but the option was specified and a value wasn't required
                    option.MarkSpecified();
                }
            }
Ejemplo n.º 2
0
        public Argument <T> DefineParameter <T>(string name, T defaultValue, Func <string, T> valueConverter)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(Strings.NameMissing, "name");
            }

            if (!IsValidName(name))
            {
                var message = string.Format(Strings.ParameterNameIsNotValidFmt, name);
                throw new ArgumentException(message, "name");
            }

            if (DefinedParameters.Any(p => p.IsList))
            {
                throw new InvalidOperationException(Strings.ParametersCannotBeDefinedAfterLists);
            }

            if (DefinedParameters.Any(p => string.Equals(name, p.Name, StringComparison.OrdinalIgnoreCase)))
            {
                var message = string.Format(Strings.ParameterAlreadyDefinedFmt, name);
                throw new InvalidOperationException(message);
            }

            var parameter = new Argument <T>(_definedCommand, name, defaultValue);

            _parameters.Add(parameter);

            if (_activeCommand != _definedCommand)
            {
                return(parameter);
            }

            try
            {
                T value;
                if (Parser.TryParseParameter(parameter.GetDisplayName(), valueConverter, out value))
                {
                    parameter.SetValue(value);
                }
            }
            catch (ArgumentSyntaxException ex)
            {
                ReportError(ex.Message);
            }

            return(parameter);
        }
Ejemplo n.º 3
0
        public ArgumentList <T> DefineOptionList <T>(string name, IReadOnlyList <T> defaultValue, Func <string, T> valueConverter, bool isRequired)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(Strings.NameMissing, "name");
            }

            if (DefinedParameters.Any())
            {
                throw new InvalidOperationException(Strings.OptionsMustBeDefinedBeforeParameters);
            }

            var names      = ParseOptionNameList(name);
            var optionList = new ArgumentList <T>(_definedCommand, names, defaultValue, isRequired);

            _options.Add(optionList);

            if (_activeCommand != _definedCommand)
            {
                return(optionList);
            }

            try
            {
                IReadOnlyList <T> value;
                bool specified;
                if (Parser.TryParseOptionList(optionList.GetDisplayName(), optionList.Names, valueConverter, isRequired, out value, out specified))
                {
                    optionList.SetValue(value);
                }
                else if (specified)
                {
                    // No value was provided, but the option was specified and a value wasn't required
                    optionList.MarkSpecified();
                }
            }
            catch (ArgumentSyntaxException ex)
            {
                ReportError(ex.Message);
            }

            return(optionList);
        }
Ejemplo n.º 4
0
        public ArgumentList <T> DefineParameterList <T>(string name, IReadOnlyList <T> defaultValue, Func <string, T> valueConverter)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(Strings.NameMissing, "name");
            }

            if (!IsValidName(name))
            {
                var message = string.Format(Strings.ParameterNameIsNotValidFmt, name);
                throw new ArgumentException(message, "name");
            }

            if (DefinedParameters.Any(p => p.IsList))
            {
                throw new InvalidOperationException(Strings.CannotDefineMultipleParameterLists);
            }

            var parameterList = new ArgumentList <T>(_definedCommand, name, defaultValue);

            _parameters.Add(parameterList);

            if (_activeCommand != _definedCommand)
            {
                return(parameterList);
            }

            try
            {
                IReadOnlyList <T> values;
                if (Parser.TryParseParameterList(parameterList.GetDisplayName(), valueConverter, out values))
                {
                    parameterList.SetValue(values);
                }
            }
            catch (ArgumentSyntaxException ex)
            {
                ReportError(ex.Message);
            }

            return(parameterList);
        }
Ejemplo n.º 5
0
        public ArgumentList <T> DefineOptionList <T>(string name, IReadOnlyList <T> defaultValue, Func <string, T> valueConverter)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(Strings.NameMissing, "name");
            }

            if (DefinedParameters.Any())
            {
                throw new InvalidOperationException(Strings.OptionsMustBeDefinedBeforeParameters);
            }

            var names      = ParseOptionNameList(name);
            var optionList = new ArgumentList <T>(_definedCommand, names, defaultValue);

            _options.Add(optionList);

            if (_activeCommand != _definedCommand)
            {
                return(optionList);
            }

            try
            {
                IReadOnlyList <T> value;
                if (Parser.TryParseOptionList(optionList.GetDisplayName(), optionList.Names, valueConverter, out value))
                {
                    optionList.SetValue(value);
                }
            }
            catch (ArgumentSyntaxException ex)
            {
                ReportError(ex.Message);
            }

            return(optionList);
        }