Beispiel #1
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);
        }
Beispiel #2
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);
            }
        }
Beispiel #3
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));
        }