Пример #1
0
        internal Example WithArgument(Argument argument, params string[] args)
        {
            if (!_currentCommand.Arguments.Contains(argument))
            {
                throw new ArgumentException($"Command {_currentCommand.Name} does not have argument {argument.Name}");
            }

            if (args.Any())
            {
                _commandParts.AddRange(args.Select(a => a.Any(char.IsWhiteSpace) ? $"'{a}'" : a));
                return(this);
            }
            _commandParts.Add(CommandLineUtils.FormatArgumentUsage(argument));
            return(this);
        }
Пример #2
0
        internal Example WithOption(Option option, params string[] args)
        {
            if (!_currentCommand.Options.Contains(option) && !_currentCommand.Options.Any(o => o.Name == option.Name))
            {
                throw new ArgumentException($"Command {_currentCommand.Name} does not have option {option.Name}");
            }

            _commandParts.Add(option.Aliases.First());
            if (args.Any())
            {
                _commandParts.AddRange(args.Select(a => a.Any(char.IsWhiteSpace) ? $"'{a}'" : a));
                return(this);
            }
            if (option.Arity.MinimumNumberOfValues == 0)
            {
                return(this);
            }

            _commandParts.Add(CommandLineUtils.FormatArgumentUsage(option));
            return(this);
        }
Пример #3
0
        private static IEnumerable <string> GetCustomUsageParts(
            HelpContext context,
            Command command,
            bool showSubcommands = true,
            bool showArguments   = true,
            bool showOptions     = true)
        {
            List <Command> parentCommands = new List <Command>();
            Command?       nextCommand    = command;

            while (nextCommand is not null)
            {
                parentCommands.Add(nextCommand);
                nextCommand = nextCommand.Parents.FirstOrDefault(c => c is Command) as Command;
            }
            parentCommands.Reverse();

            foreach (Command parentCommand in parentCommands)
            {
                yield return(parentCommand.Name);
            }
            if (showArguments)
            {
                yield return(CommandLineUtils.FormatArgumentUsage(command.Arguments.ToArray()));
            }

            if (showSubcommands)
            {
                yield return(context.HelpBuilder.LocalizationResources.HelpUsageCommand());
            }

            if (showOptions)
            {
                yield return(context.HelpBuilder.LocalizationResources.HelpUsageOptions());
            }
        }