Esempio n. 1
0
        public Command(
            string name,
            string description = "",
            IReadOnlyCollection <Symbol> symbols = null,
            Argument argument = null,
            bool treatUnmatchedTokensAsErrors = true,
            ICommandHandler handler           = null,
            IHelpBuilder helpBuilder          = null,
            bool isHidden = false) :
            base(new[] { name }, description, isHidden: isHidden)
        {
            TreatUnmatchedTokensAsErrors = treatUnmatchedTokensAsErrors;
            Handler     = handler;
            HelpBuilder = helpBuilder;
            symbols     = symbols ?? Array.Empty <Symbol>();

            Argument = argument ??
                       new Argument
            {
                Arity = ArgumentArity.Zero
            };

            foreach (var symbol in symbols)
            {
                AddSymbol(symbol);
            }
        }
Esempio n. 2
0
        public static int Run(IHost host, InvocationContext context, IHelpBuilder help)
        {
            var log = host.Services.GetRequiredService <ILogger <MainCommand> >();

            log.LogDebug("Run main");

            help.Write(context.ParseResult.CommandResult.Command);

            return(ExitCodes.Success);
        }
Esempio n. 3
0
        /// <summary>
        /// Get help builder interface
        /// </summary>
        /// <param name="helpBuilder">help builder</param>
        public Task InvokeAsync(IHelpBuilder helpBuilder)
        {
            Command command = CommandProcessor.GetCommand(Command);

            if (command != null)
            {
                helpBuilder.Write(command);
            }
            else
            {
                Console.Error.WriteLine($"Help for {Command} not found.");
            }
            return(Task.CompletedTask);
        }
Esempio n. 4
0
 public RootCommand(
     string description = "",
     IReadOnlyCollection <Symbol> symbols = null,
     Argument argument = null,
     bool treatUnmatchedTokensAsErrors = true,
     ICommandHandler handler           = null,
     IHelpBuilder helpBuilder          = null,
     bool isHidden = false) :
     base(ExeName,
          description,
          symbols,
          argument,
          treatUnmatchedTokensAsErrors,
          handler,
          helpBuilder,
          isHidden)
 {
 }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Parser" /> class.
        /// </summary>
        /// <param name="options">The settings used to create the object.</param>
        /// <param name="helpBuilder">The object used to build the help menu.</param>
        /// <exception cref="ArgumentNullException"><see cref="IHelpBuilder"/> is null.</exception>
        public Parser(ParserOptions options, IHelpBuilder helpBuilder)
        {
            _helpBuilder = helpBuilder ?? throw new ArgumentNullException(nameof(helpBuilder));
            _commandList = new Dictionary <string, CommandInfo>();
            _options     = options;

            // Appending internal commands
            if (!options.HasFlag(ParserOptions.ExcludeHelpCommand))
            {
                _commandList.Add(HelpCommand.Id, new HelpCommand());
            }
            if (!options.HasFlag(ParserOptions.ExcludeVersionCommand))
            {
                _commandList.Add(VersionCommand.Id, new VersionCommand());
            }
            if (!options.HasFlag(ParserOptions.ExcludePackageCommand))
            {
                _commandList.Add(PackageCommand.Id, new PackageCommand());
            }
        }
 /// <summary>
 /// Writes help output for the specified command.
 /// </summary>
 /// <param name="builder">The help builder to write with.</param>
 /// <param name="command">The command for which to write help output.</param>
 /// <param name="writer">The writer to write output to.</param>
 public static void Write(
     this IHelpBuilder builder,
     ICommand command,
     TextWriter writer) =>
 builder.Write(command, writer, ParseResult.Empty());
        /// <summary>
        /// Initializes a new instance of the CommandLineConfiguration class.
        /// </summary>
        /// <param name="symbols">The symbols to parse.</param>
        /// <param name="enablePosixBundling"><c>true</c> to enable POSIX bundling; otherwise, <c>false</c>.</param>
        /// <param name="enableDirectives"><c>true</c> to enable directive parsing; otherwise, <c>false</c>.</param>
        /// <param name="validationMessages">Provide custom validation messages.</param>
        /// <param name="responseFileHandling">One of the enumeration values that specifies how response files (.rsp) are handled.</param>
        /// <param name="middlewarePipeline">Provide a custom middleware pipeline.</param>
        /// <param name="helpBuilderFactory">Provide a custom help builder.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="symbols"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="symbols"/> does not contain at least one option or command.</exception>
        public CommandLineConfiguration(
            IReadOnlyList <Symbol> symbols,
            bool enablePosixBundling     = true,
            bool enableDirectives        = true,
            Resources?validationMessages = null,
            ResponseFileHandling responseFileHandling =
            ResponseFileHandling.ParseArgsAsLineSeparated,
            IReadOnlyCollection <InvocationMiddleware>?middlewarePipeline = null,
            Func <BindingContext, IHelpBuilder>?helpBuilderFactory        = null,
            Action <IHelpBuilder>?configureHelp = null
            )
        {
            if (symbols is null)
            {
                throw new ArgumentNullException(nameof(symbols));
            }

            if (symbols.Count == 0)
            {
                throw new ArgumentException("You must specify at least one option or command.");
            }

            if (symbols.Count == 1 && symbols[0] is Command rootCommand)
            {
                RootCommand = rootCommand;
            }
            else
            {
                // Reuse existing auto-generated root command, if one is present, to prevent repeated mutations
                RootCommand?parentRootCommand = symbols
                                                .SelectMany(s => s.Parents)
                                                .OfType <RootCommand>()
                                                .FirstOrDefault();

                if (parentRootCommand is null)
                {
                    parentRootCommand = new RootCommand();

                    foreach (var symbol in symbols)
                    {
                        parentRootCommand.Add(symbol);
                    }
                }

                RootCommand = rootCommand = parentRootCommand;
            }

            _symbols.Add(RootCommand);

            AddGlobalOptionsToChildren(rootCommand);

            EnablePosixBundling  = enablePosixBundling;
            EnableDirectives     = enableDirectives;
            ValidationMessages   = validationMessages ?? Resources.Instance;
            ResponseFileHandling = responseFileHandling;
            Middleware           = middlewarePipeline ?? new List <InvocationMiddleware>();
            HelpBuilderFactory   =
                helpBuilderFactory
                ?? (
                    context =>
            {
                int maxWidth = int.MaxValue;
                if (context.Console is SystemConsole systemConsole)
                {
                    maxWidth = systemConsole.GetWindowWidth();
                }
                return(new HelpBuilder(context.Console, maxWidth));
            }
                    );
            if (configureHelp != null)
            {
                var factory = HelpBuilderFactory;
                HelpBuilderFactory = context =>
                {
                    IHelpBuilder helpBuilder = factory(context);
                    configureHelp(helpBuilder);
                    return(helpBuilder);
                };
            }
        }