Пример #1
0
        public AttributeParseResult Parse(IEnumerable <string> args)
        {
            if (!_attributeHandler.TryParseSelectedVerb(Configuration.Verbs, ref args, out var error, out var verb, out var requestedHelp))
            {
                if (requestedHelp)
                {
                    if (verb == null)
                    {
                        return(ShowHelp());
                    }
                    else
                    {
                        return(ShowHelp(verb));
                    }
                }
                else if (error == null)
                {
                    throw new LogicException("The attribute handled didn't parse the result and no help was requested, but no error was generated.");
                }
                else
                {
                    return(AttributeParseResult.CreateErrorResult(verb, Configuration.Services.GetService <ErrorHandler>(), new[] { error }));
                }
            }

            IVerb safeVerb = verb ?? throw new LogicException("The attribute handler parsed the verb, but didn't return it.");

            if (!_attributeHandler.TryProcessArguments(safeVerb, Configuration.GetOptions(safeVerb), args, out var optionValues, out var errors))
            {
                return(AttributeParseResult.CreateErrorResult(verb, Configuration.Services.GetService <ErrorHandler>(), errors));
            }

            var verbData = Configuration.GetVerbData(safeVerb);

            foreach (var optionProperty in verbData.OptionProperties)
            {
                var providedOption = optionValues.FirstOrDefault((o) => o.Option == optionProperty.Option);
                if (providedOption == null)
                {
                    continue;
                }

                optionProperty.SetValue(verbData.Instance, providedOption.Value);
            }

            return(AttributeParseResult.CreateSuccessResult(safeVerb, Configuration.Services.GetService <ErrorHandler>(), verbData.Instance));
        }
Пример #2
0
        public AttributeParseResult Parse(IEnumerable <string> args)
        {
            if (!_attributeHandler.TryParseSelectedVerb(Configuration.Verbs, ref args, out var error, out var verb, out var requestedHelp))
            {
                if (requestedHelp)
                {
                    if (verb == null)
                    {
                        return(ShowHelp());
                    }
                    else
                    {
                        return(ShowHelp(verb));
                    }
                }
                else if (error == null)
                {
                    throw new LogicException("The attribute handled didn't parse the result and no help was requested, but no error was generated.");
                }
                else
                {
                    return(AttributeParseResult.CreateErrorResult(verb, Configuration.Services.GetService <ErrorHandler>(), new[] { error }));
                }
            }

            IVerb safeVerb = verb ?? throw new InvalidOperationException("The attribute handler parsed the verb, but didn't return it.");

            if (!_attributeHandler.TryProcessArguments(safeVerb, Configuration.GetOptions(safeVerb), args, out var optionValues, out var errors))
            {
                return(AttributeParseResult.CreateErrorResult(verb, Configuration.Services.GetService <ErrorHandler>(), errors));
            }

            var verbData = Configuration.GetVerbData(safeVerb);

            foreach (var optionValue in optionValues)
            {
                Configuration.GetParameterValueOption(optionValue.Option).SetValue(optionValue.Value);
            }

            return(AttributeParseResult.CreateSuccessResult(safeVerb, Configuration.Services.GetService <ErrorHandler>(), verbData.Method, verbData.Instance, verbData.Parameters.Select((x) => x.Value).ToArray()));
        }