static bool ParseArguments()

in Source/AnalyzerMain.cs [48:183]


        static bool ParseArguments(string[] args)
        {
            if (args.Contains("-h") || args.Contains("-help"))
            {
                return false;
            }

            for (int i = 0; i < args.Length; ++i)
            {
                if (args[i] == "-data")
                {
                    // The next arg is the file location
                    ++i;
                    if (i >= args.Length)
                    {
                        // Error if there are no more arguments
                        Console.WriteLine("Parameter \"-data\" requires a file path");
                        return false;
                    }
                    else if (File.Exists(args[i]) == false)
                    {
                        // Don't just run the analyzer with the default data if the data parameter was bad
                        Console.WriteLine("Error: {0} is not a valid data file.", args[i]);
                        return false;
                    }
                    else
                    {
                        m_dataFilePath = args[i];
                    }

                }
                else if (args[i] == "-rules")
                {
                    // The next arg is the rules location
                    ++i;
                    if (i >= args.Length)
                    {
                        // Error if there are no more arguments
                        Console.WriteLine("Parameter \"-rules\" requires a file path");
                        return false;
                    }
                    else if (File.Exists(args[i]) == false)
                    {
                        Console.WriteLine("Warning: {0} is not a valid rules file. Using default rules.", args[i]);

                    }
                    else
                    {
                        m_rulesFilePath = args[i];
                    }
                }
                else if (args[i] == "-outputdir")
                {
                    // The next arg is where the report should be located
                    ++i;
                    if (i >= args.Length || args[i].StartsWith("-"))
                    {
                        // Error if there are no more arguments
                        Console.WriteLine("Parameter \"-outputdir\" requires a directory path");
                        return false;
                    }
                    else if (!Directory.Exists(args[i]))
                    {
                        // Create the report directory if it doesn't exist.
                        System.IO.Directory.CreateDirectory(args[i]);
                    }

                    m_outputDirectory = args[i];
                }
                else if (args[i] == "-allendpoints")
                {
                    m_allEndpoints = true;
                }
                else if (args[i] == "-internal")
                {
                    m_isInternal = true;
                }
                else if(args[i] == "-json")
                {
                    m_jsonOnly = true;
                }
                else if(args[i] == "-reports")
                {
                    ++i;

                    var reports = args[i].Split(',');

                    m_reports.AddRange(reports);

                    if(m_reports.Contains("default"))
                    {
                        m_reports.Remove("default");
                    }
                    else
                    {
                        m_defaults = false;
                    }
                }
                else if(args[i] == "-customUserAgent")
                {
                    ++i;
                    m_customUserAgent = args[i];
                }
                else
                {
                    Console.WriteLine("Invalid Parameter: " + args[i]);
                    return false;
                }
            }

            // Set defaults if these where not set by command line switches, and check files and directories exist

            if (string.IsNullOrEmpty(m_dataFilePath))
            {
                m_dataFilePath = Utils.GeneratePathToFile("data.csv");
                Console.WriteLine("No argument provided. Using data file: \"data.csv\"");
            }
            if (!File.Exists(m_dataFilePath))
            {
                Console.WriteLine("{0} - data missing - file not found.", m_dataFilePath);
                return false;
            }

            if (string.IsNullOrEmpty(m_outputDirectory))
            {
                m_outputDirectory = ".\\";
                Console.WriteLine("Writing reports to default location: \".\\\"");
            }
            if (!Directory.Exists(m_outputDirectory))
            {
                Directory.CreateDirectory(m_outputDirectory);
            }


            return true;
        }