Ejemplo n.º 1
0
        private static ArgumentDefinition CreateArgumentDescriptor(
            IMutableMemberInfo member,
            ArgumentBaseAttribute attribute,
            object defaultValues,
            ArgumentSetDefinition argSet,
            object fixedDestination,
            ArgumentDefinition containingArgument,
            ServiceConfigurer serviceConfigurer)
        {
            if (!member.IsReadable || !member.IsWritable)
            {
                var declaringType = member.MemberInfo.DeclaringType;

                throw new InvalidArgumentSetException(member, string.Format(
                                                          CultureInfo.CurrentCulture,
                                                          Strings.MemberNotSupported,
                                                          member.MemberInfo.Name,
                                                          declaringType?.Name));
            }

            var defaultFieldValue = (defaultValues != null) ? member.GetValue(defaultValues) : null;

            return(new ArgumentDefinition(member,
                                          attribute,
                                          argSet,
                                          defaultValue: defaultFieldValue,
                                          fixedDestination: fixedDestination,
                                          containingArgument: containingArgument,
                                          serviceConfigurer: serviceConfigurer));
        }
Ejemplo n.º 2
0
 protected override ServiceConfiguration Service(ServiceConfigurer configure)
 {
     return(configure
            .MaxResultLengthInBytes(80 * 1024 * 1024)
            .RequestTimeout(TimeSpan.FromMinutes(45))
            .Localhost(32805)
            .Routine());
 }
