Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OptionList.cs « Mono.GetOptions « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 40751e656a79471f4365ea3a1faba2b898caa1db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// OptionList.cs
//
// Author: Rafael Teixeira (rafaelteixeirabr@hotmail.com)
//
// (C) 2002 Rafael Teixeira
//

using System;
using System.Collections;
using System.IO;
using System.Reflection;

namespace Mono.GetOptions
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple=true)]
	public class AuthorAttribute : System.Attribute
	{
		public string Name;
		public string SubProject;

		public AuthorAttribute(string Name)
		{
			this.Name = Name;
			this.SubProject = null;
		}

		public AuthorAttribute(string Name, string SubProject)
		{
			this.Name = Name;
			this.SubProject = SubProject;
		}

		public override string ToString()
		{
			if (SubProject == null)
				return Name;
			else
				return Name + " (" + SubProject + ")"; 
		}
	}

	enum OptionParameterType
	{
		None,
		Integer,
		Decimal, // look XML Schemas for better names
		String,
		Symbol,
		FilePath,
		FileMask,
		AssemblyName,
		AssemblyFileName,
		AssemblyNameOrFileName
	}
		
	public delegate bool OptionFound(object Value);

	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class OptionList
	{
		struct OptionDetails
		{
			public char ShortForm;
			public string LongForm;
			public string ShortDescription;
			public OptionParameterType ParameterType;
			public int MinOccurs;
			public int MaxOccurs; // negative means there is no limit
			public object DefaultValue;
			public OptionFound Dispatcher;

		}
	
		private string appTitle = "Add a [assembly: AssemblyTitle(\"Here goes the application name\")] to your assembly";
		private string appCopyright = "Add a [assembly: AssemblyCopyright(\"(c)200n Here goes the copyright holder name\")] to your assembly";
		private string appDescription = "Add a [assembly: AssemblyDescription(\"Here goes the short description\")] to your assembly";
		private string[] appAuthors;
 
		public readonly string usageFormat;
		public readonly string aboutDetails;

		private SortedList list = new SortedList();

		private object[] GetAssemblyAttributes(Type type)
		{
			Assembly entry = Assembly.GetEntryAssembly();
			return entry.GetCustomAttributes(type, false);
		}
			
		private string[] GetAssemblyAttributeStrings(Type type)
		{
			object[] result = GetAssemblyAttributes(type);
			
			if ((result == null) || (result.Length == 0))
				return new string[0];

			int i = 0;
			string[] var = new string[result.Length];

			foreach(object o in result)
				var[i++] = o.ToString(); 

			return var;
		}

		private void GetAssemblyAttributeValue(Type type, string propertyName, ref string var)
		{
			object[] result = GetAssemblyAttributes(type);
			
			if ((result != null) && (result.Length > 0))
				var = (string)(string)type.InvokeMember(propertyName, BindingFlags.Public | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Instance, null, result[0], new object [] {}); ;
		}

		public OptionList(string aboutDetails, string usageFormat)
		{
			this.aboutDetails = aboutDetails;
			this.usageFormat = usageFormat;

			GetAssemblyAttributeValue(typeof(AssemblyTitleAttribute), "Title", ref appTitle);
			GetAssemblyAttributeValue(typeof(AssemblyCopyrightAttribute), "Copyright", ref appCopyright);
			GetAssemblyAttributeValue(typeof(AssemblyDescriptionAttribute), "Description", ref appDescription);
			appAuthors = GetAssemblyAttributeStrings(typeof(AuthorAttribute));
			if (appAuthors.Length == 0)
			{
				appAuthors = new String[1];
				appAuthors[0] = "Add one or more [assembly: Mono.GetOptions.Author(\"Here goes the author name\")] to your assembly";
			}
		}

		private void AddGenericOption(
			char shortForm, 
			string longForm, 
			string shortDescription,
			OptionParameterType parameterType, 
			int minOccurs, 
			int maxOccurs, 
			object defaultValue, 
			OptionFound dispatcher)
		{
			OptionDetails option = new OptionDetails();

			option.ShortForm = shortForm;
			option.LongForm = longForm;
			option.ShortDescription = shortDescription;
			option.ParameterType = parameterType;
			option.MinOccurs = minOccurs;
			option.MaxOccurs = maxOccurs;
			option.DefaultValue = defaultValue;
			option.Dispatcher = dispatcher;

			if (shortForm == ' ')
				list.Add(longForm, option);
			else
				list.Add(shortForm.ToString(), option);
		}

		public void AddAbout(char shortForm, string longForm, string shortDescription)
		{
			AddGenericOption(shortForm, longForm, shortDescription, OptionParameterType.None, 0, 1, null, new OptionFound(DoAbout));
		}

		public void AddBooleanSwitch(char shortForm, string longForm, string shortDescription, bool defaultValue, OptionFound switcher)
		{
			AddGenericOption(shortForm, longForm, shortDescription, OptionParameterType.None, 0, 1, defaultValue, switcher);
		}

		public void ShowAbout()
		{
			Console.WriteLine(appTitle + " - " + appCopyright); 
			Console.WriteLine(appDescription); 
			Console.WriteLine();
			Console.WriteLine(aboutDetails); 
			Console.WriteLine();
			Console.WriteLine("Authors:");
			foreach(string s in appAuthors)
				Console.WriteLine ("\t" + s);
		}

		private bool DoAbout(object nothing)
		{
			ShowAbout();
			return true;
		}

		public void ShowUsage()
		{
			Console.WriteLine(appTitle + " - " + appCopyright); 
			Console.Write("Usage: ");
			Console.WriteLine(usageFormat);
			// TODO: list registered options here
			foreach (DictionaryEntry option in list)
				Console.WriteLine(option.Value.ToString());
		}

		public void ShowUsage(string errorMessage)
		{
			Console.WriteLine(errorMessage);
			ShowUsage();
		}

		public void ProcessArgs(string[] args)
		{
			ShowAbout();
		}
	}
}