[Mono.Options] Add option header support.
[mono.git] / mcs / class / Mono.Options / Documentation / en / examples / bundling.cs
1 using System;
2 using System.Linq;
3 using System.Collections.Generic;
4 using Mono.Options;
5
6 class Test {
7         public static void Main (string[] args)
8         {
9                 var show_help = false;
10                 var macros = new Dictionary<string, string>();
11                 bool create = false, extract = false, list = false;
12                 string output = null, input = null;
13                 string color  = null;
14
15                 var p = new OptionSet () {
16                         "Usage: bundling [OPTIONS]+",
17                         "Demo program to show the effects of bundling options and their values",
18                         "",
19                         "gcc-like options:",
20                         { "D:", "Predefine a macro with an (optional) value.",
21                                 (m, v) => {
22                                         if (m == null)
23                                                 throw new OptionException ("Missing macro name for option -D.", 
24                                                                 "-D");
25                                         macros.Add (m, v);
26                                 } },
27                         { "d={-->}{=>}", "Alternate macro syntax.", 
28                                 (m, v) => macros.Add (m, v) },
29                         { "o=", "Specify the output file", v => output = v },
30
31                         "",
32                         "tar-like options:",
33                         { "f=", "The input file",   v => input = v },
34                         { "x",  "Extract the file", v => extract = v != null },
35                         { "c",  "Create the file",  v => create = v != null },
36                         { "t",  "List the file",    v => list = v != null },
37
38                         "",
39                         "ls-like optional values:",
40                         { "color:", "control whether and when color is used", 
41                                 v => color = v },
42
43                         "",
44                         "other:",
45                         { "h|help",  "show this message and exit", 
46                           v => show_help = v != null },
47                         // default
48                         { "<>",
49                                 v => Console.WriteLine ("def handler: color={0}; arg={1}", color, v)},
50                 };
51
52                 try {
53                         p.Parse (args);
54                 }
55                 catch (OptionException e) {
56                         Console.Write ("bundling: ");
57                         Console.WriteLine (e.Message);
58                         Console.WriteLine ("Try `greet --help' for more information.");
59                         return;
60                 }
61
62                 if (show_help) {
63                         p.WriteOptionDescriptions (Console.Out);
64                         return;
65                 }
66
67                 Console.WriteLine ("Macros:");
68                 foreach (var m in (from k in macros.Keys orderby k select k)) {
69                         Console.WriteLine ("\t{0}={1}", m, macros [m] ?? "<null>");
70                 }
71                 Console.WriteLine ("Options:");
72                 Console.WriteLine ("\t Input File: {0}", input);
73                 Console.WriteLine ("\tOuptut File: {0}", output);
74                 Console.WriteLine ("\t     Create: {0}", create);
75                 Console.WriteLine ("\t    Extract: {0}", extract);
76                 Console.WriteLine ("\t       List: {0}", list);
77                 Console.WriteLine ("\t      Color: {0}", color ?? "<null>");
78         }
79 }
80