Ejemplo n.º 3
0
 public ArgumentDefinition(MemberInfo member,
                           ArgumentBaseAttribute attribute,
                           ArgumentSetDefinition argSet,
                           object defaultValue = null,
                           ArgumentDefinition containingArgument = null,
                           ServiceConfigurer serviceConfigurer   = null)
     : this(GetMutableMemberInfo(member), attribute, argSet, defaultValue, /*fixedDestination=*/ null, containingArgument, serviceConfigurer)
 {
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Internal constructor.
        /// </summary>
        /// <param name="member">Field to describe.</param>
        /// <param name="attribute">Argument attribute on the field.</param>
        /// <param name="argSet">Argument set containing this argument.</param>
        /// <param name="defaultValue">Default value for the field.</param>
        /// <param name="fixedDestination">Optionally provides fixed parse destination object.</param>
        /// <param name="containingArgument">Optionally provides a reference
        /// to the definition of the argument that "contains" these arguments.
        /// </param>
        /// <param name="serviceConfigurer">Optionally provides a service configurer.</param>
        internal ArgumentDefinition(IMutableMemberInfo member,
                                    ArgumentBaseAttribute attribute,
                                    ArgumentSetDefinition argSet,
                                    object defaultValue     = null,
                                    object fixedDestination = null,
                                    ArgumentDefinition containingArgument = null,
                                    ServiceConfigurer serviceConfigurer   = null)
        {
            Member                 = member ?? throw new ArgumentNullException(nameof(member));
            Attribute              = attribute ?? throw new ArgumentNullException(nameof(attribute));
            ContainingSet          = argSet ?? throw new ArgumentNullException(nameof(argSet));
            ContainingArgument     = containingArgument;
            FixedDestination       = fixedDestination;
            IsPositional           = attribute is PositionalArgumentAttribute;
            ArgumentType           = GetArgumentType(Attribute, member, member.MemberType, serviceConfigurer);
            CollectionArgumentType = AsCollectionType(ArgumentType);
            HasDefaultValue        = attribute.ExplicitDefaultValue || attribute.DynamicDefaultValue;
            ValidationAttributes   = GetValidationAttributes(ArgumentType, Member);

            LongName          = GetLongName(attribute, argSet.Attribute, member.MemberInfo);
            ExplicitShortName = HasExplicitShortName(attribute);
            ShortName         = GetShortNameOrNull(attribute, argSet.Attribute, member.MemberInfo);
            DefaultValue      = GetDefaultValue(attribute, member, defaultValue);

            var nullableBase = Nullable.GetUnderlyingType(member.MemberType);

            if (CollectionArgumentType != null)
            {
                ValueType = CollectionArgumentType.ElementType;
            }
            else if (nullableBase != null)
            {
                // For nullable arguments, we use the wrapped type (T in
                // Nullable<T>) as the value type. Parsing an enum or int is the
                // same as parsing an enum? or int?, for example, since null can
                // only arise if the value was not provided at all.
                ValueType = GetArgumentType(Attribute, member, nullableBase, serviceConfigurer);
            }
            else
            {
                ValueType = ArgumentType;
            }

            Debug.Assert(ValueType != null);

            if (Unique && !IsCollection)
            {
                throw new InvalidArgumentSetException(member, Strings.UniqueUsedOnNonCollectionArgument);
            }

            Debug.Assert(!string.IsNullOrEmpty(LongName));
        }
Ejemplo n.º 5
0
        public static ColoredMultistring GetUsageInfo(
            Type type,
            ArgumentSetHelpOptions options      = null,
            object defaultValues                = null,
            ServiceConfigurer serviceConfigurer = null)
        {
            var argSet = AttributeBasedArgumentDefinitionFactory.CreateArgumentSet(
                type,
                attribute: null,
                defaultValues: defaultValues,
                serviceConfigurer: serviceConfigurer);

            return(GetUsageInfo(argSet, options, null));
        }
Ejemplo n.º 6
0
        private void ConfigureServices(IServiceCollection services, ServiceConfigurer inputConfigurer)
        {
            // Apply incoming configuration.
            inputConfigurer?.Invoke(services);

            // Register LoopOptions.
            services.AddSingleton(_options);

            // Register CommandLineParserOptions.
            services.AddSingleton(_options.ParserOptions);

            // Register ArgumentSetAttribute.
            services.AddSingleton(_argSet.Attribute);

            // Register Loop.
            services.AddSingleton(this);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Instantiate the command.
        /// </summary>
        /// <param name="serviceConfigurer">Service configurer.</param>
        /// <returns>Instantiated command object.</returns>
        internal ICommand Instantiate(ServiceConfigurer serviceConfigurer)
        {
            var services = new ServiceCollection();

            serviceConfigurer?.Invoke(services);

            services.AddTransient(typeof(ICommand), ImplementingType);

            var provider = services.BuildServiceProvider();

            try
            {
                return(provider.GetService <ICommand>());
            }
            catch (InvalidOperationException ex)
            {
                throw new InvalidCommandException($"No matching command constructor could be found on type '{ImplementingType.FullName}'.", ex);
            }
        }
Ejemplo n.º 8
0
        public static ArgumentSetDefinition CreateArgumentSet(
            Type typeToReflectOn,
            ArgumentSetAttribute attribute      = null,
            object defaultValues                = null,
            object fixedDestination             = null,
            ServiceConfigurer serviceConfigurer = null)
        {
            // Find high-level metadata for the argument set.
            var argSetAttrib = attribute ?? GetSetAttributeOrDefault(typeToReflectOn);

            // Construct an empty definition.
            var argSet = new ArgumentSetDefinition(argSetAttrib);

            // Add arguments.
            AddToArgumentSet(
                argSet,
                typeToReflectOn,
                defaultValues: defaultValues,
                fixedDestination: fixedDestination,
                serviceConfigurer: serviceConfigurer);

            return(argSet);
        }
Ejemplo n.º 9
0
        public static void AddToArgumentSet(
            ArgumentSetDefinition argSet,
            Type typeToReflectOn,
            object defaultValues    = null,
            object fixedDestination = null,
            ArgumentDefinition containingArgument = null,
            ServiceConfigurer serviceConfigurer   = null)
        {
            // Extract argument descriptors from the defining type.
            var args = GetArgumentDescriptors(
                typeToReflectOn,
                argSet,
                defaultValues,
                fixedDestination,
                containingArgument,
                serviceConfigurer).ToList();

            // Define the arguments.
            argSet.Add(args);

            // If the provided type we're reflecting on has an ArgumentSetAttribute,
            // then add that as auxiliary information.
            var auxiliaryAttrib = TryGetSetAttribute(typeToReflectOn);

            if (auxiliaryAttrib != null)
            {
                argSet.AddAuxiliaryAttribute(auxiliaryAttrib);
            }

            // If the argument set doesn't already have a default assembly associated
            // with it, then fill that out.
            if (argSet.DefaultAssembly == null)
            {
                argSet.DefaultAssembly = typeToReflectOn.GetTypeInfo().Assembly;
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Retrieves the <see cref="IArgumentType"/> type for the provided type.
        /// </summary>
        /// <param name="attrib">The argument attribute to use.</param>
        /// <param name="memberInfo">Member info for the argument.</param>
        /// <param name="type">The type to look up.</param>
        /// <param name="configurer">Optionally provides service configurer.</param>
        /// <returns>The found type.</returns>
        private static IArgumentType GetArgumentType(ArgumentBaseAttribute attrib, IMutableMemberInfo memberInfo, Type type, ServiceConfigurer configurer)
        {
            // First try to retrieve the default IArgumentType implementation
            // for this type, but don't fail if we can't find one.
            Types.ArgumentType.TryGetType(type, out IArgumentType argType);

            // If we don't have any overrides, then we already have the
            // implementation we'll need (or it doesn't exist).
            if (attrib.ArgumentType == null && attrib.Parser == null && attrib.Formatter == null && attrib.Completer == null)
            {
                if (argType == null)
                {
                    throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Strings.TypeNotSupported, type.Name));
                }

                return(argType);
            }

            var serviceCollection = new ServiceCollection();

            configurer?.Invoke(serviceCollection);

            serviceCollection.AddSingleton <Type>(type);
            serviceCollection.AddSingleton <IMutableMemberInfo>(memberInfo);

            if (attrib.Parser != null)
            {
                serviceCollection.AddTransient(typeof(IStringParser), attrib.Parser);
            }

            if (attrib.Formatter != null)
            {
                serviceCollection.AddTransient(typeof(IObjectFormatter), attrib.Formatter);
            }

            if (attrib.Completer != null)
            {
                serviceCollection.AddTransient(typeof(IStringCompleter), attrib.Completer);
            }

            if (attrib.ArgumentType != null)
            {
                if (argType != null)
                {
                    serviceCollection.AddSingleton <IArgumentType>(argType);
                }

                serviceCollection.AddTransient(typeof(object), attrib.ArgumentType);

                argType = (IArgumentType)serviceCollection
                          .BuildServiceProvider()
                          .GetService <object>();
            }

            if (argType == null)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Strings.TypeNotSupported, type.Name));
            }

            serviceCollection.AddSingleton <IArgumentType>(argType);

            if (attrib.Parser == null && attrib.Formatter == null && attrib.Completer == null)
            {
                return(argType);
            }

            var provider = serviceCollection.BuildServiceProvider();

            var parser    = provider.GetService <IStringParser>();
            var formatter = provider.GetService <IObjectFormatter>();
            var completer = provider.GetService <IStringCompleter>();

            return(new ArgumentTypeExtension(argType, parser, formatter, completer));
        }
