Skip to content

Commit bfa3024

Browse files
committed
Command arg is positional again, web is default, some related refactor
1 parent 12f6964 commit bfa3024

File tree

5 files changed

+110
-65
lines changed

5 files changed

+110
-65
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ RUNNING
2323

2424
Run from code with watch
2525
```
26-
dotnet watch run --project .\netmockery\netmockery.csproj --command web --endpoints [YourEndpointFilesPath] --urls http://localhost:9876
26+
dotnet watch run --project .\netmockery\netmockery.csproj web --endpoints [YourEndpointFilesPath] --urls http://localhost:9876
2727
```
2828

2929
Run published .dll
3030
```
3131
dotnet publish .\netmockery.sln
3232
33-
dotnet .\netmockery\bin\Debug\net6.0\publish\netmockery.dll --command web --endpoints [YourEndpointFilesPath] --urls http://localhost:9876
33+
dotnet .\netmockery\bin\Debug\net6.0\publish\netmockery.dll web --endpoints [YourEndpointFilesPath] --urls http://localhost:9876
3434
```
3535

3636
Run published .exe
3737
```
3838
dotnet publish .\netmockery.sln
3939
40-
.\netmockery\bin\Debug\net6.0\publish\netmockery.exe --command web --endpoints [YourEndpointFilesPath] --urls http://localhost:9876
40+
.\netmockery\bin\Debug\net6.0\publish\netmockery.exe web --endpoints [YourEndpointFilesPath] --urls http://localhost:9876
4141
```
4242

4343

UnitTests/TestCommandLineParser.cs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using netmockery;
2+
using System;
23
using Xunit;
34

