You can simulate this with NDesk.Options
, although you will have to write some validation code yourself.
In NDesk.Options
you can register a handler for the default option ("<>").
That handler will run for each of the connections in your example. Every time you encounter a value for the default option, you can create a connection settings object, and fill out the latest one when the normal options are supplied.
public class ConnectionListParser : OptionSet
{
public class ConnectionSettings
{
public string Name { get; set; }
public string Servername { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
public List<ConnectionSettings> Connections {get;} = new List<ConnectionSettings>();
public ConnectionListParser() {
Add("<>", x => {
Connections.Add(new ConnectionSettings() {
Name = x
});
});
Add("s:", x => Connections.Last().Servername = x);
Add("u:", x => Connections.Last().Username = x);
Add("p:", x => Connections.Last().Password = x);
}
}
Usage:
var opts = new ConnectionListParser();
var remain = opts.Parse("conn1 -sservername1 -uusername1 -ppassword1 conn2 -sservername2 -uusername2 -ppassword2".Split(' '));
// or opts.Parse(args) when you have actual command line arguments to parse
foreach(var c in opts.Connections) {
Console.WriteLine($"Connection '{c.Name}': Username={c.Username};Password={c.Password};Servername={c.Servername}");
}