コード例 #1
0
        private static CommandInfo Build(CommandInfo?parent, ConfiguredCommand command)
        {
            var info = new CommandInfo(parent, command);

            foreach (var parameter in GetParameters(info))
            {
                info.Parameters.Add(parameter);
            }

            foreach (var childCommand in command.Children)
            {
                var child = Build(info, childCommand);
                info.Children.Add(child);
            }

            // Normalize argument positions.
            var index = 0;

            foreach (var argument in info.Parameters.OfType <CommandArgument>()
                     .OrderBy(argument => argument.Position))
            {
                argument.Position = index;
                index++;
            }

            return(info);
        }
コード例 #2
0
        public CommandInfo(CommandInfo?parent, ConfiguredCommand prototype)
        {
            Parent = parent;

            Name             = prototype.Name;
            Aliases          = new HashSet <string>(prototype.Aliases);
            Description      = prototype.Description;
            Data             = prototype.Data;
            CommandType      = prototype.CommandType;
            SettingsType     = prototype.SettingsType;
            Delegate         = prototype.Delegate;
            IsDefaultCommand = prototype.IsDefaultCommand;
            IsHidden         = prototype.IsHidden;

            Children   = new List <CommandInfo>();
            Parameters = new List <CommandParameter>();
            Examples   = prototype.Examples ?? new List <string[]>();

            if (CommandType != null && string.IsNullOrWhiteSpace(Description))
            {
                var description = CommandType.GetCustomAttribute <DescriptionAttribute>();
                if (description != null)
                {
                    Description = description.Description;
                }
            }
        }
コード例 #3
0
        public ICommandConfigurator AddCommand <TCommand>(string name)
            where TCommand : class, ICommand
        {
            var command = Commands.AddAndReturn(ConfiguredCommand.FromType <TCommand>(name, false));

            return(new CommandConfigurator(command));
        }
コード例 #4
0
        public void AddBranch <TDerivedSettings>(string name, Action <IConfigurator <TDerivedSettings> > action)
            where TDerivedSettings : TSettings
        {
            var command = ConfiguredCommand.FromBranch <TDerivedSettings>(name);

            action(new Configurator <TDerivedSettings>(command, _registrar));
            _command.Children.Add(command);
        }
コード例 #5
0
        public void AddBranch <TSettings>(string name, Action <IConfigurator <TSettings> > action)
            where TSettings : CommandSettings
        {
            var command = ConfiguredCommand.FromBranch <TSettings>(name);

            action(new Configurator <TSettings>(command, _registrar));
            Commands.Add(command);
        }
コード例 #6
0
        public ICommandConfigurator AddDelegate <TSettings>(string name, Func <CommandContext, TSettings, int> func)
            where TSettings : CommandSettings
        {
            var command = Commands.AddAndReturn(ConfiguredCommand.FromDelegate <TSettings>(
                                                    name, (context, settings) => func(context, (TSettings)settings)));

            return(new CommandConfigurator(command));
        }
コード例 #7
0
        public ICommandConfigurator AddDelegate <TDerivedSettings>(string name, Func <CommandContext, TDerivedSettings, int> func)
            where TDerivedSettings : TSettings
        {
            var command = ConfiguredCommand.FromDelegate <TDerivedSettings>(
                name, (context, settings) => func(context, (TDerivedSettings)settings));

            _command.Children.Add(command);
            return(new CommandConfigurator(command));
        }
コード例 #8
0
        public ICommandConfigurator AddCommand <TCommand>(string name)
            where TCommand : class, ICommandLimiter <TSettings>
        {
            var command      = ConfiguredCommand.FromType <TCommand>(name);
            var configurator = new CommandConfigurator(command);

            _command.Children.Add(command);
            return(configurator);
        }
コード例 #9
0
        void IUnsafeConfigurator.AddBranch(string name, Type settings, Action <IUnsafeBranchConfigurator> action)
        {
            var command = ConfiguredCommand.FromBranch(settings, name);

            // Create the configurator.
            var configuratorType = typeof(Configurator <>).MakeGenericType(settings);

            if (!(Activator.CreateInstance(configuratorType, new object?[] { command, _registrar }) is IUnsafeBranchConfigurator configurator))
            {
                throw new CommandConfigurationException("Could not create configurator by reflection.");
            }

            action(configurator);
            _command.Children.Add(command);
        }
コード例 #10
0
 public Configurator(ConfiguredCommand command, ITypeRegistrar?registrar)
 {
     _command   = command;
     _registrar = registrar;
 }
コード例 #11
0
 public void SetDefaultCommand <TDefaultCommand>()
     where TDefaultCommand : class, ICommand
 {
     DefaultCommand = ConfiguredCommand.FromType <TDefaultCommand>(
         Constants.DefaultCommandName, isDefaultCommand: true);
 }