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); }
internal static void NormalizeFlags(this IEnumerable <Flag.Flag> flags, FlagSet flagSet) { foreach (var flag in flags) { var parts = flag.Name.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 1) { if (!flagSet.Actual.ContainsKey(parts[0])) { flagSet.Set(parts[0], flag.Value); } continue; } Flag.Flag f = null; foreach (var part in parts) { var name = part.Trim(); if (flagSet.Actual.ContainsKey(name)) { if (f != null) { throw new BadSyntaxException($"Can't use two forms of the same flag: {name} {f.Name}"); } f = flagSet.Formal[name]; } } if (f == null) { continue; } foreach (var part in parts) { var name = part.Trim(); if (!flagSet.Actual.ContainsKey(name)) { flagSet.Set(name, f.Value); } } } }