@@ -2,10 +2,12 @@ namespace GitVersion
22{
33 using System ;
44 using System . Collections . Generic ;
5+ using System . Collections . Specialized ;
56 using System . Linq ;
67 using System . Reflection ;
8+ using System . Text . RegularExpressions ;
9+
710
8-
911 public class ArgumentParser
1012 {
1113 static ArgumentParser ( )
@@ -66,11 +68,23 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
6668 arguments . TargetPath = firstArgument ;
6769 namedArguments = commandLineArguments . Skip ( 1 ) . ToList ( ) ;
6870 }
71+
72+ var args = CollectSwitchesAndValuesFromArguments ( namedArguments ) ;
6973
70- for ( var index = 0 ; index < namedArguments . Count ; index = index + 2 )
74+ foreach ( var name in args . AllKeys )
7175 {
72- var name = namedArguments [ index ] ;
73- var value = namedArguments . Count > index + 1 ? namedArguments [ index + 1 ] : null ;
76+ var values = args . GetValues ( name ) ;
77+
78+ string value = null ;
79+
80+ if ( values != null )
81+ {
82+ //Currently, no arguments use more than one value, so having multiple values is an input error.
83+ //In the future, this exception can be removed to support multiple values for a switch.
84+ if ( values . Length > 1 ) throw new WarningException ( string . Format ( "Could not parse command line parameter '{0}'." , values [ 1 ] ) ) ;
85+
86+ value = values . FirstOrDefault ( ) ;
87+ }
7488
7589 if ( IsSwitch ( "l" , name ) )
7690 {
@@ -161,8 +175,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
161175 }
162176 else
163177 {
164- arguments . UpdateAssemblyInfo = true ;
165- index -- ;
178+ arguments . UpdateAssemblyInfo = true ;
166179 }
167180 continue ;
168181 }
@@ -190,8 +203,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
190203 }
191204 else
192205 {
193- arguments . ShowConfig = true ;
194- index -- ;
206+ arguments . ShowConfig = true ;
195207 }
196208 continue ;
197209 }
@@ -208,15 +220,54 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
208220 continue ;
209221 }
210222
223+ if ( IsSwitch ( "nofetch" , name ) )
224+ {
225+ arguments . NoFetch = true ;
226+ continue ;
227+ }
228+
211229 throw new WarningException ( string . Format ( "Could not parse command line parameter '{0}'." , name ) ) ;
212230 }
213231
214232 return arguments ;
215233 }
216234
235+ static NameValueCollection CollectSwitchesAndValuesFromArguments ( List < string > namedArguments )
236+ {
237+ var args = new NameValueCollection ( ) ;
238+
239+ string currentKey = null ;
240+ for ( var index = 0 ; index < namedArguments . Count ; index = index + 1 )
241+ {
242+ var arg = namedArguments [ index ] ;
243+ //If this is a switch, create new name/value entry for it, with a null value.
244+ if ( IsSwitchArgument ( arg ) )
245+ {
246+ currentKey = arg ;
247+ args . Add ( currentKey , null ) ;
248+ }
249+ //If this is a value (not a switch)
250+ else
251+ {
252+ //And if the current switch does not have a value yet, set it's value to this argument.
253+ if ( String . IsNullOrEmpty ( args [ currentKey ] ) )
254+ {
255+ args [ currentKey ] = arg ;
256+ }
257+ //Otherwise add the value under the same switch.
258+ else
259+ {
260+ args . Add ( currentKey , arg ) ;
261+ }
262+ }
263+ }
264+ return args ;
265+ }
266+
217267 static bool IsSwitchArgument ( string value )
218268 {
219- return value != null && ( value . StartsWith ( "-" ) || value . StartsWith ( "/" ) ) ;
269+ return value != null && ( value . StartsWith ( "-" ) || value . StartsWith ( "/" ) )
270+ && ! Regex . Match ( value , @"/\w+:" ) . Success ; //Exclude msbuild & project parameters in form /blah:, which should be parsed as values, not switch names.
220271 }
221272
222273 static bool IsSwitch ( string switchName , string value )
0 commit comments