Ejemplo n.º 11
0
        private static IEnumerable <ArgumentDefinition> CreateArgumentDescriptorsIfApplicable(IMutableMemberInfo member, object defaultValues,
                                                                                              ArgumentSetDefinition argSet, object fixedDestination, ArgumentDefinition containingArgument, ServiceConfigurer serviceConfigurer)
        {
            var descriptors = Enumerable.Empty <ArgumentDefinition>();

            var argAttrib = member.MemberInfo.GetSingleAttribute <ArgumentBaseAttribute>();

            if (argAttrib != null)
            {
                descriptors = descriptors.Concat(
                    new[]
                {
                    CreateArgumentDescriptor(
                        member,
                        argAttrib,
                        defaultValues,
                        argSet,
                        fixedDestination,
                        containingArgument,
                        serviceConfigurer)
                });
            }

            return(descriptors);
        }
Ejemplo n.º 12
0
        private static IEnumerable <ArgumentDefinition> GetArgumentDescriptors(Type type, ArgumentSetDefinition argSet, object defaultValues, object fixedDestination, ArgumentDefinition containingArgument, ServiceConfigurer serviceConfigurer)
        {
            // Find all fields and properties that have argument attributes on
            // them. For each that we find, capture information about them.
            var argList = GetAllFieldsAndProperties(type, includeNonPublicMembers: true)
                          .SelectMany(member => CreateArgumentDescriptorsIfApplicable(member, defaultValues, argSet, fixedDestination, containingArgument, serviceConfigurer));

            // If the argument set attribute indicates that we should also
            // include un-attributed, public, writable members as named
            // arguments, then look for them now.
            if (argSet.Attribute.PublicMembersAreNamedArguments)
            {
                argList = argList.Concat(GetAllFieldsAndProperties(type, includeNonPublicMembers: false)
                                         .Where(member => member.IsWritable)
                                         .Where(member => member.MemberInfo.GetSingleAttribute <ArgumentBaseAttribute>() == null)
                                         .Select(member => CreateArgumentDescriptor(
                                                     member,
                                                     new NamedArgumentAttribute(),
                                                     defaultValues,
                                                     argSet,
                                                     fixedDestination,
                                                     containingArgument,
                                                     serviceConfigurer)));
            }

            return(argList);
        }
        public static FluentBuilder <CommandLineParserOptions> ConfigureServices(this FluentBuilder <CommandLineParserOptions> builder, ServiceConfigurer configurer)
        {
            if (configurer == null)
            {
                throw new ArgumentNullException(nameof(configurer));
            }

            builder.AddTransformer(options => options.ServiceConfigurer = configurer);
            return(builder);
        }