简体   繁体   中英

If-else construct failing (.NET Framework)

Am working on a Binary Image maker app. I am having difficulty with the main method algorithm. The program always terminates after the first argument ('-help') has been supplied. It does not flow sequentially as depicted. I think the if-else construct is faulty but I cannot figure it out. Can someone have a look and advise?

namespace BinaryImage_Console
{
    /// <summary>
    /// Program Class of Console App
    /// </summary>
    class Program
    {
        /// <summary>
        /// Main entry point for Program
        /// </summary>
        /// <param name="args">Argument of main method</param>
        static void Main(string[] args)
        {

            Console.WriteLine("Welcome to Binary Image Maker");            
            Console.WriteLine("\nUse following command for help:");
            Console.WriteLine("dotnet ImageBinarizerApp -help");
            args = new String[] { Console.ReadLine() };

            //Test if necessary input arguments were supplied.
            if (args.Length < 8)
            {
                if(args.Length == 1 && args[0].Equals("-help"))
                {
                    Console.WriteLine("\nHelp:");
                    Console.WriteLine("\nPass the arguments as following:");
                    Console.WriteLine("\nExample with automatic RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image d:\\out.txt -width 32 -height 32");
                    Console.WriteLine("\nExample with explicit RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image d:\\out.txt -width 32 -height 32 -red 100 -green 100 -blue 100");
                }
                else
                {
                    Console.WriteLine("\nError: All necessary arguments are not passed. Please pass the arguments first.");
                }
                Console.WriteLine("\nPress any key to exit the application.");
                Console.ReadLine();
                return;
            }
            else
            {
                String inputImagePath = "";
                String outputImagePath = "";
                int imageWidth = 0;
                int imageHeight = 0;
                int redThreshold = -1;
                int greenThreshold = -1;
                int blueThreshold = -1;

                if(args[0].Equals("--input-image") && File.Exists(args[1]))
                {
                    inputImagePath = args[1];
                }
                else
                {
                    Console.WriteLine("\nError: Input file doesn't exist.");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                int separatorIndex = args[3].LastIndexOf(Path.DirectorySeparatorChar);
                if (args[2].Equals("--output-image") && separatorIndex >= 0 && Directory.Exists(args[3].Substring(0, separatorIndex)))
                {
                    outputImagePath = args[3];
                }
                else
                {
                    Console.WriteLine("\nError: Output Directory doesn't exist.");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                if (!args[4].Equals("-width") || !int.TryParse(args[5], out imageWidth))
                {
                    Console.WriteLine("\nError: Image Width should be integer.");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                if (!args[6].Equals("-height") || !int.TryParse(args[7], out imageHeight))
                {
                    Console.WriteLine("\nError: Image Height should be integer.");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                if(args.Length > 8)
                {
                    if(args.Length < 14)
                    {
                        Console.WriteLine("\nError: All three Red, Green and Blue Thresholds should be passed.");
                        Console.WriteLine("\nPress any key to exit the application.");
                        Console.ReadLine();
                        return;
                    }
                    else
                    {
                        if (!args[8].Equals("-red") || !(int.TryParse(args[9], out redThreshold)) || redThreshold < 0 || redThreshold > 255)
                        {
                            Console.WriteLine("\nError: Red Threshold should be in between 0 and 255.");
                            Console.WriteLine("\nPress any key to exit the application.");
                            Console.ReadLine();
                            return;
                        }

                        if (!args[10].Equals("-green") || !(int.TryParse(args[11], out greenThreshold)) || greenThreshold < 0 || greenThreshold > 255)
                        {
                            Console.WriteLine("\nError: Green Threshold should be in between 0 and 255.");
                            Console.WriteLine("\nPress any key to exit the application.");
                            Console.ReadLine();
                            return;
                        }

                        if (!args[12].Equals("-blue") || !(int.TryParse(args[13], out blueThreshold)) || blueThreshold < 0 || blueThreshold > 255)
                        {
                            Console.WriteLine("\nError: Blue Threshold should be in between 0 and 255.");
                            Console.WriteLine("\nPress any key to exit the application.");
                            Console.ReadLine();
                            return;
                        }
                    }                    
                }
                else
                {
                    redThreshold = -1;
                    greenThreshold = -1;
                    blueThreshold = -1;
                }

                Console.WriteLine("\nImage Binarization in progress...");

                try
                {
                    ImageBinarizerApplication obj = new ImageBinarizerApplication();
                    obj.Binarizer(inputImagePath, outputImagePath, imageWidth, imageHeight, redThreshold, greenThreshold, blueThreshold);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"\nError: {e.Message}");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                Console.WriteLine("\nImage Binarization completed.");
                Console.WriteLine("\nPress any key to exit the application.");

                Console.ReadLine();
            }            
        }
    }
}

I guess that if you are not typing -help as a parameter you are getting the message:

"Error: All necessary arguments are not passed. Please pass the arguments first."

Becuase you assign args as:

args = new String[] { Console.ReadLine() };

your args.Length will always be equal to 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM