Exemplo n.º 1
0
 internal static void KeyIsNotNullOrEmpty <TRunInfo>(StackableCommand <TRunInfo> command,
                                                     int commandLevel) where TRunInfo : class
 {
     if (string.IsNullOrWhiteSpace(command.Key))
     {
         throw new CommandValidationException("Command key must be provided.",
                                              CommandValidationError.KeyNotProvided, commandLevel);
     }
 }
Exemplo n.º 2
0
            internal static void SubCommandKeysMustBeUnique <TRunInfo>(StackableCommand <TRunInfo> command,
                                                                       int commandLevel) where TRunInfo : class
            {
                bool hasDuplicate = command.SubCommands.Count != command.SubCommands.Select(c => c.Key).Distinct().Count();

                if (hasDuplicate)
                {
                    throw new CommandValidationException("Command key is invalid because "
                                                         + "it clashes with an already configured key.",
                                                         CommandValidationError.DuplicateKey, commandLevel);
                }
            }
Exemplo n.º 3
0
            internal static void SubCommandsCannotBeNull <TRunInfo>(StackableCommand <TRunInfo> command,
                                                                    int commandLevel) where TRunInfo : class
            {
                int nullIndex = command.SubCommands.IndexOfFirstNull();

                if (nullIndex != -1)
                {
                    throw new CommandValidationException(
                              $"Command contains a null subcommand (index {nullIndex}).",
                              CommandValidationError.NullObject, commandLevel, nullIndex);
                }
            }
Exemplo n.º 4
0
        internal Queue <Stage <TRunInfo> > Create <TRunInfo>(
            StackableCommand <TRunInfo> command, bool hasGlobalOptions, Action <TRunInfo> postBuildCallback)
            where TRunInfo : class
        {
            var pipeline = new Queue <Stage <TRunInfo> >();

            if (command is Command <TRunInfo> rootCommand)
            {
                pipeline.Enqueue(rootCommand.ToStage());
            }

            Queue <Stage <TRunInfo> > commonStages = BuildCommonPipelineStages(command, hasGlobalOptions);

            while (commonStages.Any())
            {
                pipeline.Enqueue(commonStages.Dequeue());
            }

            // recursively add subcommand pipelines
            if (command.SubCommands.Any())
            {
                var subCommandInfoMap = new Dictionary <string, (Queue <Stage <TRunInfo> >, SubCommand <TRunInfo>)>();

                foreach (SubCommand <TRunInfo> subCommand in command.SubCommands)
                {
                    Queue <Stage <TRunInfo> > subCommandPipeline = Create(subCommand, hasGlobalOptions, postBuildCallback);

                    subCommandInfoMap.Add(subCommand.Key, (subCommandPipeline, subCommand));
                }

                pipeline.Enqueue(new SubCommandStage <TRunInfo>(subCommandInfoMap));
            }

            // EndProcessStage should ONLY be on a leaf stage node
            if (!command.SubCommands.Any())
            {
                pipeline.Enqueue(new EndProcessStage <TRunInfo>(postBuildCallback));
            }

            return(pipeline);
        }
Exemplo n.º 5
0
        private static void AppendCommandInfo <TRunInfo>(StringBuilder sb,
                                                         string rootCommandKey, bool isRoot, StackableCommand <TRunInfo> command, int commandDepth,
                                                         string programName)
            where TRunInfo : class
        {
            var helpTokens = new List <string>();

            if (!string.IsNullOrWhiteSpace(programName))
            {
                helpTokens.Add(programName);
            }
            helpTokens.Add(rootCommandKey);

            helpTokens.AddRange(GetRepeatedList("...", commandDepth));

            if (!isRoot)
            {
                helpTokens.Add(command.Key);
            }

            IEnumerable <string> argumentTokens = command.Arguments.Select(a => a.GetHelpToken());
            IEnumerable <string> optionTokens   = command.Options.Select(o => o.GetHelpToken());

            helpTokens.AddRange(argumentTokens);
            helpTokens.AddRange(optionTokens);

            if (command.SubCommands.Any())
            {
                var keys = string.Join("|", command.SubCommands.Select(c => c.Key));
                helpTokens.Add($"({keys})");
                helpTokens.Add("...");
            }

            string result = string.Join(" ", helpTokens.Select(t => t.Trim()));

            if (command.SubCommands.Any())
            {
                sb.AppendLine(PaddingRepeated(2) + result);
            }
            else if (!command.SubCommands.Any() && isRoot)
            {
                sb.AppendLine(result);
            }
            else
            {
                sb.AppendLine(PaddingRepeated(2) + result);
            }

            // recursively add subcommands with more padding
            foreach (SubCommand <TRunInfo> subCommand in command.SubCommands)
            {
                AppendCommandInfo(sb, rootCommandKey, isRoot: false, subCommand, commandDepth + 1, programName);
            }
        }