mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:03:46 +01:00
Use SimpleCommandLineParser
This commit is contained in:
85
src/WireMock.Net.StandAlone/SimpleCommandLineParser.cs
Normal file
85
src/WireMock.Net.StandAlone/SimpleCommandLineParser.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace WireMock.Net.StandAlone
|
||||
{
|
||||
// Based on http://blog.gauffin.org/2014/12/simple-command-line-parser/
|
||||
internal class SimpleCommandLineParser
|
||||
{
|
||||
public IDictionary<string, string[]> Arguments { get; private set; } = new Dictionary<string, string[]>();
|
||||
|
||||
public void Parse(string[] args)
|
||||
{
|
||||
string currentName = null;
|
||||
|
||||
var values = new List<string>();
|
||||
foreach (string arg in args)
|
||||
{
|
||||
if (arg.StartsWith("--"))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(currentName))
|
||||
{
|
||||
Arguments[currentName] = values.ToArray();
|
||||
}
|
||||
|
||||
values.Clear();
|
||||
currentName = arg.Substring(2);
|
||||
}
|
||||
else if (string.IsNullOrEmpty(currentName))
|
||||
{
|
||||
Arguments[arg] = new string[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
values.Add(arg);
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(currentName))
|
||||
{
|
||||
Arguments[currentName] = values.ToArray();
|
||||
}
|
||||
}
|
||||
public bool Contains(string name)
|
||||
{
|
||||
return Arguments.ContainsKey(name);
|
||||
}
|
||||
|
||||
public string[] GetValues(string name, string[] defaultValue = null)
|
||||
{
|
||||
return Contains(name) ? Arguments[name] : defaultValue;
|
||||
}
|
||||
|
||||
public T GetValue<T>(string name, Func<string[], T> func, T defaultValue = default(T))
|
||||
{
|
||||
return Contains(name) ? func(Arguments[name]) : defaultValue;
|
||||
}
|
||||
|
||||
public bool GetBoolValue(string name, bool defaultValue = false)
|
||||
{
|
||||
return GetValue<bool>(name, (values) =>
|
||||
{
|
||||
string value = values.FirstOrDefault();
|
||||
return !string.IsNullOrEmpty(value) ? bool.Parse(value) : defaultValue;
|
||||
}, defaultValue);
|
||||
}
|
||||
|
||||
public int? GetIntValue(string name, int? defaultValue = null)
|
||||
{
|
||||
return GetValue<int?>(name, (values) =>
|
||||
{
|
||||
string value = values.FirstOrDefault();
|
||||
return !string.IsNullOrEmpty(value) ? int.Parse(value) : defaultValue;
|
||||
}, defaultValue);
|
||||
}
|
||||
|
||||
public string GetStringValue(string name, string defaultValue = null)
|
||||
{
|
||||
return GetValue<string>(name, (values) =>
|
||||
{
|
||||
return values.FirstOrDefault() ?? defaultValue;
|
||||
}, defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
142
src/WireMock.Net.StandAlone/StandAloneApp.CommandLineParser.cs
Normal file
142
src/WireMock.Net.StandAlone/StandAloneApp.CommandLineParser.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
// using System;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Linq;
|
||||
// using CommandLineParser.Arguments;
|
||||
// using CommandLineParser.Exceptions;
|
||||
// using WireMock.Server;
|
||||
// using WireMock.Settings;
|
||||
// using WireMock.Validation;
|
||||
// using JetBrains.Annotations;
|
||||
// using Newtonsoft.Json;
|
||||
|
||||
// namespace WireMock.Net.StandAlone
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// The StandAloneApp
|
||||
// /// </summary>
|
||||
// public static class StandAloneApp
|
||||
// {
|
||||
// private class Options
|
||||
// {
|
||||
// [ValueArgument(typeof(int?), "Port", Description = "Port to listen on.", Optional = true)]
|
||||
// public int? Port { get; set; }
|
||||
|
||||
// [ValueArgument(typeof(string), "Urls", Description = "URL(s) to listen on.", Optional = true, AllowMultiple = true)]
|
||||
// public List<string> Urls { get; set; }
|
||||
|
||||
// [SwitchArgument("AllowPartialMapping", false, Description = "Allow Partial Mapping (default set to false).", Optional = true)]
|
||||
// public bool AllowPartialMapping { get; set; }
|
||||
|
||||
// [SwitchArgument("StartAdminInterface", true, Description = "Start the AdminInterface (default set to true).", Optional = true)]
|
||||
// public bool StartAdminInterface { get; set; }
|
||||
|
||||
// [SwitchArgument("ReadStaticMappings", true, Description = "Read StaticMappings from ./__admin/mappings (default set to true).", Optional = true)]
|
||||
// public bool ReadStaticMappings { get; set; }
|
||||
|
||||
// [ValueArgument(typeof(string), "ProxyURL", Description = "The ProxyURL to use.", Optional = true)]
|
||||
// public string ProxyURL { get; set; }
|
||||
|
||||
// [SwitchArgument("SaveProxyMapping", true, Description = "Save the proxied request and response mapping files in ./__admin/mappings. (default set to true).", Optional = true)]
|
||||
// public bool SaveMapping { get; set; }
|
||||
|
||||
// [ValueArgument(typeof(string), "X509Certificate2ThumbprintOrSubjectName", Description = "The X509Certificate2 Thumbprint or SubjectName to use.", Optional = true)]
|
||||
// public string X509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||
|
||||
// [ValueArgument(typeof(string), "AdminUsername", Description = "The username needed for __admin access.", Optional = true)]
|
||||
// public string AdminUsername { get; set; }
|
||||
|
||||
// [ValueArgument(typeof(string), "AdminPassword", Description = "The password needed for __admin access.", Optional = true)]
|
||||
// public string AdminPassword { get; set; }
|
||||
|
||||
// [ValueArgument(typeof(int?), "RequestLogExpirationDuration", Description = "The RequestLog expiration in hours.", Optional = true)]
|
||||
// public int? RequestLogExpirationDuration { get; set; }
|
||||
|
||||
// [ValueArgument(typeof(int?), "MaxRequestLogCount", Description = "The MaxRequestLog count.", Optional = true)]
|
||||
// public int? MaxRequestLogCount { get; set; }
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// Start WireMock.Net standalone based on the FluentMockServerSettings.
|
||||
// /// </summary>
|
||||
// /// <param name="settings">The FluentMockServerSettings</param>
|
||||
// [PublicAPI]
|
||||
// public static FluentMockServer Start([NotNull] FluentMockServerSettings settings)
|
||||
// {
|
||||
// Check.NotNull(settings, nameof(settings));
|
||||
|
||||
// return FluentMockServer.Start(settings);
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// Start WireMock.Net standalone based on the commandline arguments.
|
||||
// /// </summary>
|
||||
// /// <param name="args">The commandline arguments</param>
|
||||
// [PublicAPI]
|
||||
// public static FluentMockServer Start([NotNull] string[] args)
|
||||
// {
|
||||
// Check.NotNull(args, nameof(args));
|
||||
|
||||
// // Console.WriteLine("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'")));
|
||||
|
||||
// var options = new Options();
|
||||
// var parser = new CommandLineParser.CommandLineParser();
|
||||
// parser.ExtractArgumentAttributes(options);
|
||||
|
||||
// try
|
||||
// {
|
||||
// parser.ParseCommandLine(args);
|
||||
|
||||
// Console.WriteLine("WireMock.Net server options {0}", JsonConvert.SerializeObject(options, Formatting.Indented));
|
||||
|
||||
// var settings = new FluentMockServerSettings
|
||||
// {
|
||||
// StartAdminInterface = options.StartAdminInterface,
|
||||
// ReadStaticMappings = options.ReadStaticMappings,
|
||||
// AllowPartialMapping = options.AllowPartialMapping,
|
||||
// AdminUsername = options.AdminUsername,
|
||||
// AdminPassword = options.AdminPassword,
|
||||
// MaxRequestLogCount = options.MaxRequestLogCount,
|
||||
// RequestLogExpirationDuration = options.RequestLogExpirationDuration
|
||||
// };
|
||||
|
||||
// if (options.Port != null)
|
||||
// {
|
||||
// settings.Port = options.Port;
|
||||
// }
|
||||
// else if (options.Urls != null && options.Urls.Any())
|
||||
// {
|
||||
// settings.Urls = options.Urls.ToArray();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// settings.Urls = new [] { "http://*:9091/" };
|
||||
// }
|
||||
|
||||
// if (!string.IsNullOrEmpty(options.ProxyURL))
|
||||
// {
|
||||
// settings.ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
// {
|
||||
// Url = options.ProxyURL,
|
||||
// SaveMapping = options.SaveMapping,
|
||||
// X509Certificate2ThumbprintOrSubjectName = options.X509Certificate2ThumbprintOrSubjectName
|
||||
// };
|
||||
// }
|
||||
|
||||
// Console.WriteLine("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
|
||||
|
||||
// FluentMockServer server = Start(settings);
|
||||
|
||||
// Console.WriteLine("WireMock.Net server listening at {0}", string.Join(" and ", server.Urls));
|
||||
|
||||
// return server;
|
||||
// }
|
||||
// catch (CommandLineException e)
|
||||
// {
|
||||
// Console.WriteLine(e.Message);
|
||||
// parser.ShowUsage();
|
||||
|
||||
// throw;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
@@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CommandLineParser.Arguments;
|
||||
using CommandLineParser.Exceptions;
|
||||
using WireMock.Server;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Validation;
|
||||
@@ -16,45 +14,6 @@ namespace WireMock.Net.StandAlone
|
||||
/// </summary>
|
||||
public static class StandAloneApp
|
||||
{
|
||||
private class Options
|
||||
{
|
||||
[ValueArgument(typeof(int), "Port", Description = "Port to listen on.", Optional = true)]
|
||||
public int? Port { get; set; }
|
||||
|
||||
[ValueArgument(typeof(string), "Urls", Description = "URL(s) to listen on.", Optional = true, AllowMultiple = true)]
|
||||
public List<string> Urls { get; set; }
|
||||
|
||||
[SwitchArgument("AllowPartialMapping", false, Description = "Allow Partial Mapping (default set to false).", Optional = true)]
|
||||
public bool AllowPartialMapping { get; set; }
|
||||
|
||||
[SwitchArgument("StartAdminInterface", true, Description = "Start the AdminInterface (default set to true).", Optional = true)]
|
||||
public bool StartAdminInterface { get; set; }
|
||||
|
||||
[SwitchArgument("ReadStaticMappings", true, Description = "Read StaticMappings from ./__admin/mappings (default set to true).", Optional = true)]
|
||||
public bool ReadStaticMappings { get; set; }
|
||||
|
||||
[ValueArgument(typeof(string), "ProxyURL", Description = "The ProxyURL to use.", Optional = true)]
|
||||
public string ProxyURL { get; set; }
|
||||
|
||||
[SwitchArgument("SaveProxyMapping", true, Description = "Save the proxied request and response mapping files in ./__admin/mappings. (default set to true).", Optional = true)]
|
||||
public bool SaveMapping { get; set; }
|
||||
|
||||
[ValueArgument(typeof(string), "X509Certificate2ThumbprintOrSubjectName", Description = "The X509Certificate2 Thumbprint or SubjectName to use.", Optional = true)]
|
||||
public string X509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||
|
||||
[ValueArgument(typeof(string), "AdminUsername", Description = "The username needed for __admin access.", Optional = true)]
|
||||
public string AdminUsername { get; set; }
|
||||
|
||||
[ValueArgument(typeof(string), "AdminPassword", Description = "The password needed for __admin access.", Optional = true)]
|
||||
public string AdminPassword { get; set; }
|
||||
|
||||
[ValueArgument(typeof(int?), "RequestLogExpirationDuration", Description = "The RequestLog expiration in hours (optional).", Optional = true)]
|
||||
public int? RequestLogExpirationDuration { get; set; }
|
||||
|
||||
[ValueArgument(typeof(int?), "MaxRequestLogCount", Description = "The MaxRequestLog count (optional).", Optional = true)]
|
||||
public int? MaxRequestLogCount { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start WireMock.Net standalone based on the FluentMockServerSettings.
|
||||
/// </summary>
|
||||
@@ -68,7 +27,7 @@ namespace WireMock.Net.StandAlone
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start WireMock.Net standalone bases on the commandline arguments.
|
||||
/// Start WireMock.Net standalone based on the commandline arguments.
|
||||
/// </summary>
|
||||
/// <param name="args">The commandline arguments</param>
|
||||
[PublicAPI]
|
||||
@@ -76,75 +35,49 @@ namespace WireMock.Net.StandAlone
|
||||
{
|
||||
Check.NotNull(args, nameof(args));
|
||||
|
||||
var options = new Options();
|
||||
var parser = new CommandLineParser.CommandLineParser();
|
||||
parser.ExtractArgumentAttributes(options);
|
||||
Console.WriteLine("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'")));
|
||||
|
||||
try
|
||||
var parser = new SimpleCommandLineParser();
|
||||
parser.Parse(args);
|
||||
|
||||
var settings = new FluentMockServerSettings
|
||||
{
|
||||
parser.ParseCommandLine(args);
|
||||
StartAdminInterface = parser.GetBoolValue("StartAdminInterface", true),
|
||||
ReadStaticMappings = parser.GetBoolValue("ReadStaticMappings"),
|
||||
AllowPartialMapping = parser.GetBoolValue("AllowPartialMapping", true),
|
||||
AdminUsername = parser.GetStringValue("AdminUsername"),
|
||||
AdminPassword = parser.GetStringValue("AdminPassword"),
|
||||
MaxRequestLogCount = parser.GetIntValue("MaxRequestLogCount"),
|
||||
RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"),
|
||||
};
|
||||
|
||||
if (!options.Urls.Any())
|
||||
if (parser.Contains("Port"))
|
||||
{
|
||||
settings.Port = parser.GetIntValue("Port");
|
||||
}
|
||||
else
|
||||
{
|
||||
settings.Urls = parser.GetValues("Urls", new[] { "http://*:9091/" });
|
||||
}
|
||||
|
||||
string proxyURL = parser.GetStringValue("ProxyURL");
|
||||
if (!string.IsNullOrEmpty(proxyURL))
|
||||
{
|
||||
settings.ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
options.Urls.Add("http://localhost:9091/");
|
||||
}
|
||||
|
||||
var settings = new FluentMockServerSettings
|
||||
{
|
||||
StartAdminInterface = options.StartAdminInterface,
|
||||
ReadStaticMappings = options.ReadStaticMappings,
|
||||
AllowPartialMapping = options.AllowPartialMapping,
|
||||
AdminUsername = options.AdminUsername,
|
||||
AdminPassword = options.AdminPassword,
|
||||
MaxRequestLogCount = options.MaxRequestLogCount,
|
||||
RequestLogExpirationDuration = options.RequestLogExpirationDuration
|
||||
|
||||
Url = proxyURL,
|
||||
SaveMapping = parser.GetBoolValue("SaveMapping"),
|
||||
X509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("X509Certificate2ThumbprintOrSubjectName")
|
||||
};
|
||||
|
||||
if (options.Port != null)
|
||||
{
|
||||
settings.Port = options.Port;
|
||||
}
|
||||
else if (options.Urls != null)
|
||||
{
|
||||
settings.Urls = options.Urls.ToArray();
|
||||
}
|
||||
|
||||
// if (options.MaxRequestLogCount > 0)
|
||||
// {
|
||||
// settings.MaxRequestLogCount = options.MaxRequestLogCount;
|
||||
// }
|
||||
|
||||
// if (options.RequestLogExpirationDuration > 0)
|
||||
// {
|
||||
// settings.RequestLogExpirationDuration = options.RequestLogExpirationDuration;
|
||||
// }
|
||||
|
||||
if (!string.IsNullOrEmpty(options.ProxyURL))
|
||||
{
|
||||
settings.ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = options.ProxyURL,
|
||||
SaveMapping = options.SaveMapping,
|
||||
X509Certificate2ThumbprintOrSubjectName = options.X509Certificate2ThumbprintOrSubjectName
|
||||
};
|
||||
}
|
||||
|
||||
Console.WriteLine("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
|
||||
|
||||
FluentMockServer server = Start(settings);
|
||||
|
||||
Console.WriteLine("WireMock.Net server listening at {0}", string.Join(" and ", server.Urls));
|
||||
|
||||
return server;
|
||||
}
|
||||
catch (CommandLineException e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
parser.ShowUsage();
|
||||
|
||||
throw;
|
||||
}
|
||||
Console.WriteLine("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
|
||||
|
||||
FluentMockServer server = Start(settings);
|
||||
|
||||
Console.WriteLine("WireMock.Net server listening at {0}", string.Join(" and ", server.Urls));
|
||||
|
||||
return server;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,10 @@
|
||||
<PackageReference Include="JetBrains.Annotations" Version="10.4.0">
|
||||
<PrivateAssets>All</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="CommandLineArgumentsParser" Version="3.0.14" />
|
||||
<!-- <PackageReference Include="CommandLineArgumentsParser" Version="3.0.16" /> -->
|
||||
<!-- <PackageReference Include="Marsonsoft.CommandLineParser" Version="1.0.34" /> -->
|
||||
<!-- <PackageReference Include="BurnSystems.CommandLine" Version="1.1.0" /> -->
|
||||
<!-- <ProjectReference Include="..\..\..\CommandLineParser\src\CommandLineArgumentsParser\CommandLineArgumentsParser.csproj" /> -->
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user