Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

Commit f3112ed

Browse files
dresdorstajs
authored andcommitted
Updated handling of arguments (#68)
* Updated handling of arguments Updated handling of arguments to avoid issues with spaces. * updated from code review updated from code review suggestions * added space to combined args args tokenizes on spaces, so if two args are combined there should be a space between them. * Update Args.cs
1 parent de47bc2 commit f3112ed

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

src/SpecFlow.NetCore/Args.cs

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using static System.Console;
@@ -21,59 +22,74 @@ public Args(string[] args)
2122
{
2223
WorkingDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
2324

25+
// Command line arguments are required. If absent, exit.
2426
if (args == null || !args.Any())
27+
{
2528
return;
29+
}
2630

27-
// Very basic arg parsing:
28-
// - Assume odd elements are arg names.
29-
// - Assume even elements are arg values.
30-
// - Paths with spaces will probably blow up.
31-
// - Duplicates, last one wins.
32-
33-
for (var i = 0; i < args.Length; i++)
31+
// establish a dictionary of all good command line variables
32+
var argDictionary = new Dictionary<string, string>()
3433
{
35-
if (IsOdd(i))
36-
continue;
34+
{SpecFlowPathArgName , null},
35+
{WorkingDirectoryArgName, null },
36+
{TestFrameworkArgName, null },
37+
{ToolsVersionArgName, null }
38+
};
3739

38-
if (i + 1 >= args.Length)
39-
throw new Exception("Uneven arguments");
4040

41-
var name = args[i];
42-
var value = args[i + 1];
41+
string lastKey = null;
42+
// loop through all arguments, attempting to detect and fix bad values
43+
foreach(string arg in args)
44+
{
45+
if (argDictionary.ContainsKey(arg))
46+
{
47+
lastKey = arg;
48+
}
49+
else if (arg.StartsWith("--") || string.IsNullOrEmpty(lastKey))
50+
{
51+
// We are making the assumption that anything starting with -- is intentionally an argument key.
52+
// If that argument key is not in the dictionary, we know it is a bad argument.
53+
// Additionally, if the first argument key is not in our dictionary, the arguments are bad.
54+
throw new Exception("Unknown argument: " + arg);
55+
}
56+
else
57+
{
58+
argDictionary[lastKey] = string.IsNullOrEmpty(argDictionary[lastKey]) ? arg : argDictionary[lastKey] + " " + arg;
59+
}
60+
}
4361

44-
switch (name)
62+
foreach (var key in argDictionary.Keys)
63+
{
64+
switch (key)
4565
{
4666
case SpecFlowPathArgName:
47-
SpecFlowPath = value;
67+
SpecFlowPath = argDictionary[key];
4868
break;
4969

5070
case WorkingDirectoryArgName:
51-
if (!Directory.Exists(value))
52-
throw new Exception("Working directory doesn't exist: " + value);
53-
WorkingDirectory = new DirectoryInfo(value);
71+
var path = string.IsNullOrEmpty(argDictionary[key]) ? Directory.GetCurrentDirectory() : argDictionary[key];
72+
if (!Directory.Exists(path))
73+
{
74+
throw new Exception("Working directory doesn't exist: " + path);
75+
}
76+
WorkingDirectory = new DirectoryInfo(path);
5477
break;
5578

5679
case TestFrameworkArgName:
57-
TestFramework = value;
80+
TestFramework = argDictionary[key];
5881
break;
5982

6083
case ToolsVersionArgName:
61-
ToolsVersion = value;
84+
ToolsVersion = argDictionary[key];
6285
break;
63-
64-
default:
65-
throw new Exception("Unknown argument: " + name);
6686
}
6787
}
6888

6989
WriteLine("SpecFlowPath: " + SpecFlowPath);
7090
WriteLine("WorkingDirectory: " + WorkingDirectory.FullName);
7191
WriteLine("TestFramework: " + TestFramework);
72-
}
73-
74-
private bool IsOdd(int i)
75-
{
76-
return i % 2 != 0;
92+
WriteLine("ToolsVersion: " + ToolsVersion);
7793
}
7894
}
79-
}
95+
}

0 commit comments

Comments
 (0)