Exemple #1
0
        static int Main(string[] args)
        {
            String command = "", sourceFile = "", destinationFile = "", type = "";
            int    width = 0, height = 0, startX = 0, startY = 0;

            // Get CLI arguments as List
            var arg = new List <string>(args);

            // Validate arguments count
            if (arg.Count < 1)
            {
                Usage();

                return(1);
            }

            ImageTools tools = new ImageTools();

            // Get command
            command = arg.First().ToLower();
            arg.RemoveAt(0);

            // Get Options
            while (arg.Count > 0)
            {
                switch (arg.First().ToLower())
                {
                case "-s":
                case "--source":
                    // Remove option name
                    arg.RemoveAt(0);

                    // Get option value
                    sourceFile = arg.First().ToLower();
                    arg.RemoveAt(0);
                    break;

                case "-d":
                case "--destination":
                    // Remove option name
                    arg.RemoveAt(0);

                    // Get option value
                    destinationFile = arg.First().ToLower();
                    arg.RemoveAt(0);
                    break;

                case "-t":
                case "--type":
                    // Remove option name
                    arg.RemoveAt(0);

                    // Get option value
                    type = arg.First().ToLower();
                    arg.RemoveAt(0);
                    break;

                case "-w":
                case "--width":
                    // Remove option name
                    arg.RemoveAt(0);

                    // Get option value
                    width = Int32.Parse(arg.First());
                    arg.RemoveAt(0);
                    break;

                case "-h":
                case "--height":
                    // Remove option name
                    arg.RemoveAt(0);

                    // Get option value
                    height = Int32.Parse(arg.First());
                    arg.RemoveAt(0);
                    break;

                case "-x":
                case "--startX":
                    // Remove option name
                    arg.RemoveAt(0);

                    // Get option value
                    startX = Int32.Parse(arg.First());
                    arg.RemoveAt(0);
                    break;

                case "-y":
                case "--startY":
                    // Remove option name
                    arg.RemoveAt(0);

                    // Get option value
                    startY = Int32.Parse(arg.First());
                    arg.RemoveAt(0);
                    break;

                default:
                    // In case if we have nonsense as option
                    arg.RemoveAt(0);
                    break;
                }
            }

            // TODO: Add more exceptions to exception handling
            try {
                switch (command)
                {
                case "help":
                case "h":
                    Usage();
                    return(0);

                case "convert":
                case "c":
                    tools.Convert(sourceFile, destinationFile, type);
                    break;

                case "resize":
                case "r":
                    tools.Resize(sourceFile, destinationFile, type, width, height, startX, startY);
                    break;

                default:
                    Usage();
                    return(1);
                }
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                return(1);
            }

            return(0);
        }