1 // Sub-commands with Mono.Options.CommandSet
4 // mcs -r:Mono.Options.dll commands.cs
7 using System.Collections.Generic;
12 public static int Main (string[] args)
14 var commands = new CommandSet ("commands") {
15 "usage: commands COMMAND [OPTIONS]",
17 "Mono.Options.CommandSet sample app.",
22 (int? n) => Verbosity = n.HasValue ? n.Value : Verbosity + 1 },
24 "Available commands:",
25 new Command ("echo", "Echo arguments to the screen") {
26 Run = ca => Console.WriteLine ("{0}", string.Join (" ", ca)),
30 return commands.Run (args);
33 public static int Verbosity;
36 class RequiresArgs : Command {
38 public RequiresArgs ()
39 : base ("requires-args", "Class-based Command subclass")
41 Options = new OptionSet () {
42 "usage: commands requires-args [OPTIONS]",
44 "Class-based Command subclass example.",
46 "{name} of person to greet.",
49 "Show this message and exit.",
50 v => ShowHelp = v != null },
54 public bool ShowHelp {get; private set;}
55 public new string Name {get; private set;}
57 public override int Invoke (IEnumerable<string> args)
60 var extra = Options.Parse (args);
62 Options.WriteOptionDescriptions (CommandSet.Out);
65 if (string.IsNullOrEmpty (Name)) {
66 Console.Error.WriteLine ("commands: Missing required argument `--name=NAME`.");
67 Console.Error.WriteLine ("commands: Use `commands help requires-args` for details.");
70 Console.WriteLine ($"Hello, {Name}!");
74 Console.Error.WriteLine ("commands: {0}", CommandDemo.Verbosity >= 1 ? e.ToString () : e.Message);