Exemplo n.º 1
0
        private static string GetHelpText(string commandName = null, bool manual = false, bool includeValues = false)
        {
            if (commandName != null)
            {
                Command command = CommandLoader.LoadCommand(typeof(HelpCommand).Assembly, commandName);

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

                return(HelpProvider.GetHelpText(command, includeValues));
            }
            else if (manual)
            {
                return(HelpProvider.GetManual(includeValues: includeValues));
            }
            else
            {
                return(HelpProvider.GetHelpText(includeValues: includeValues));
            }
        }
Exemplo n.º 2
0
        private static int Main(string[] args)
        {
            //WriteLine($"Orang Command Line Tool version {typeof(Program).GetTypeInfo().Assembly.GetName().Version}");
            //WriteLine("Copyright (c) Josef Pihrt. All rights reserved.");
            //WriteLine();

            if (args != null)
            {
                if (args.Length == 1)
                {
                    if (IsHelpOption(args[0]))
                    {
                        Console.Write(HelpProvider.GetHelpText());
                        return(0);
                    }
                }
                else if (args.Length == 2)
                {
                    if (args?.Length == 2 &&
                        IsHelpOption(args[1]))
                    {
                        Command command = CommandLoader.LoadCommand(typeof(HelpCommand).Assembly, args[0]);

                        if (command != null)
                        {
                            Console.Write(HelpProvider.GetHelpText(command));
                            return(0);
                        }
                    }
                }
            }

            try
            {
                ParserSettings defaultSettings = Parser.Default.Settings;

                var parser = new Parser(settings =>
                {
                    settings.AutoHelp    = false;
                    settings.AutoVersion = defaultSettings.AutoVersion;
                    settings.CaseInsensitiveEnumValues = defaultSettings.CaseInsensitiveEnumValues;
                    settings.CaseSensitive             = defaultSettings.CaseSensitive;
                    settings.EnableDashDash            = true;
                    settings.HelpWriter             = null;
                    settings.IgnoreUnknownArguments = defaultSettings.IgnoreUnknownArguments;
                    settings.MaximumDisplayWidth    = defaultSettings.MaximumDisplayWidth;
                    settings.ParsingCulture         = defaultSettings.ParsingCulture;
                });

                ParserResult <object> parserResult = parser.ParseArguments <
                    CopyCommandLineOptions,
                    DeleteCommandLineOptions,
                    EscapeCommandLineOptions,
                    FindCommandLineOptions,
                    HelpCommandLineOptions,
                    ListSyntaxCommandLineOptions,
                    MatchCommandLineOptions,
                    MoveCommandLineOptions,
                    RenameCommandLineOptions,
                    ReplaceCommandLineOptions,
                    SplitCommandLineOptions
                    >(args);

                bool help    = false;
                bool success = true;

                parserResult.WithNotParsed(_ =>
                {
                    var helpText = new HelpText(SentenceBuilder.Create(), HelpProvider.GetHeadingText());

                    helpText = HelpText.DefaultParsingErrorsHandler(parserResult, helpText);

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

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

                    Console.Error.WriteLine(helpText);

                    success = false;
                });

                parserResult.WithParsed <AbstractCommandLineOptions>(options =>
                {
                    if (options.Help)
                    {
                        string commandName = options.GetType().GetCustomAttribute <VerbAttribute>().Name;

                        Console.WriteLine(HelpProvider.GetHelpText(commandName));

                        help = true;
                        return;
                    }

                    success = false;

                    var defaultVerbosity = Verbosity.Normal;

                    if (options.Verbosity == null ||
                        TryParseVerbosity(options.Verbosity, out defaultVerbosity))
                    {
                        ConsoleOut.Verbosity = defaultVerbosity;

                        if (TryParseOutputOptions(options.Output, OptionNames.Output, out string filePath, out Verbosity fileVerbosity, out Encoding encoding, out bool append))
                        {
                            if (filePath != null)
                            {
                                FileMode fileMode = (append)
                                    ? FileMode.Append
                                    : FileMode.Create;

                                var stream = new FileStream(filePath, fileMode, FileAccess.Write, FileShare.Read);
                                var writer = new StreamWriter(stream, encoding, bufferSize: 4096, leaveOpen: false);
                                Out        = new TextWriterWithVerbosity(writer)
                                {
                                    Verbosity = fileVerbosity
                                };
                            }

                            success = true;
                        }
                        else
                        {
                            success = false;
                        }
                    }
                });

                if (help)
                {
                    return(0);
                }

                if (!success)
                {
                    return(2);
                }

                return(parserResult.MapResult(
                           (CopyCommandLineOptions options) => Copy(options),
                           (MoveCommandLineOptions options) => Move(options),
                           (DeleteCommandLineOptions options) => Delete(options),
                           (EscapeCommandLineOptions options) => Escape(options),
                           (FindCommandLineOptions options) => Find(options),
                           (HelpCommandLineOptions options) => Help(options),
                           (ListSyntaxCommandLineOptions options) => ListSyntax(options),
                           (MatchCommandLineOptions options) => Match(options),
                           (RenameCommandLineOptions options) => Rename(options),
                           (ReplaceCommandLineOptions options) => Replace(options),
                           (SplitCommandLineOptions options) => Split(options),
                           _ => 2));
            }