Exemple #1
0
        private static void WriteHelp(
            string commandName,
            bool online,
            bool manual,
            bool includeValues,
            Filter filter = null)
        {
            if (online)
            {
                OpenHelpInBrowser(commandName);
            }
            else if (commandName != null)
            {
                Command command = CommandLoader.LoadCommand(typeof(HelpCommand).Assembly, commandName);

                if (command == null)
                {
                    throw new InvalidOperationException($"Command '{commandName}' does not exist.");
                }

                WriteCommandHelp(command, includeValues: includeValues, filter: filter);
            }
            else if (manual)
            {
                WriteManual(includeValues: includeValues, filter: filter);
            }
            else
            {
                WriteCommandsHelp(includeValues: includeValues, filter: filter);
            }
        }
Exemple #2
0
        private static int Main(string[] args)
        {
#if DEBUG
            if (args.LastOrDefault() == "--debug")
            {
                WriteArgs(args.Take(args.Length - 1).ToArray(), Verbosity.Quiet);
                return(ExitCodes.NotSuccess);
            }
#endif
            Parser parser = null;
            try
            {
                parser = CreateParser(ignoreUnknownArguments: true);

                if (args == null ||
                    args.Length == 0)
                {
                    HelpCommand.WriteCommandsHelp();
                    return(ExitCodes.Success);
                }

                bool?success = null;

                ParserResult <BaseCommandLineOptions> defaultResult = parser
                                                                      .ParseArguments <BaseCommandLineOptions>(args)
                                                                      .WithParsed(options =>
                {
                    if (!options.Help)
                    {
                        return;
                    }

                    string commandName = args?.FirstOrDefault();
                    Command command    = (commandName != null)
                            ? CommandLoader.LoadCommand(typeof(Program).Assembly, commandName)
                            : null;

                    if (!ParseVerbosityAndOutput(options))
                    {
                        success = false;
                        return;
                    }

                    WriteArgs(args, Verbosity.Diagnostic);

                    if (command != null)
                    {
                        HelpCommand.WriteCommandHelp(command);
                    }
                    else
                    {
                        HelpCommand.WriteCommandsHelp();
                    }

                    success = true;
                });

                if (success == false)
                {
                    return(ExitCodes.Error);
                }

                if (success == true)
                {
                    return(ExitCodes.Success);
                }

                parser = CreateParser();

                ParserResult <object> parserResult = parser.ParseArguments(
                    args,
                    new Type[]
                {
                    typeof(AnalyzeCommandLineOptions),
                    typeof(FixCommandLineOptions),
                    typeof(FormatCommandLineOptions),
                    typeof(GenerateDocCommandLineOptions),
                    typeof(GenerateDocRootCommandLineOptions),
                    typeof(HelpCommandLineOptions),
                    typeof(ListSymbolsCommandLineOptions),
                    typeof(LogicalLinesOfCodeCommandLineOptions),
                    typeof(MigrateCommandLineOptions),
                    typeof(PhysicalLinesOfCodeCommandLineOptions),
                    typeof(RenameSymbolCommandLineOptions),
                    typeof(SpellcheckCommandLineOptions),
#if DEBUG
                    typeof(AnalyzeAssemblyCommandLineOptions),
                    typeof(FindSymbolsCommandLineOptions),
                    typeof(GenerateSourceReferencesCommandLineOptions),
                    typeof(ListVisualStudioCommandLineOptions),
                    typeof(ListReferencesCommandLineOptions),
                    typeof(SlnListCommandLineOptions),
#endif
                });

                parserResult.WithNotParsed(e =>
                {
                    if (e.Any(f => f.Tag == ErrorType.VersionRequestedError))
                    {
                        Console.WriteLine(typeof(Program).GetTypeInfo().Assembly.GetName().Version);
                        success = false;
                        return;
                    }

                    var helpText = new HelpText(SentenceBuilder.Create(), HelpCommand.GetHeadingText());

                    helpText = HelpText.DefaultParsingErrorsHandler(parserResult, helpText);

                    VerbAttribute verbAttribute = parserResult.TypeInfo.Current.GetCustomAttribute <VerbAttribute>();

                    if (verbAttribute != null)
                    {
                        helpText.AddPreOptionsText(Environment.NewLine + HelpCommand.GetFooterText(verbAttribute.Name));
                    }

                    Console.Error.WriteLine(helpText);

                    success = false;
                });

                if (success == true)
                {
                    return(ExitCodes.Success);
                }

                if (success == false)
                {
                    return(ExitCodes.Error);
                }

                parserResult.WithParsed <AbstractCommandLineOptions>(
                    options =>
                {
                    if (ParseVerbosityAndOutput(options))
                    {
                        WriteArgs(args, Verbosity.Diagnostic);
                    }
                    else
                    {
                        success = false;
                    }
                });

                if (success == false)
                {
                    return(ExitCodes.Error);
                }

                return(parserResult.MapResult(
                           (MSBuildCommandLineOptions options) =>
                {
                    switch (options)
                    {
                    case AnalyzeCommandLineOptions analyzeCommandLineOptions:
                        return AnalyzeAsync(analyzeCommandLineOptions).Result;

                    case FixCommandLineOptions fixCommandLineOptions:
                        return FixAsync(fixCommandLineOptions).Result;

                    case FormatCommandLineOptions formatCommandLineOptions:
                        return FormatAsync(formatCommandLineOptions).Result;

                    case GenerateDocCommandLineOptions generateDocCommandLineOptions:
                        return GenerateDocAsync(generateDocCommandLineOptions).Result;

                    case GenerateDocRootCommandLineOptions generateDocRootCommandLineOptions:
                        return GenerateDocRootAsync(generateDocRootCommandLineOptions).Result;

                    case ListSymbolsCommandLineOptions listSymbolsCommandLineOptions:
                        return ListSymbolsAsync(listSymbolsCommandLineOptions).Result;

                    case LogicalLinesOfCodeCommandLineOptions logicalLinesOfCodeCommandLineOptions:
                        return LogicalLinesOrCodeAsync(logicalLinesOfCodeCommandLineOptions).Result;

                    case PhysicalLinesOfCodeCommandLineOptions physicalLinesOfCodeCommandLineOptions:
                        return PhysicalLinesOfCodeAsync(physicalLinesOfCodeCommandLineOptions).Result;

                    case RenameSymbolCommandLineOptions renameSymbolCommandLineOptions:
                        return RenameSymbolAsync(renameSymbolCommandLineOptions).Result;

                    case SpellcheckCommandLineOptions spellcheckCommandLineOptions:
                        return SpellcheckAsync(spellcheckCommandLineOptions).Result;

#if DEBUG
                    case FindSymbolsCommandLineOptions findSymbolsCommandLineOptions:
                        return FindSymbolsAsync(findSymbolsCommandLineOptions).Result;

                    case GenerateSourceReferencesCommandLineOptions generateSourceReferencesCommandLineOptions:
                        return GenerateSourceReferencesAsync(generateSourceReferencesCommandLineOptions).Result;

                    case ListReferencesCommandLineOptions listReferencesCommandLineOptions:
                        return ListReferencesAsync(listReferencesCommandLineOptions).Result;

                    case SlnListCommandLineOptions slnListCommandLineOptions:
                        return SlnListAsync(slnListCommandLineOptions).Result;
#endif
                    default:
                        throw new InvalidOperationException();
                    }
                },
                           (AbstractCommandLineOptions options) =>
                {
                    switch (options)
                    {
                    case HelpCommandLineOptions helpCommandLineOptions:
                        return Help(helpCommandLineOptions);

                    case MigrateCommandLineOptions migrateCommandLineOptions:
                        return Migrate(migrateCommandLineOptions);

#if DEBUG
                    case AnalyzeAssemblyCommandLineOptions analyzeAssemblyCommandLineOptions:
                        return AnalyzeAssembly(analyzeAssemblyCommandLineOptions);

                    case ListVisualStudioCommandLineOptions listVisualStudioCommandLineOptions:
                        return ListVisualStudio(listVisualStudioCommandLineOptions);
#endif
                    default:
                        throw new InvalidOperationException();
                    }
                },
                           _ => ExitCodes.Error));
            }
            catch (Exception ex) when(ex is AggregateException ||
                                      ex is FileNotFoundException ||
                                      ex is InvalidOperationException)
            {
                WriteError(ex);
            }
            finally
            {
                parser?.Dispose();
                Out?.Dispose();
                Out = null;
            }

            return(ExitCodes.Error);
        }