예제 #1
0
        public IEnumerable <ParsedCommand> Adapt <T>(IEnumerable <ParsedCommand> commands)
        {
            var definition = DefinitionHelper.GetCommandDefinitions(typeof(T)).ToArray();

            foreach (var command in commands)
            {
                //check the command first, then the params
                _commandChecks.Checks(command, typeof(T), definition);

                //adapt
                var def = definition.First(d => d.Name.ToLower() == command.Name.ToLower());
                command.Param = Internal(command.Param, def);
                command.Param = InternalOptionalMethodParams(command.Param, def);

                if (command.Param != null)
                {
                    //named command with only 1 nameless param: the name of the param is the name of the command
                    if (def.SubParams.Length <= 1 && String.IsNullOrEmpty(command.Param.Name))
                    {
                        command.Param.Name = command.Name;
                    }
                }

                yield return(command);
            }
        }
예제 #2
0
        public void Initialize(Type type)
        {
            var defs = DefinitionHelper.GetCommandDefinitions(type).ToList();

            if (!defs.Any(cmd => cmd.Name.ToLower() == this.HelpCommandDefinition.Name.ToLower()))
            {
                defs.Add(HelpCommandDefinition);

                DefinitionHelper.Update(type, defs.ToArray());
            }
        }
예제 #3
0
        public void GetHelp(Type type, IParsedParam parsedParam)
        {
            this.Initialize(type);

            string helpParam = ((SimpleParam)parsedParam)?.Value;

            var cmdDefs = DefinitionHelper.GetCommandDefinitions(type);

            cmdDefs.OrderBy(cDef => cDef.Name).ToList();

            if (String.IsNullOrEmpty(helpParam))
            {
                string indentation = new string( ' ', 2 );
                Console.WriteLine();

                Console.WriteLine($"{indentation}AVAILABLE COMMANDS:");
                Console.WriteLine();

                Console.WriteLine($"{indentation}{indentation}{"COMMAND",-20}{"DESCRIPTION"}");
                Console.WriteLine();

                foreach (var cmdDef in cmdDefs)
                {
                    Console.WriteLine($"{indentation}{indentation}{cmdDef.Name.ToLower(),-20}{cmdDef.Options.HelpText}");
                }

                Console.WriteLine();
                Console.WriteLine($"{indentation}To invoke a command, type --<command>");
                Console.WriteLine($"{indentation}For more info about a specific command, type --help <command>");
                //Console.WriteLine( $"{indentation}Collection parameters must be surrounded by []" );
                //Console.WriteLine( $"{indentation}Non primitives types must be surrounded by ()" );
                Console.WriteLine();
            }

            //else if( !(helpCmd.Param is SimpleParam) )
            //{
            //    Console.WriteLine( "--help accepts only one input argument" );

            //    //if( this.ShowHelpOnError )
            //    //    this.Parse<T>( $"--{HelpCommandDefinition.Name.ToLower()} {HelpCommandDefinition.Name.ToLower()}" );
            //    //else
            //    //    Console.WriteLine();
            //}
            else
            {
                var cmdUsage = cmdDefs.FirstOrDefault(cmd => cmd.Name.ToLower() == helpParam.ToLower());
                if (cmdUsage == null)
                {
                    Console.WriteLine($"A command named '{helpParam}' does not exist");

                    //if( this.ShowHelpOnError )
                    //    this.Parse<T>( $"--{HelpCommandDefinition.Name.ToLower()}" );
                    //else
                    //    Console.WriteLine();
                }
                else
                {
                    string indentation = new string( ' ', 2 );

                    Console.WriteLine();
                    Console.WriteLine($"{indentation}COMMAND '{cmdUsage.Name.ToUpper()}'");

                    if (!String.IsNullOrWhiteSpace(cmdUsage.Options.HelpText))
                    {
                        Console.WriteLine($"{indentation}{indentation}{cmdUsage.Options.HelpText}");
                    }

                    Console.WriteLine();
                    Console.WriteLine($"{indentation}USAGE:");

                    var parameters = cmdUsage.SubParams.Select(p => p.Options.IsRequired ? p.Name.ToLower() : $"[{p.Name.ToLower()}]");
                    Console.WriteLine($"{indentation}{indentation}--{cmdUsage.Name.ToLower() } {String.Join( " ", parameters )}");

                    if (cmdUsage.SubParams.Length > 0)
                    {
                        Console.WriteLine();
                        Console.WriteLine($"{indentation}PARAMETERS DETAILS:");
                        Console.WriteLine();

                        int namePadRight     = Math.Max(cmdUsage.SubParams.Max(p => p.Name.Length), "NAME".Length) + 3;
                        int typePadRight     = Math.Max(cmdUsage.SubParams.Max(p => p.Type.GetPrettifiedName().Length), "TYPE".Length) + 3;
                        int requiredPadRight = "REQUIRED".Length + 3;

                        Console.WriteLine($"{indentation}{indentation}{"NAME".PadRight( namePadRight )}{"TYPE".PadRight( typePadRight )}{"REQUIRED".PadRight( requiredPadRight )}DESCRIPTION");
                        foreach (var param in cmdUsage.SubParams)
                        {
                            string required    = param.Options.IsRequired ? "Yes" : "No";
                            string description = param.Options.HelpText ?? "<No description provided>";

                            Console.WriteLine($"{indentation}{indentation}{param.Name.PadRight( namePadRight )}{param.Type.GetPrettifiedName().PadRight( typePadRight )}{required.PadRight( requiredPadRight )}{description}");
                        }
                    }
                    Console.WriteLine();
                }
            }
        }