Example #1
0
        public void AddModule(Module module, IReadOnlyList <string> segments, int startIndex)
        {
            if (segments.Count == 0)
            {
                return;
            }

            var segment = segments[startIndex];

            if (startIndex == segments.Count - 1)
            {
                if (_modules.TryGetValue(segment, out var modules))
                {
                    modules.Add(module);
                }

                else
                {
                    _modules.Add(segment, new List <Module> {
                        module
                    });
                }
            }

            else
            {
                if (!_nodes.TryGetValue(segment, out var node))
                {
                    node = new CommandNode(_service);
                    _nodes.Add(segment, node);
                }

                node.AddModule(module, segments, startIndex + 1);
            }
        }
Example #2
0
        public void AddCommand(Command command, IReadOnlyList <string> segments, int startIndex)
        {
            if (segments.Count == 0)
            {
                throw new InvalidOperationException("Cannot add commands to the root node.");
            }

            var segment = segments[startIndex];

            if (startIndex == segments.Count - 1)
            {
                if (_commands.TryGetValue(segment, out var commands))
                {
                    commands.Add(command);
                }

                else
                {
                    _commands.Add(segment, new List <Command> {
                        command
                    });
                }
            }

            else
            {
                if (!_nodes.TryGetValue(segment, out var node))
                {
                    node = new CommandNode(_service);
                    _nodes.Add(segment, node);
                }

                node.AddCommand(command, segments, startIndex + 1);
            }
        }
Example #3
0
        public void AddCommand(Command command, IReadOnlyList <string> segments, int startIndex)
        {
            if (segments.Count == 0)
            {
                throw new CommandMappingException(command, null, "Cannot map commands without aliases to the root node.");
            }

            var segment = segments[startIndex];

            if (startIndex == segments.Count - 1)
            {
                if (_commands.TryGetValue(segment, out var commands))
                {
                    for (var i = 0; i < commands.Count; i++)
                    {
                        var otherCommand   = commands[i];
                        var signature      = command.SignatureIdentifier;
                        var otherSignature = otherCommand.SignatureIdentifier;
                        if (signature.Identifier == otherSignature.Identifier)
                        {
                            if (signature.HasRemainder == otherSignature.HasRemainder)
                            {
                                throw new CommandMappingException(command, segment, "Cannot map multiple overloads with the same signature.");
                            }

                            else if (!signature.HasRemainder && command.IgnoreExtraArguments || !otherSignature.HasRemainder && otherCommand.IgnoreExtraArguments)
                            {
                                throw new CommandMappingException(command, segment, "Cannot map multiple overloads with the same argument types, with one of them being a remainder, if the other one ignores extra arguments.");
                            }
                        }
                    }

                    commands.Add(command);
                }

                else
                {
                    _commands.Add(segment, new List <Command> {
                        command
                    });
                }
            }

            else
            {
                if (!_nodes.TryGetValue(segment, out var node))
                {
                    node = new CommandNode(_service);
                    _nodes.Add(segment, node);
                }

                node.AddCommand(command, segments, startIndex + 1);
            }
        }