Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
        private static void WriteHelp(
            string?commandName,
            bool manual,
            bool includeValues,
            Filter?filter = null)
        {
            if (commandName != null)
            {
                Command?command = CommandLoader.LoadCommand(typeof(HelpCommand).Assembly, commandName);

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

                WriteCommandHelp(command, includeValues: includeValues, filter: filter);
            }
            else if (manual)
            {
                WriteManual(includeValues: includeValues, filter: filter);
            }
            else
            {
                WriteCommandsHelp(includeValues: includeValues, filter: filter);
            }
        }
Ejemplo n.º 3
0
        public static string GetHelpText(string commandName, bool includeValues = false)
        {
            Command command = CommandLoader.LoadCommand(typeof(HelpCommand).Assembly, commandName);

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

            return(GetHelpText(command, includeValues));
        }
Ejemplo n.º 4
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));
            }
        }
Ejemplo n.º 5
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();

            try
            {
                Parser parser = CreateParser(ignoreUnknownArguments: true);

                bool help = false;

                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;

                    ParseVerbosityAndOutput(options);
                    WriteArgs(args);

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

                    help = true;
                })
#if DEBUG
                                                                      .WithNotParsed(_ =>
                {
                });
#else
                ;
#endif

                if (help)
                {
                    return(0);
                }

                bool success = true;

                parser = CreateParser();

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

                parserResult.WithNotParsed(_ =>
                {
                    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)
                {
                    return(2);
                }

                parserResult.WithParsed <AbstractCommandLineOptions>(options =>
                {
                    success = ParseVerbosityAndOutput(options);
                    WriteArgs(args);
                });

                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),
                           (ListPatternsCommandLineOptions options) => ListPatterns(options),
                           (MatchCommandLineOptions options) => Match(options),
                           (RenameCommandLineOptions options) => Rename(options),
                           (ReplaceCommandLineOptions options) => Replace(options),
                           (SplitCommandLineOptions options) => Split(options),
                           _ => 2));
            }
            catch (Exception ex)
            {
                WriteError(ex);
            }
            finally
            {
                Out?.Dispose();
                Out = null;
            }

            return(2);
        }
Ejemplo n.º 6
0
        private static int Main(string[] args)
        {
#if DEBUG // short command syntax
            if (args?.Length > 0)
            {
                switch (args[0])
                {
                case "f":
                {
                    ReplaceArgs("find");
                    break;
                }

                case "r":
                {
                    ReplaceArgs("replace");
                    break;
                }
                }

                void ReplaceArgs(string commandName)
                {
                    Array.Resize(ref args, args.Length + 1);

                    for (int i = args.Length - 1; i >= 2; i--)
                    {
                        args[i] = args[i - 1];
                    }

                    args[0] = commandName;
                    args[1] = "-c";
                }
            }
#endif
            try
            {
                Parser parser = CreateParser(ignoreUnknownArguments: true);

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

                var success = true;
                var help    = false;

                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;

                    success = ParseVerbosityAndOutput(options);

                    if (!success)
                    {
                        return;
                    }

                    WriteArgs(args);

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

                    help = true;
                })
#if DEBUG
                                                                      .WithNotParsed(_ =>
                {
                });
#else
                ;
#endif
                if (!success)
                {
                    return(ExitCodes.Error);
                }

                if (help)
                {
                    return(ExitCodes.Match);
                }

                parser = CreateParser();

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

                parserResult.WithNotParsed(_ =>
                {
                    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)
                {
                    return(ExitCodes.Error);
                }

                parserResult.WithParsed <AbstractCommandLineOptions>(options =>
                {
                    success = ParseVerbosityAndOutput(options);

                    if (success)
                    {
                        WriteArgs(args);
                    }
                });

                if (!success)
                {
                    return(ExitCodes.Error);
                }

                return(parserResult.MapResult(
                           (CopyCommandLineOptions options) => Copy(options),
                           (MoveCommandLineOptions options) => Move(options),
                           (SyncCommandLineOptions options) => Sync(options),
                           (DeleteCommandLineOptions options) => Delete(options),
                           (EscapeCommandLineOptions options) => Escape(options),
                           (FindCommandLineOptions options) => Find(options),
                           (HelpCommandLineOptions options) => Help(options),
                           (ListPatternsCommandLineOptions options) => ListPatterns(options),
                           (MatchCommandLineOptions options) => Match(options),
                           (SpellcheckCommandLineOptions options) => Spellcheck(options),
                           (RenameCommandLineOptions options) => Rename(options),
                           (ReplaceCommandLineOptions options) => Replace(options),
                           (SplitCommandLineOptions options) => Split(options),
                           _ => ExitCodes.Error));
            }
            catch (Exception ex)
            {
                WriteError(ex);
            }
            finally
            {
                Out?.Dispose();
                Out = null;
            }

            return(ExitCodes.Error);
        }
Ejemplo n.º 7
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));
            }