[Mono.Options] Add Mono.Options.Command, .CommandSet
[mono.git] / mcs / class / Mono.Options / Documentation / en / examples / commands.cs
1 // Sub-commands with Mono.Options.CommandSet
2 //
3 // Compile as:
4 //   mcs -r:Mono.Options.dll commands.cs
5
6 using System;
7 using System.Collections.Generic;
8
9 using Mono.Options;
10
11 class CommandDemo {
12         public static int Main (string[] args)
13         {
14                 var commands = new CommandSet ("commands") {
15                         "usage: commands COMMAND [OPTIONS]",
16                         "",
17                         "Mono.Options.CommandSet sample app.",
18                         "",
19                         "Global options:",
20                         { "v:",
21                           "Output verbosity.",
22                           (int? n) => Verbosity = n.HasValue ? n.Value : Verbosity + 1 },
23                         "",
24                         "Available commands:",
25                         new Command ("echo", "Echo arguments to the screen") {
26                                 Run = ca => Console.WriteLine ("{0}", string.Join (" ", ca)),
27                         },
28                         new RequiresArgs (),
29                 };
30                 return commands.Run (args);
31         }
32
33         public static int Verbosity;
34 }
35
36 class RequiresArgs : Command {
37
38         public RequiresArgs ()
39                 : base ("requires-args", "Class-based Command subclass")
40         {
41                 Options = new OptionSet () {
42                         "usage: commands requires-args [OPTIONS]",
43                         "",
44                         "Class-based Command subclass example.",
45                         { "name|n=",
46                           "{name} of person to greet.",
47                           v => Name = v },
48                         { "help|h|?",
49                           "Show this message and exit.",
50                           v => ShowHelp = v != null },
51                 };
52         }
53
54         public        bool    ShowHelp    {get; private set;}
55         public  new   string  Name        {get; private set;}
56
57         public override int Invoke (IEnumerable<string> args)
58         {
59                 try {
60                         var extra = Options.Parse (args);
61                         if (ShowHelp) {
62                                 Options.WriteOptionDescriptions (CommandSet.Out);
63                                 return 0;
64                         }
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.");
68                                 return 1;
69                         }
70                         Console.WriteLine ($"Hello, {Name}!");
71                         return 0;
72                 }
73                 catch (Exception e) {
74                         Console.Error.WriteLine ("commands: {0}", CommandDemo.Verbosity >= 1 ? e.ToString () : e.Message);
75                         return 1;
76                 }
77         }
78 }