2 // Parameters.cs: Class that contains information about command line parameters
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Collections
;
34 using System
.Reflection
;
35 using Microsoft
.Build
.BuildEngine
;
36 using Microsoft
.Build
.Framework
;
37 using Microsoft
.Build
.Utilities
;
39 namespace Mono
.XBuild
.CommandLine
{
40 public class Parameters
{
42 string consoleLoggerParameters
;
47 LoggerVerbosity loggerVerbosity
;
51 BuildPropertyGroup properties
;
52 IList remainingArguments
;
53 Hashtable responseFiles
;
56 string validationSchema
;
60 public Parameters (string binPath
)
62 consoleLoggerParameters
= "";
64 displayVersion
= true;
65 loggers
= new ArrayList ();
66 loggerVerbosity
= LoggerVerbosity
.Normal
;
67 noConsoleLogger
= false;
69 properties
= new BuildPropertyGroup ();
70 targets
= new string [0];
72 responseFile
= Path
.Combine (
73 Path
.GetDirectoryName (Assembly
.GetExecutingAssembly ().Location
),
77 public void ParseArguments (string[] args
)
79 bool autoResponse
= true;
80 flatArguments
= new ArrayList ();
81 remainingArguments
= new ArrayList ();
82 responseFiles
= new Hashtable ();
83 foreach (string s
in args
) {
84 if (s
.StartsWith ("/noautoresponse") || s
.StartsWith ("/noautorsp")) {
89 flatArguments
.Add (s
);
92 string responseFilename
= Path
.GetFullPath (s
.Substring (1));
93 if (responseFiles
.ContainsKey (responseFilename
))
94 ErrorUtilities
.ReportError (1, String
.Format ("We already have {0} file.", responseFilename
));
95 responseFiles
[responseFilename
] = responseFilename
;
96 LoadResponseFile (responseFilename
);
98 if (autoResponse
== true) {
99 // FIXME: we do not allow nested auto response file
100 LoadResponseFile (responseFile
);
102 foreach (string s
in flatArguments
) {
103 if (s
[0] != '/' || !ParseFlatArgument (s
))
104 remainingArguments
.Add (s
);
106 if (remainingArguments
.Count
== 0) {
107 string[] files
= Directory
.GetFiles (Directory
.GetCurrentDirectory (), "*.??proj");
108 if (files
.Length
> 0)
109 projectFile
= files
[0];
111 ErrorUtilities
.ReportError (3, "No .proj file specified and no found in current directory.");
112 } else if (remainingArguments
.Count
== 1) {
113 projectFile
= (string) remainingArguments
[0];
115 ErrorUtilities
.ReportError (4, "Too many project files specified");
119 void LoadResponseFile (string filename
)
121 StreamReader sr
= null;
124 sr
= new StreamReader (filename
);
125 StringBuilder sb
= new StringBuilder ();
127 while ((line
= sr
.ReadLine ()) != null) {
130 for (int i
= 0; i
< t
; i
++) {
134 // comment, ignore rest of the line
137 if (c
== '"' || c
== '\'') {
140 for (i
++; i
< t
; i
++) {
147 } else if (c
== ' ') {
149 flatArguments
.Add (sb
.ToString ());
156 flatArguments
.Add (sb
.ToString ());
160 } catch (Exception
) {
161 // FIXME: we lose exception message
162 ErrorUtilities
.ReportError (2, "Error during loading response file.");
169 private bool ParseFlatArgument (string s
)
175 ErrorUtilities
.ShowUsage ();
182 ErrorUtilities
.ShowVersion (true);
184 case "/noconsolelogger":
186 noConsoleLogger
= true;
193 if (s
.StartsWith ("/target:") || s
.StartsWith ("/t:")) {
195 } else if (s
.StartsWith ("/property:") || s
.StartsWith ("/p:")) {
196 if (!ProcessProperty (s
))
198 } else if (s
.StartsWith ("/logger:") || s
.StartsWith ("/l:")) {
200 } else if (s
.StartsWith ("/verbosity:") || s
.StartsWith ("/v:")) {
201 ProcessVerbosity (s
);
202 } else if (s
.StartsWith ("/consoleloggerparameters:") || s
.StartsWith ("/clp:")) {
203 ProcessConsoleLoggerParameters (s
);
204 } else if (s
.StartsWith ("/validate:") || s
.StartsWith ("/val:")) {
214 internal void ProcessTarget (string s
)
216 string[] temp
= s
.Split (':');
217 targets
= temp
[1].Split (';');
220 internal bool ProcessProperty (string s
)
222 string[] parameter
, splittedProperties
, property
;
223 parameter
= s
.Split (':');
224 if (parameter
.Length
!= 2) {
225 ErrorUtilities
.ReportError (5, "Property name and value expected as /p:<prop name>=<prop value>");
229 splittedProperties
= parameter
[1].Split (';');
230 foreach (string st
in splittedProperties
) {
231 if (st
.IndexOf ('=') < 0) {
232 ErrorUtilities
.ReportError (5,
233 "Invalid syntax. Property name and value expected as " +
234 "<prop name>=[<prop value>]");
237 property
= st
.Split ('=');
238 properties
.SetProperty (property
[0], property
.Length
== 2 ? property
[1] : "");
244 internal void ProcessLogger (string s
)
246 loggers
.Add (new LoggerInfo (s
));
249 internal void ProcessVerbosity (string s
)
251 string[] temp
= s
.Split (':');
255 loggerVerbosity
= LoggerVerbosity
.Quiet
;
259 loggerVerbosity
= LoggerVerbosity
.Minimal
;
263 loggerVerbosity
= LoggerVerbosity
.Normal
;
267 loggerVerbosity
= LoggerVerbosity
.Detailed
;
271 loggerVerbosity
= LoggerVerbosity
.Diagnostic
;
276 internal void ProcessConsoleLoggerParameters (string s
)
278 consoleLoggerParameters
= s
;
281 internal void ProcessValidate (string s
)
285 temp
= s
.Split (':');
286 validationSchema
= temp
[1];
288 public bool DisplayHelp
{
289 get { return displayHelp; }
293 get { return noLogo; }
296 public bool DisplayVersion
{
297 get { return displayVersion; }
300 public string ProjectFile
{
301 get { return projectFile; }
304 public string[] Targets
{
305 get { return targets; }
308 public BuildPropertyGroup Properties
{
309 get { return properties; }
312 public IList Loggers
{
313 get { return loggers; }
316 public LoggerVerbosity LoggerVerbosity
{
317 get { return loggerVerbosity; }
320 public string ConsoleLoggerParameters
{
321 get { return consoleLoggerParameters; }
324 public bool NoConsoleLogger
{
325 get { return noConsoleLogger; }
328 public bool Validate
{
329 get { return validate; }
332 public string ValidationSchema
{
333 get { return validationSchema; }