From c8c291fe6143c16d68c306ca27063a54fe8c37d9 Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Sat, 31 Aug 2024 22:24:32 +0300 Subject: [PATCH] CLI --- Esiur.CLI/Esiur.CLI.csproj | 4 +++ Esiur.CLI/GetTemplateOptions.cs | 26 +++++++++++++++++ Esiur.CLI/Program.cs | 52 +++++++++++++++------------------ 3 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 Esiur.CLI/GetTemplateOptions.cs diff --git a/Esiur.CLI/Esiur.CLI.csproj b/Esiur.CLI/Esiur.CLI.csproj index 9389e91..83034a7 100644 --- a/Esiur.CLI/Esiur.CLI.csproj +++ b/Esiur.CLI/Esiur.CLI.csproj @@ -12,6 +12,10 @@ + + + + diff --git a/Esiur.CLI/GetTemplateOptions.cs b/Esiur.CLI/GetTemplateOptions.cs new file mode 100644 index 0000000..d1d5e90 --- /dev/null +++ b/Esiur.CLI/GetTemplateOptions.cs @@ -0,0 +1,26 @@ +using CommandLine; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Esiur.CLI +{ + internal class GetTemplateOptions + { + [Option('d', "dir", Required = false, HelpText = "Directory name where the generated models will be saved.")] + public string Dir { get; set; } + + [Option('u', "username", Required = false, HelpText = "Authentication username.")] + public string Username { get; set; } + + [Option('p', "password", Required = false, HelpText = "Authentication password.")] + public string Password { get; set; } + + + [Option('a', "async-setters", Required = false, HelpText = "Use asynchronous property setters.")] + public bool AsyncSetters { get; set; } + + } +} diff --git a/Esiur.CLI/Program.cs b/Esiur.CLI/Program.cs index baf6adf..b1a6200 100644 --- a/Esiur.CLI/Program.cs +++ b/Esiur.CLI/Program.cs @@ -1,8 +1,11 @@  +using CommandLine; +using Esiur.CLI; using Esiur.Data; using Microsoft.CodeAnalysis; using System; using System.Diagnostics; +using System.IO; using System.Reflection; static void Main(string[] args) @@ -16,22 +19,18 @@ static void Main(string[] args) { var url = args[1]; - var parameters = GetParams(args, 2); - - - var username = args.ElementAtOrDefault(3); - var password = args.ElementAtOrDefault(4); - var asyncSetters = Convert.ToBoolean(args.ElementAtOrDefault(5)); - - var path = Esiur.Proxy.TemplateGenerator.GetTemplate(url, - parameters["-d"] ?? parameters["--dir"], - parameters["-u"] ?? parameters["--username"], - parameters["-p"] ?? parameters["--password"], - parameters["-d"] ?? parameters["--dir"] - parameters.Contains ["--a"] ?? parameters["--dir"]); - - Console.WriteLine($"Generated successfully: {path}"); - + Parser.Default.ParseArguments(args.Skip(2)) + .WithParsed(o => + { + try + { + var path = Esiur.Proxy.TemplateGenerator.GetTemplate(url, o.Dir, o.Username, o.Password, o.AsyncSetters); + Console.WriteLine($"Generated successfully: {path}"); + } + catch (Exception ex) { + Console.WriteLine(ex.ToString()); + } + }); return; } catch (Exception ex) @@ -40,23 +39,17 @@ static void Main(string[] args) return; } } + else if (args[0].ToLower() == "version") + { + var version = FileVersionInfo.GetVersionInfo(typeof(Esiur.Core.AsyncReply).Assembly.Location).FileVersion; + + Console.WriteLine(version); + } } PrintHelp(); } -static StringKeyList GetParams(string[] args, int offset) -{ - var rt = new StringKeyList(); - for(var i = offset; i< args.Length; i+=2) - { - var v = args.Length >= (i + 1) ? args[i+1] : null; - rt.Add(args[i], v); - } - - return rt; -} - static void PrintHelp() { var version = FileVersionInfo.GetVersionInfo(typeof(Esiur.Core.AsyncReply).Assembly.Location).FileVersion; @@ -71,6 +64,7 @@ static void PrintHelp() Console.WriteLine("Global options:"); Console.WriteLine("\t-u, --username\tAuthentication username."); Console.WriteLine("\t-p, --password\tAuthentication password."); - Console.WriteLine("\t-d, --dir\tName of the directory to generate model inside."); + Console.WriteLine("\t-d, --dir\tDirectory name where the generated models will be saved."); + Console.WriteLine("\t-a, --async-setters\tUse asynchronous property setters."); } \ No newline at end of file