private void CollectCommands(CommandNode parentNode, IEnumerable <ICommand> commands)
 {
     foreach (var item in commands)
     {
         CollectCommands(parentNode, item);
     }
 }
        private void InitializeCommand(CommandNode commandNode)
        {
            var query = from item in commandNode.CommandList
                        where item is ICommandHost commandHost
                        select item as ICommandHost;

            foreach (var item in commandNode.Childs)
            {
                this.InitializeCommand(item);
            }
        }
 protected CommandContextBase(string name, IEnumerable <ICommand> commands)
 {
     if (name == string.Empty)
     {
         throw new ArgumentException(Resources.Exception_EmptyStringsAreNotAllowed);
     }
     this.Name           = name ?? throw new ArgumentNullException(nameof(name));
     this.fullName       = name;
     this.filename       = name;
     this.HelpCommand    = commands.SingleOrDefault(item => item.GetType().GetCustomAttribute <HelpCommandAttribute>() != null) ?? new HelpCommand();
     this.VersionCommand = commands.SingleOrDefault(item => item.GetType().GetCustomAttribute <VersionCommandAttribute>() != null) ?? new VersionCommand();
     this.commandNode    = new CommandNode(this);
     this.Initialize(this.commandNode, commands);
 }
 protected CommandContextBase(Assembly assembly, IEnumerable <ICommand> commands)
 {
     if (assembly == null)
     {
         throw new ArgumentNullException(nameof(assembly));
     }
     this.Name           = Path.GetFileNameWithoutExtension(assembly.Location);
     this.filename       = Path.GetFileName(assembly.Location);
     this.fullName       = assembly.Location;
     this.versionInfo    = FileVersionInfo.GetVersionInfo(assembly.Location);
     this.Version        = this.versionInfo.ProductVersion;
     this.HelpCommand    = commands.SingleOrDefault(item => item.GetType().GetCustomAttribute <HelpCommandAttribute>() != null) ?? new HelpCommand();
     this.VersionCommand = commands.SingleOrDefault(item => item.GetType().GetCustomAttribute <VersionCommandAttribute>() != null) ?? new VersionCommand();
     this.commandNode    = new CommandNode(this);
     this.Initialize(this.commandNode, commands);
 }
        private void CollectCommands(CommandNode parentNode, ICommand command)
        {
            var commandName = command.Name;
            var partialAttr = command.GetType().GetCustomAttribute <PartialCommandAttribute>();

            if (parentNode.Childs.ContainsKey(commandName) == true && partialAttr == null)
            {
                throw new InvalidOperationException(string.Format(Resources.Exception_CommandAlreadyExists_Format, commandName));
            }
            if (parentNode.Childs.ContainsKey(commandName) == false && partialAttr != null)
            {
                throw new InvalidOperationException(string.Format(Resources.Exception_CommandDoesNotExists_Format, commandName));
            }
            if (partialAttr != null && command.Aliases.Any() == true)
            {
                throw new InvalidOperationException($"Partial command cannot have alias.: '{commandName}'");
            }
            if (parentNode.Childs.ContainsKey(commandName) == false)
            {
                var commandNode = new CommandNode(this)
                {
                    Parent  = parentNode,
                    Name    = commandName,
                    Command = command
                };
                parentNode.Childs.Add(commandNode);
                foreach (var item in command.Aliases)
                {
                    parentNode.ChildsByAlias.Add(new CommandAliasNode(commandNode, item));
                }
            }
            {
                var commandNode = parentNode.Childs[commandName];
                commandNode.CommandList.Add(command);
                if (command is ICommandHost commandHost)
                {
                    commandHost.Node = commandNode;
                }
                if (command is ICommandHierarchy hierarchy)
                {
                    this.CollectCommands(commandNode, hierarchy.Commands);
                }
            }
        }
 private string[] GetCompletion(ICommandNode parentNode, IList <string> itemList, string find)
 {
     if (itemList.Count == 0)
     {
         var query = from item in parentNode.Childs
                     where item.IsEnabled
                     from name in new string[] { item.Name }.Concat(item.Aliases)
         where name.StartsWith(find)
         orderby name
         select name;
         return(query.ToArray());
     }
     else
     {
         var commandName = itemList.First();
         var commandNode = parentNode.Childs[commandName];
         if (commandNode == null)
         {
             commandNode = parentNode.ChildsByAlias[commandName];
             if (commandNode == null)
             {
                 return(null);
             }
         }
         if (commandNode.IsEnabled == true && commandNode.Childs.Any() == true)
         {
             itemList.RemoveAt(0);
             return(this.GetCompletion(commandNode, itemList, find));
         }
         else
         {
             var args = itemList.Skip(1).ToArray();
             foreach (var item in commandNode.Commands)
             {
                 if (this.GetCompletion(item, args, find) is string[] completions)
                 {
                     return(completions);
                 }
             }
         }
         return(null);
     }
 }
 private void Initialize(CommandNode commandNode, IEnumerable <ICommand> commands)
 {
     this.CollectCommands(commandNode, this.ValidateCommands(commands));
     this.InitializeCommand(commandNode);
 }