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); }
public void TestSetOutput() { var flags = new FlagSet("test", ErrorHandling.ContinueOnError); var writer = new StringWriter(); flags.SetOutput(writer); flags.Parse(new[] { "-unknown" }); var output = writer.ToString(); Assert.True(output.Contains("-unknown"), $"expected output mentioning unknown; got \"{output}\""); }
private void PrivateTestParse(FlagSet f) { Assert.False(f.Parsed, "f.Parse = true before Parse"); var boolFlag = f.Bool("bool", false, "bool value"); var bool2Flag = f.Bool("bool2", false, "bool2 value"); var intFlag = f.Int("int", 0, "int value"); var int64Flag = f.Int64("int64", 0, "int64 value"); var uintFlag = f.Uint("uint", 0, "uint value"); var uint64Flag = f.Uint64("uint64", 0, "uint64 value"); var stringFlag = f.String("string", "0", "string value"); var float64Flag = f.Float("float64", 0, "float64 value"); var durationFlag = f.Duration("duration", 5 * TimeSpan.FromSeconds(1), "time.Duration value"); var extra = "one-extra-argument"; var args = new[] { "-bool", "-bool2=true", "--int", "22", "--int64", "0x23", "-uint", "24", "--uint64", "25", "-string", "hello", "-float64", "2718e28", "-duration", "00:02:00", extra }; var error = f.Parse(args); Assert.True(error == null, $"error parsing: {error}"); Assert.True(f.Parsed, "f.Parse() = false after Parse"); Assert.True(boolFlag, $"bool flag should be true, but is {boolFlag}"); Assert.True(bool2Flag, $"bool2 flag should be true, is {bool2Flag}"); Assert.True(intFlag == 22, $"int flag should be 22, is {intFlag}"); Assert.True(int64Flag == 0x23, $"int64 flag should be 0x23, is {int64Flag}"); Assert.True(uintFlag == 24, $"uint flag should be 24, is {uintFlag}"); Assert.True(uint64Flag == 25, $"uint64 flag should be 25, is {uint64Flag}"); Assert.True(stringFlag == "hello", $"string flag should be `hello`, is {stringFlag}"); Assert.True(AlmostEqual2sComplement(float64Flag, (float)2718e28, 1), $"float64 flag should be 2718e28, is {float64Flag}"); Assert.True(durationFlag == TimeSpan.FromMinutes(2), $"duration flag should be 2m, is {durationFlag}"); Assert.True(f.Args.Length == 1, $"expected one argument, got {f.Args.Length}"); Assert.True(f.Args[0] == extra, $"expected argument \"{extra}\" got {f.Args[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); } }