Esempio n. 1
0
 public void WriteVersion(ICommandExecutorOptions options)
 {
     foreach (var version in options.VersionProvider.GetVersions())
     {
         Console.WriteLine(version);
     }
 }
 public CommandExecutor CreateSut(ICommandParser parser = null, ICommandExecutorOptions executorOptions = null, IAttributeDecorator attributeDecorator = null, IValueConverter valueConverter = null, ICommandHelper commandHelper = null, ITypeHelper typeHelper = null, ICommandExecutorImpl impl = null)
 {
     return(new CommandExecutor(parser ?? Substitute.For <ICommandParser>(),
                                executorOptions ?? Substitute.For <ICommandExecutorOptions>(),
                                attributeDecorator ?? Substitute.For <IAttributeDecorator>(),
                                valueConverter ?? Substitute.For <IValueConverter>(),
                                commandHelper ?? Substitute.For <ICommandHelper>(),
                                typeHelper ?? Substitute.For <ITypeHelper>(),
                                impl));
 }
Esempio n. 3
0
 public CommandExecutor(ICommandParser parser, ICommandExecutorOptions executorOptions, IAttributeDecorator attributeDecorator, IValueConverter valueConverter, ICommandHelper commandHelper, ITypeHelper typeHelper, ICommandExecutorImpl impl = null)
 {
     _parser             = parser;
     _options            = executorOptions;
     _attributeDecorator = attributeDecorator;
     _valueConverter     = valueConverter;
     _commandHelper      = commandHelper;
     _typeHelper         = typeHelper;
     _impl = impl ?? this;
 }
Esempio n. 4
0
        void ICommandHelper.WriteVerbHelp(VerbAttribute verb, ICommandExecutorOptions options, GlobalOptionsWrapper globalOptions)
        {
            var sortedDictionary = new SortedDictionary <string, string>();

            _commandHelper.GetVerbHelp(verb, "", sortedDictionary);

            if (verb.IsRoot)
            {
                WriteUsage(globalOptions, globalOptions.GlobalOptions != null, "COMMAND");

                WriteGlobalOptions(globalOptions);
            }

            Console.WriteLine();

            var commandGroups = verb.Commands.Select(x => x.Value).Distinct().Select(x => new
            {
                x.Category,
                Name = x.GetName(),
                x.HelpText
            }).Concat(verb.Verbs.Select(x => x.Value).Distinct().Select(x => new
            {
                x.Category,
                Name = x.GetName(),
                x.HelpText
            })).OrderBy(x => x.Name).GroupBy(x => x.Category);

            foreach (var commandGroup in commandGroups)
            {
                Console.WriteLine((commandGroup.Key ?? "Commands") + ":");

                foreach (var command in commandGroup)
                {
                    Console.CursorLeft = 2;
                    Console.Write(command.Name);

                    foreach (var line in GetLines(command.HelpText, 55))
                    {
                        Console.CursorLeft = 14;
                        Console.Write(line);
                    }

                    Console.WriteLine();
                }

                Console.WriteLine();
            }

            Console.WriteLine($"For help with command syntax, type `COMMAND --{options.HelpLongForm}` or `COMMAND -{options.HelpShortForm}`");
        }
Esempio n. 5
0
        bool ICommandHelper.TryShowHelpOrVersion(CommandPart commandPart, VerbAttribute verb, string key, ICommandExecutorOptions options, GlobalOptionsWrapper globalOptions)
        {
            if ((commandPart.IsShortForm && key[0] == options.VersionShortForm) || (!commandPart.IsShortForm && key.Equals(options.VersionLongForm, StringComparison.OrdinalIgnoreCase)))
            {
                _commandHelper.WriteVersion(options);
                return(true);
            }

            if ((commandPart.IsShortForm && key[0] == options.HelpShortForm) || (!commandPart.IsShortForm && key.Equals(options.HelpLongForm, StringComparison.OrdinalIgnoreCase)))
            {
                _commandHelper.WriteVerbHelp(verb, options, globalOptions);
                return(true);
            }

            return(false);
        }
Esempio n. 6
0
        void ICommandHelper.WriteCommandHelp(CommandAttribute command, ICommandExecutorOptions options, GlobalOptionsWrapper globalOptions)
        {
            Console.Write($"Usage:  {command.GetName()}");

            if (options.ValuesFirst)
            {
                foreach (var value in command.Values)
                {
                    Console.Write($" {{{value.Parameter.Name}}}");
                }
            }

            if (command.Options.Count > 0)
            {
                Console.Write(" [Options]");
            }

            if (!options.ValuesFirst)
            {
                foreach (var value in command.Values)
                {
                    Console.Write($" {{{value.Parameter.Name}}}");
                }
            }

            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine(command.HelpText);

            if (command.Values.Count > 0)
            {
                Console.WriteLine();
                Console.WriteLine("Values:");

                foreach (var value in command.Values)
                {
                    Console.WriteLine($"    {value.GetName(),-20}{value.HelpText}");
                }
            }

            if (command.Options.Count == 0)
            {
                return;
            }

            Console.WriteLine();
            Console.WriteLine("Options:");

            var written = new HashSet <OptionAttribute>();

            foreach (var option in command.Options)
            {
                if (written.Contains(option.Value))
                {
                    continue;
                }

                Console.Write("    ");

                string name = "   ";

                if (option.Value.ShortForm != '\0')
                {
                    name = " -" + option.Value.ShortForm;
                }

                if (option.Value.LongForm != null)
                {
                    name += " --" + option.Value.LongForm + (option.Value.Parameter.ParameterType == typeof(bool) ? "" : "=<value>");
                }

                var helpText = option.Value.HelpText;

                if (option.Value.Parameter.ParameterType.IsArray)
                {
                    helpText = "(Array) " + helpText;
                }

                Console.WriteLine(name.PadRight(30, ' ') + helpText);

                written.Add(option.Value);
            }

            Console.WriteLine();
        }