// This will search for .nupkg files, identify the packages which are templates
        // and analyze them. If the path points to a .nupkg file, that will be analyzed
        // and results returned.
        // > templates analyze --packages --path c:\data\mycode\sample\templates

        // This will search for templates under the path specified
        // and analyze them.
        // > templates analyze --folders --path c:\data\mycode\sample\templates

        public override Command CreateCommand() =>
        new Command(name: "analyze", description: "template analyzer tool")
        {
            CommandHandler.Create <string[], string[]>(
                (packages, folders) => {
                _reporter.WriteLine("analyze command");

                _reporter.WriteLine(packages != null ?
                                    $"packages: {string.Join(',',packages)}" :
                                    "packages is null");

                _reporter.WriteLine(folders != null ?
                                    $"folders: {string.Join(',',folders)}" :
                                    "folders is null");

                bool foundIssues = false;
                foreach (var f in folders)
                {
                    // finding folders under f that has a .template.config folder
                    var foundDirs = Directory.GetDirectories(f, ".template.config", new EnumerationOptions {
                        RecurseSubdirectories = true, AttributesToSkip = FileAttributes.System
                    });
                    if (foundDirs == null || foundDirs.Length <= 0)
                    {
                        _reporter.WriteLine($"ERROR: No templates found under path '{f}'");
                    }
                    foreach (var fd in foundDirs)
                    {
                        foundIssues = _templateAnalyzer.Analyze(Directory.GetParent(fd).FullName) || foundIssues;
                    }
                }

                return(foundIssues ? -1 : 0);
            }),
            OptionPackages(),
            OptionFolders(),
        };
        // This will search for .nupkg files, identify the packages which are templates
        // and analyze them. If the path points to a .nupkg file, that will be analyzed
        // and results returned.
        // > templates analyze --packages --path c:\data\mycode\sample\templates

        // This will search for templates under the path specified
        // and analyze them.
        // > templates analyze --folders --path c:\data\mycode\sample\templates

        public override Command CreateCommand() =>
        new Command(name: "analyze", description: "template analyzer tool")
        {
            CommandHandler.Create <string[], string[], bool, bool>(
                (packages, folders, verbose, usePackageCache) => {
                _reporter.EnableVerbose = verbose;
                _reporter.WriteLine("analyzing...");

                _reporter.WriteVerboseLine(packages != null ?
                                           $"packages: {string.Join(',',packages)}" :
                                           "packages is null");

                _reporter.WriteVerboseLine(folders != null ?
                                           $"folders: {string.Join(',',folders)}" :
                                           "folders is null");

                var foldersList = new List <string>();
                if (folders != null && folders.Length > 0)
                {
                    foldersList.AddRange(folders);
                }

                if (packages != null && packages.Length > 0)
                {
                    foreach (var p in packages)
                    {
                        // check that the path exists and then extract to a folder
                        if (File.Exists(p))
                        {
                            _reporter.WriteVerbose($"extracting package '{p}'");
                            var packageFolder = _remoteFile.ExtractZipLocally(p, !usePackageCache);
                            foldersList.Add(packageFolder);
                        }
                        else
                        {
                            _reporter.WriteLine($"ERROR: package not found at '{p}'");
                        }
                    }
                }

                var analyzeResult = new AnalyzeResult();
                if (foldersList != null && foldersList.Count > 0)
                {
                    foreach (var f in foldersList)
                    {
                        // finding folders under f that has a .template.config folder
                        var foundDirs = Directory.GetDirectories(f, ".template.config", new EnumerationOptions {
                            RecurseSubdirectories = true, ReturnSpecialDirectories = true
                        });
                        foundDirs = Directory.GetDirectories(f, ".template.config", SearchOption.AllDirectories);

                        if (foundDirs == null || foundDirs.Length <= 0)
                        {
                            _reporter.WriteLine($"ERROR: No templates found under path '{f}'");
                        }
                        foreach (var fd in foundDirs)
                        {
                            // TODO: Is this correct?
                            analyzeResult = AnalyzeResult.Combine(
                                analyzeResult,
                                _templateAnalyzer.Analyze(Directory.GetParent(fd).FullName));

                            // analyzeResult = _templateAnalyzer.Analyze(Directory.GetParent(fd).FullName);
                        }
                    }
                }

                //return analyzeResult;
                return(analyzeResult.HasErrors() ? -1 : 0);
                // return foundIssues ? -1 : 0;
            }),
            OptionPackages(),
            OptionFolders(),
            OptionVerbose(),
            OptionUsePackageCache()
        };