Ejemplo n.º 1
0
        private void RunAsSubcommand(CommandContext context)
        {
            if (this.GetCommand(helpCommand.Name) == null && !HideHelp)
            {
                Commands.Add(helpCommand);
                if (!this.HasFlag(helpFlag))
                {
                    Flags.Add(helpFlag);
                }
            }

            var flagSet = new FlagSet(Name);

            foreach (var flag in Flags)
            {
                flag.Apply(flagSet);
            }

            flagSet.Parse(context.Args().Skip(1).ToArray());
            Flags.NormalizeFlags(flagSet);

            var subContext = new CommandContext(this, flagSet, context);
            //var ctxArgs = subContext.Args();
            var ctxArgs = subContext.Args();

            if (ctxArgs.Any())
            {
                var name = ctxArgs.First();
                var c    = this.GetCommand(name);
                if (c != null)
                {
                    if (c.Flags.Any())
                    {
                        foreach (var flag in c.Flags)
                        {
                            flag.Apply(flagSet);
                        }

                        flagSet.Parse(subContext.Args().ToArray());
                        Flags.NormalizeFlags(flagSet);
                    }
                    subContext.ConsoleCommand = c;
                    if (!CheckHelp(subContext, c))
                    {
                        c.Action(subContext);
                    }
                    return;
                }
                else
                {
                    flagSet.Set("help", true);
                    CheckHelp(subContext, context.ConsoleCommand);
                    return;
                }
            }

            Action(context);
        }
Ejemplo n.º 2
0
        public void Run(params string[] args)
        {
            if (!string.IsNullOrEmpty(Author) || !string.IsNullOrEmpty(Email))
            {
                Authors.Add(new Author {
                    Name = Author, Email = Email
                });
            }

            foreach (var command in Commands)
            {
                if (string.IsNullOrEmpty(command.HelpName))
                {
                    command.HelpName = $"{HelpName} {command.Name}";
                }
            }

            if (this.GetCommand(clearCommand.Name) == null)
            {
                Commands.Add(clearCommand);
            }

            if (this.GetCommand(helpCommand.Name) == null && !HideHelp)
            {
                Commands.Add(helpCommand);

                if (!this.HasFlag(helpFlag))
                {
                    Flags.Add(helpFlag);
                }
            }

            if (this.GetCommand(versionCommand.Name) == null && !HideVersion)
            {
                Commands.Add(versionCommand);
            }

            var flagSet = new FlagSet(Name);

            foreach (var flag in Flags)
            {
                flag.Apply(flagSet);
            }

            flagSet.Parse(args.ToArray());
            Flags.NormalizeFlags(flagSet);

            var context = new CommandContext(this, flagSet);
            var ctxArgs = context.Args();

            if (ctxArgs.Any())
            {
                var name = args.First();

                var c = this.GetCommand(name);
                if (c != null)
                {
                    if (c.Flags.Any())
                    {
                        foreach (var flag in c.Flags)
                        {
                            flag.Apply(flagSet);
                        }

                        flagSet.Parse(context.Args().ToArray());
                        Flags.NormalizeFlags(flagSet);
                    }

                    context.ConsoleCommand = c;
                    if (c.Subcommands.Any())
                    {
                        new Application(this, c, c.Subcommands).RunAsSubcommand(context);
                        return;
                    }

                    if (!CheckHelp(context, c))
                    {
                        c?.Action(context);
                    }
                }
            }
            else
            {
                Action(context);
            }
        }