45
using static netmockery.CommandLineParser;
@@ -8,27 +9,34 @@ namespace UnitTests
89
{
910
public class TestCommandLineParser
1011
{
12+
[Fact]
13+
public void WebCommandAsDefault()
14+
{
15+
var result = ParseArguments(new[] { "--endpoints", "c:\\dir\\foo" });
16+
Assert.Equal(COMMAND_WEB, result.Command);
17+
}
18+
1119
[Fact]
1220
public void WebCommand()
1321
{
14-
var result = ParseArguments(new[] { "--command", "web", "--endpoints", "c:\\dir\\foo" });
22+
var result = ParseArguments(new[] { "web", "--endpoints", "c:\\dir\\foo" });
1523
Assert.Equal(COMMAND_WEB, result.Command);
1624
Assert.Equal("c:\\dir\\foo", result.Endpoints);
1725
Assert.Null(result.Urls);
1826
}
1927

2028
[Fact]
21-
public void WebCommandWithVaryingCasing()
29+
public void WebCommandWithVaryingArgCasing()
2230
{
23-
var result = ParseArguments(new[] { "--COMMAND", "web", "--EndPoints", "c:\\dir\\foo" });
31+
var result = ParseArguments(new[] { "web", "--EndPoints", "c:\\dir\\foo" });
2432
Assert.Equal(COMMAND_WEB, result.Command);
2533
Assert.Equal("c:\\dir\\foo", result.Endpoints);
2634
}
2735

2836
[Fact]
2937
public void WebCommandNoTestMode()
3038
{
31-
var result = ParseArguments(new[] { "--command", "web", "--endpoints", "c:\\dir\\foo", "--notestmode" });
39+
var result = ParseArguments(new[] { "web", "--endpoints", "c:\\dir\\foo", "--notestmode" });
3240
Assert.Equal(COMMAND_WEB, result.Command);
3341
Assert.Equal("c:\\dir\\foo", result.Endpoints);
3442
Assert.True(result.NoTestMode);
@@ -38,43 +46,44 @@ public void WebCommandNoTestMode()
3846
[Fact]
3947
public void WebCommandWithUrls()
4048
{
41-
var result = ParseArguments(new[] { "--command", "web", "--endpoints", "c:\\dir\\foo", "--urls", "http://*:5000" });
49+
var result = ParseArguments(new[] { "web", "--endpoints", "c:\\dir\\foo", "--urls", "http://*:5000" });
4250
Assert.Equal(COMMAND_WEB, result.Command);
4351
Assert.Equal("c:\\dir\\foo", result.Endpoints);
4452
Assert.Equal("http://*:5000", result.Urls);
4553
}
4654

47-
[Fact]
48-
public void MissingCommand()
49-
{
50-
AssertGivesException("Missing required switch --command", new[] { "--endpoints", "c:\\foo\\bar" });
51-
}
52-
5355
[Fact]
5456
public void UnknownCommand()
5557
{
56-
AssertGivesException("Unknown command 'foobar'", new[] { "--command", "foobar", "--endpoints", "c:\\foo\\bar" });
58+
AssertGivesException("Unknown command 'webz'", new[] { "webz", "--endpoints", "c:\\foo\\bar" });
5759
}
5860

5961
[Fact]
6062
public void MissingEndpoints()
6163
{
62-
AssertGivesException("Missing required switch --endpoints", new[] { "--command", "web" });
64+
AssertGivesException("Missing required switch --endpoints", new[] { "web" });
6365
}
6466

6567
[Fact]
6668
public void TestCommand()
6769
{
68-
var result = ParseArguments(new[] { "--command", "test", "--endpoints", "c:\\dir\\foo" });
70+
var result = ParseArguments(new[] { "test", "--endpoints", "c:\\dir\\foo" });
6971
Assert.Equal(COMMAND_TEST, result.Command);
7072
Assert.False(result.ShowResponse);
7173
Assert.Null(result.Only);
7274
}
7375

76+
[Fact]
77+
public void TestCommandWhenUpperCase()
78+
{
79+
var result = ParseArguments(new[] { "TEST", "--endpoints", "c:\\dir\\foo" });
80+
Assert.Equal(COMMAND_TEST, result.Command);
81+
}
82+
7483
[Fact]
7584
public void TestCommandWithOptions()
7685
{
77-
var result = ParseArguments(new[] { "--command", "test", "--endpoints", "c:\\dir\\foo", "--only", "1", "--showresponse" });
86+
var result = ParseArguments(new[] { "test", "--endpoints", "c:\\dir\\foo", "--only", "1", "--showresponse" });
7887
Assert.Equal(COMMAND_TEST, result.Command);
7988
Assert.Equal("1", result.Only);
8089
Assert.True(result.ShowResponse);
@@ -84,7 +93,7 @@ public void TestCommandWithOptions()
8493
[Fact]
8594
public void TestWithStopOption()
8695
{
87-
var result = ParseArguments(new[] { "--command", "test", "--endpoints", "c:\\dir\\foo", "--stop" });
96+
var result = ParseArguments(new[] { "test", "--endpoints", "c:\\dir\\foo", "--stop" });
8897
Assert.Equal(COMMAND_TEST, result.Command);
8998
Assert.Null(result.Only);
9099
Assert.False(result.ShowResponse);
@@ -94,28 +103,34 @@ public void TestWithStopOption()
94103
[Fact]
95104
public void DumpCommand()
96105
{
97-
var result = ParseArguments(new[] { "--command", "dump", "--endpoints", "c:\\foo\\bar" });
106+
var result = ParseArguments(new[] { "dump", "--endpoints", "c:\\foo\\bar" });
98107
Assert.Equal(COMMAND_DUMP, result.Command);
99108
Assert.Equal("c:\\foo\\bar", result.Endpoints);
100109
}
101110

102111
[Fact]
103112
public void InvalidArgumentsForDumpCommand()
104113
{
105-
AssertGivesException("'--only' is not a valid argument for the 'dump' command", new[] { "--command", "dump", "--endpoints", "c:\\foo\\bar", "--only", "2" });
106-
AssertGivesException("'--urls' is not a valid argument for the 'dump' command", new[] { "--command", "dump", "--endpoints", "c:\\foo\\bar", "--urls", "http://localhost:5000/" });
114+
AssertGivesException("'--only' is not a valid argument for the 'dump' command", new[] { "dump", "--endpoints", "c:\\foo\\bar", "--only", "2" });
115+
AssertGivesException("'--urls' is not a valid argument for the 'dump' command", new[] { "dump", "--endpoints", "c:\\foo\\bar", "--urls", "http://localhost:5000/" });
107116
}
108117

109118
[Fact]
110119
public void InvalidArgumentsForWebCommand()
111120
{
112-
AssertGivesException("'--only' is not a valid argument for the 'web' command", new[] { "--command", "web", "--endpoints", "c:\\foo\\bar", "--only", "2" });
121+
AssertGivesException("'--only' is not a valid argument for the 'web' command", new[] { "web", "--endpoints", "c:\\foo\\bar", "--only", "2" });
122+
}
123+
124+
[Fact]
125+
public void NoArguments()
126+
{
127+
AssertGivesException("No arguments", Array.Empty<string>());
113128
}
114129

115130
private void AssertGivesException(string expectedMessage, string[] args)
116131
{
117-
var tx = Assert.Throws<CommandLineParsingException>(() => ParseArguments(args));
118-
Assert.Equal(expectedMessage, tx.Message);
132+
var ex = Assert.Throws<CommandLineParsingException>(() => ParseArguments(args));
133+
Assert.Equal(expectedMessage, ex.Message);
119134
}
120135
}
121136
}

UnitTests/WebTestBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public CustomWebApplicationFactory(EndpointCollectionProvider endpointCollection
1717

1818
protected override void ConfigureWebHost(IWebHostBuilder builder)
1919
{
20-
builder.UseSetting("command", "web");
2120
builder.UseSetting("endpoints", ".."); // EndpointCollectionProvider is mocked anyway
2221

2322
builder.ConfigureServices(services =>

netmockery/CommandLineParser.cs

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Linq;
54

65
namespace netmockery
@@ -12,7 +11,6 @@ static public class CommandLineParser
1211
public const string COMMAND_DUMP = "dump";
1312
public const string COMMAND_DUMPREFS = "dumprefs";
1413

15-
private const string VALUE_SWITCH_COMMAND = "--command";
1614
private const string VALUE_SWITCH_ENDPOINTS = "--endpoints";
1715
private const string VALUE_SWITCH_URLS = "--urls";
1816
private const string VALUE_SWITCH_ONLY = "--only";
@@ -27,20 +25,22 @@ static public class CommandLineParser
2725
private const string BOOL_SWITCH_DIFF = "--diff";
2826
private const string BOOL_SWITCH_LIST = "--list";
2927

30-
static private string[] COMMAND_VALUES = new[] { COMMAND_WEB, COMMAND_TEST, COMMAND_DUMP, COMMAND_DUMPREFS };
31-
static private string[] VALUE_SWITCHES = new[] { VALUE_SWITCH_COMMAND, VALUE_SWITCH_ENDPOINTS, VALUE_SWITCH_URLS, VALUE_SWITCH_ONLY, VALUE_UT_SWITCH_ENVIRONMENT, VALUE_UT_SWITCH_CONTENTROOT, VALUE_UT_SWITCH_APPLICATIONNAME };
28+
static private string[] VALUE_SWITCHES = new[] { VALUE_SWITCH_ENDPOINTS, VALUE_SWITCH_URLS, VALUE_SWITCH_ONLY, VALUE_UT_SWITCH_ENVIRONMENT, VALUE_UT_SWITCH_CONTENTROOT, VALUE_UT_SWITCH_APPLICATIONNAME };
3229
static private string[] BOOL_SWITCHES = new[] { BOOL_SWITCH_SHOWRESPONSE, BOOL_SWITCH_NOTESTMODE, BOOL_SWITCH_STOP, BOOL_SWITCH_DIFF, BOOL_SWITCH_LIST };
3330

3431
static private Dictionary<string, string[]> VALID_SWITHCES_BY_COMMAND = new Dictionary<string, string[]> {
3532

36-
{ COMMAND_WEB, new[] { VALUE_SWITCH_COMMAND, VALUE_SWITCH_ENDPOINTS, VALUE_SWITCH_URLS, BOOL_SWITCH_NOTESTMODE, VALUE_UT_SWITCH_ENVIRONMENT, VALUE_UT_SWITCH_CONTENTROOT, VALUE_UT_SWITCH_APPLICATIONNAME } },
37-
{ COMMAND_TEST, new[] { VALUE_SWITCH_COMMAND, VALUE_SWITCH_ENDPOINTS, VALUE_SWITCH_URLS, VALUE_SWITCH_ONLY, BOOL_SWITCH_SHOWRESPONSE, BOOL_SWITCH_STOP, BOOL_SWITCH_DIFF, BOOL_SWITCH_LIST} },
38-
{ COMMAND_DUMP, new[] { VALUE_SWITCH_COMMAND, VALUE_SWITCH_ENDPOINTS } },
39-
{ COMMAND_DUMPREFS, new[] { VALUE_SWITCH_COMMAND, VALUE_SWITCH_ENDPOINTS } }
33+
{ COMMAND_WEB, new[] { VALUE_SWITCH_ENDPOINTS, VALUE_SWITCH_URLS, BOOL_SWITCH_NOTESTMODE, VALUE_UT_SWITCH_ENVIRONMENT, VALUE_UT_SWITCH_CONTENTROOT, VALUE_UT_SWITCH_APPLICATIONNAME } },
34+
{ COMMAND_TEST, new[] { VALUE_SWITCH_ENDPOINTS, VALUE_SWITCH_URLS, VALUE_SWITCH_ONLY, BOOL_SWITCH_SHOWRESPONSE, BOOL_SWITCH_STOP, BOOL_SWITCH_DIFF, BOOL_SWITCH_LIST} },
35+
{ COMMAND_DUMP, new[] { VALUE_SWITCH_ENDPOINTS } },
36+
{ COMMAND_DUMPREFS, new[] { VALUE_SWITCH_ENDPOINTS } }
4037
};
4138

4239
static public ParsedCommandLine ParseArguments(string[] args)
4340
{
41+
if (args.Length == 0)
42+
throw new CommandLineParsingException("No arguments");
43+
4444
// Technical debt to support multiple arg input formats, should refactor argument parsing to something standard like System.CommandLine
4545
// key=value is converted to [key, value]
4646
args = args
@@ -50,18 +50,70 @@ static public ParsedCommandLine ParseArguments(string[] args)
5050
.ToArray();
5151

5252
// Parsing
53+
(var command, args) = ParseCommand(args);
54+
(var stringSwiches, var boolSwitches) = ParseSwitches(command, args);
55+
56+
// Return
57+
return new ParsedCommandLine
58+
{
59+
Command = command,
60+
Endpoints = stringSwiches[VALUE_SWITCH_ENDPOINTS],
61+
62+
Urls = stringSwiches[VALUE_SWITCH_URLS],
63+
Only = stringSwiches[VALUE_SWITCH_ONLY],
64+
65+
ShowResponse = boolSwitches[BOOL_SWITCH_SHOWRESPONSE],
66+
NoTestMode = boolSwitches[BOOL_SWITCH_NOTESTMODE],
67+
Stop = boolSwitches[BOOL_SWITCH_STOP],
68+
Diff = boolSwitches[BOOL_SWITCH_DIFF],
69+
List = boolSwitches[BOOL_SWITCH_LIST]
70+
};
71+
}
72+
73+
static private (string Command, string[] RemaingArgs) ParseCommand(string[] args)
74+
{
75+
string first = args.First();
76+
77+
if (first == COMMAND_WEB)
78+
{
79+
return (COMMAND_WEB, args.Skip(1).ToArray());
80+
}
81+
else if (first == COMMAND_TEST)
82+
{
83+
return (COMMAND_TEST, args.Skip(1).ToArray());
84+
}
85+
else if (first == COMMAND_DUMP)
86+
{
87+
return (COMMAND_DUMP, args.Skip(1).ToArray());
88+
}
89+
else if (first == COMMAND_DUMPREFS)
90+
{
91+
return (COMMAND_DUMPREFS, args.Skip(1).ToArray());
92+
}
93+
else if (!first.StartsWith("--"))
94+
{
95+
throw new CommandLineParsingException($"Unknown command '{first}'");
96+
}
97+
else
98+
{
99+
return (COMMAND_WEB, args.ToArray());
100+
}
101+
}
102+
103+
static private (Dictionary<string, string> StringSwitches, Dictionary<string, bool> BoolSwitches) ParseSwitches(string command, string[] args)
104+
{
53105
var seenSwitches = new List<string>();
54-
var switchValues = new Dictionary<string, string>();
55-
var boolValues = new Dictionary<string, bool>();
106+
var stringSwitch = new Dictionary<string, string>();
107+
var boolSwitches = new Dictionary<string, bool>();
56108

57109
foreach (var valueSwitch in VALUE_SWITCHES)
58110
{
59-
switchValues[valueSwitch] = null;
111+
stringSwitch[valueSwitch] = null;
60112
}
61113

62114
foreach (var boolSwitch in BOOL_SWITCHES)
63115
{
64-
boolValues[boolSwitch] = false;
116+
boolSwitches[boolSwitch] = false;
65117
}
66118

67119
var i = 0;
@@ -71,12 +123,12 @@ static public ParsedCommandLine ParseArguments(string[] args)
71123
if (VALUE_SWITCHES.Contains(arg))
72124
{
73125
var value = args[++i];
74-
switchValues[arg] = value;
126+
stringSwitch[arg] = value;
75127
seenSwitches.Add(arg);
76128
}
77129
else if (BOOL_SWITCHES.Contains(arg))
78130
{
79-
boolValues[arg] = true;
131+
boolSwitches[arg] = true;
80132
seenSwitches.Add(arg);
81133
}
82134
else if (arg.StartsWith("--"))
@@ -91,16 +143,9 @@ static public ParsedCommandLine ParseArguments(string[] args)
91143
}
92144

93145
// Validation
94-
if (switchValues[VALUE_SWITCH_COMMAND] == null)
95-
throw new CommandLineParsingException($"Missing required switch {VALUE_SWITCH_COMMAND}");
96-
if (switchValues[VALUE_SWITCH_ENDPOINTS] == null)
146+
if (stringSwitch[VALUE_SWITCH_ENDPOINTS] == null)
97147
throw new CommandLineParsingException($"Missing required switch {VALUE_SWITCH_ENDPOINTS}");
98148

99-
var command = switchValues[VALUE_SWITCH_COMMAND];
100-
101-
if (!COMMAND_VALUES.Contains(command))
102-
throw new CommandLineParsingException($"Unknown command '{command}'");
103-
104149
foreach (var seenSwitch in seenSwitches)
105150
{
106151
if (!VALID_SWITHCES_BY_COMMAND[command].Contains(seenSwitch))
@@ -109,21 +154,7 @@ static public ParsedCommandLine ParseArguments(string[] args)
109154
}
110155
}
111156

112-
// Return
113-
return new ParsedCommandLine
114-
{
115-
Command = command,
116-
Endpoints = switchValues[VALUE_SWITCH_ENDPOINTS],
117-
118-
Urls = switchValues[VALUE_SWITCH_URLS],
119-
Only = switchValues[VALUE_SWITCH_ONLY],
120-
121-
ShowResponse = boolValues[BOOL_SWITCH_SHOWRESPONSE],
122-
NoTestMode = boolValues[BOOL_SWITCH_NOTESTMODE],
123-
Stop = boolValues[BOOL_SWITCH_STOP],
124-
Diff = boolValues[BOOL_SWITCH_DIFF],
125-
List = boolValues[BOOL_SWITCH_LIST]
126-
};
157+
return (stringSwitch, boolSwitches);
127158
}
128159
}
129160

netmockery/documentation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
Command line:
44
``` bash
55
# Netmockery starts and listens on default port
6-
dotnet netmockery.dll --command web --endpoints p:\ath\to\endpoint\directory
6+
dotnet netmockery.dll web --endpoints [YourEndpointFilesPath]
77

88
# Netmockery starts and listens on given port
9-
dotnet netmockery.dll --command web --endpoints p:\ath\to\endpoint\directory --urls http://*:9876
9+
dotnet netmockery.dll web --endpoints [YourEndpointFilesPath] --urls http://*:9876
1010
```
1111

1212

0 commit comments

Comments
 (0)