示例#1
0
        public static void Run(string cmdname)
        {
            if (String.IsNullOrWhiteSpace(cmdname))
            {
                Console.WriteLine($"\nRant Command Line Tools for Rant {typeof(RantEngine).Assembly.GetName().Version.ToString(3)}\n");
                Console.WriteLine($"Usage: {Program.Name} <command> [args]\n");
                Console.WriteLine("Use the --help option on any command to display its help text.\n");
                Console.WriteLine("Available commands: ");
                foreach (var c in _cmdMap.Values.OrderBy(c => c.Name))
                {
                    Console.WriteLine($"  {c.Name}\t\t{c.Description}");
                }
                Console.WriteLine();
                return;
            }

            if (!_cmdMap.TryGetValue(cmdname, out Command cmd))
            {
                Console.WriteLine($"Command '{cmdname}' does not exist.");
                return;
            }

            if (CmdLine.Flag("h") || CmdLine.Flag("help"))
            {
                GetHelpText(cmd);
                return;
            }

            if (cmd._requiresPath && CmdLine.GetPaths().Length == 0)
            {
                throw new ArgumentException($"The command '{cmd.Name}' requires a path.");
            }

            var missing = cmd._params.Where(p => p.Required && String.IsNullOrEmpty(CmdLine.Property(p.Name))).ToArray();

            if (missing.Length > 0)
            {
                throw new ArgumentException($"The command '{cmd.Name}' is missing the following required properties: {missing.Aggregate(missing[0].Name, (c, n) => c + ", " + n.Name)}");
            }

            foreach (var p in cmd._params.Where(p => p.Required))
            {
                if (String.IsNullOrEmpty(CmdLine.Property(p.Name)))
                {
                    throw new ArgumentException($"The command '{cmd.Name}' requires a -{p.Name} parameter.");
                }
            }

            cmd.OnRun();
        }
示例#2
0
文件: Program.cs 项目: thygrrr/rant3
        private static void Main(string[] args)
        {
            if (CmdLine.Flag("version"))
            {
                WriteLine($"Rant {typeof(RantEngine).Assembly.GetName().Version}");
                return;
            }
#if !DEBUG
            try
            {
#endif
            Command.Run(CmdLine.Command);
#if !DEBUG
        }

        catch (Exception ex)
        {
            ForegroundColor = ConsoleColor.Red;
            WriteLine(ex.Message);
            ResetColor();
            Environment.Exit(1);
        }
#endif
        }