CSharp · Console
using System;
using CommandLine;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
//when I change LINE_NUMBER value from 1 KEEP_ALIVE_OPTION is recognized somehow
var args = new string[] {"/SCRIPT_NAME=DEALER_TRACK_KEEP_ALIVE", "/OEM_FAMILY=GM", "/COMPLAINT_CODE=\"0131\"", "/OUT_DIR=C:\\\\Applications\\\\DataFiles\\\\", "/KEEP_ALIVE_OPTION=1", "/STORY=\"Lorem ipsum #UPDATE#dolor sit amet\"", "/DEALER_TRACK_LOGIN_URL=https://blah",
"/DEALER_TRACK_UserID=myId", "/DEALER_TRACK_Password=myPwd12", "/DEALER_TRACK_ADVISOR_PASSWORD=\"asd\"", "/DEALER_TRACK_COMPANY_NUMBER=\"TT\"", "/RO_NUMBER=3464990", "/LINE_NUMBER=1", "/CAUSAL_PART_NUMBER=\"11546959:SL-N-BOLT (00293-CKT)\""};
new ScriptFactory().GetScriptData(args);
Console.WriteLine("Hello World");
}
}
public class ScriptFactory
{
public KeyValuePair<Type, IScriptOptions> GetScriptData(string[] args)
{
string scriptName = args[0].Split('=')[1];
IScriptOptions options = default;
ParserResult<DTKeepAliveOptions> parseRes = Parse<DTKeepAliveOptions>(args)
.WithParsed(keepAliveOptions => Console.WriteLine("success"))
.WithNotParsed(errs => HandleParseError(typeof(DTKeepAlive), errs));
return new KeyValuePair<Type, IScriptOptions>(typeof(DTKeepAlive), options);
}
public ParserResult<T> Parse<T>(string[] args) where T : IScriptOptions
{
Parser parser = new(s =>
{
s.CaseSensitive = false;
s.IgnoreUnknownArguments = true;
s.HelpWriter = Parser.Default.Settings.HelpWriter;
});
var scriptOptions = new List<string>();
for (int i = 0; i < args.Length; i++)
{
//1st element is a script name;
if (i > 0 && !args[i].EndsWith('='))
{
//do not add to scriptOptions with no value
string option = $"--{args[i].TrimStart('/')}";
scriptOptions.Add(option);
}
}
return parser.ParseArguments<T>(scriptOptions);
}
private void HandleParseError(Type scriptType, IEnumerable<Error> errors)
{
var namedErrs = errors.Cast<NamedError>();
string exceptionText = string.Empty;
string errorParamsString = string.Empty;
var missedParamsNames = namedErrs.Where(e => e.Tag == ErrorType.MissingRequiredOptionError);
if (missedParamsNames.Any())
{
foreach (var err in missedParamsNames)
{
errorParamsString += err.NameInfo.NameText + " ";
}
exceptionText = $"Missed required parameter(s) {errorParamsString}for the {scriptType.Name} script";
}
else
{
foreach (var err in namedErrs)
{
errorParamsString += err.NameInfo.NameText + " ";
}
exceptionText = $"Error in parameters {errorParamsString} for the {scriptType.Name} script";
}
throw new Exception(exceptionText);
}
}
public class DTLoginOptions
{
[Option]
public string LINE_NUMBER {get;set;}
[Option(Required = true)]
public string DEALER_TRACK_LOGIN_URL { get; set; }
[Option(Required = true)]
public string DEALER_TRACK_ADVISOR_PASSWORD { get; set; }
[Option(Required = true)]
public string DEALER_TRACK_COMPANY_NUMBER { get; set; }
[Option(Required = true)]
public string OUT_DIR { get; set; }
[Option(Required = true)]
public string DEALER_TRACK_UserID { get; set; }
[Option(Required = true)]
public string DEALER_TRACK_Password { get; set; }
}
public class DTKeepAliveOptions : DTLoginOptions, IScriptOptions
{
[Option(Required = true)]
public string KEEP_ALIVE_OPTION { get; set; }
}
public interface IScriptOptions
{
}
public class DTKeepAlive {}