Exemplo n.º 1
0
        public static string CreateAvailableCommandsHelpText()
        {
            var catalog = new CommandCatalog();
            var lines   =
                from command in catalog
                let optionsType = CommandCatalog.GetOptionsType(command)
                                  let options                                                           = Activator.CreateInstance(optionsType)
                                                                       let name                         = CommandCatalog.GetCommandName(command)
                                                                                              let title = Prompt.GetAppTitle(options)
                                                                                                          orderby name
                                                                                                          select new[] {
                name,
                ' '.Replicate(12) + title
            };
            var text = lines.SelectMany().JoinLines();

            return(text);
        }
Exemplo n.º 2
0
        private static int CallCommand(string commandName, string[] commandArgs)
        {
            var commands = new CommandCatalog();
            var command  = (
                from x in commands
                let name = CommandCatalog.GetCommandName(x)
                           where name == commandName
                           select x
                ).FirstOrDefault();

            if (command == null)
            {
                Prompt.PrintInvalidUsage($"Comando não encontrado: {commandName}");
                return(1);
            }

            var commandMethod = CommandCatalog.GetCommandMethod(command);

            var commmandOptionsType = CommandCatalog.GetOptionsType(command);
            var commmandOptions     = Activator.CreateInstance(commmandOptionsType);

            PosixParser.ParseArgs(commmandOptions, commandArgs);

            var result = commandMethod.Invoke(command, new[] { commmandOptions });

            if (result is int)
            {
                return((int)result);
            }

            if (result is bool)
            {
                return(true.Equals(result) ? 0 : 1);
            }

            return(0);
        }