Skip to content

Commit

Permalink
core: apply options parser
Browse files Browse the repository at this point in the history
  • Loading branch information
luantranminh committed Aug 8, 2024
1 parent 669a74d commit 459501f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/Atlas.Provider.Core/Executor/EFDesign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class EFDesign : IDisposable
private readonly string _appBasePath;

public EFDesign(
string? assembly,
string assembly,
string? startupAssembly,
string? projectDir,
string? dataDirectory,
Expand Down
75 changes: 75 additions & 0 deletions src/Atlas.Provider.Core/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;

namespace Atlas.Provider.Core
{
public class Options
{
public string Assembly { get; set; } = string.Empty;
public string Project { get; set; } = string.Empty;
public string StartupAssembly { get; set; } = string.Empty;
public string StartupProject { get; set; } = string.Empty;
public string ProjectDir { get; set; } = string.Empty;
public string RootNamespace { get; set; } = string.Empty;
public string Language { get; set; } = string.Empty;
public string Framework { get; set; } = string.Empty;
public string WorkingDir { get; set; } = string.Empty;
public bool Nullable { get; set; } = true;
public List<string>? PositionalArgs { get; set; }

public Options(string[] args)
{
Assembly = string.Empty;
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "--assembly":
if (i + 1 < args.Length) Assembly = args[++i];
break;
case "--project":
if (i + 1 < args.Length) Project = args[++i];
break;
case "--startup-assembly":
if (i + 1 < args.Length) StartupAssembly = args[++i];
break;
case "--startup-project":
if (i + 1 < args.Length) StartupProject = args[++i];
break;
case "--project-dir":
if (i + 1 < args.Length) ProjectDir = args[++i];
break;
case "--root-namespace":
if (i + 1 < args.Length) RootNamespace = args[++i];
break;
case "--language":
if (i + 1 < args.Length) Language = args[++i];
break;
case "--framework":
if (i + 1 < args.Length) Framework = args[++i];
break;
case "--working-dir":
if (i + 1 < args.Length) WorkingDir = args[++i];
break;
default:
if (PositionalArgs == null)
{
PositionalArgs = new List<string>();
}
PositionalArgs.Add(args[i]);
break;
}
}

if (string.IsNullOrEmpty(Assembly)) throw new ArgumentException("--assembly option requires a value.");
if (string.IsNullOrEmpty(Project)) throw new ArgumentException("--project option requires a value.");
if (string.IsNullOrEmpty(StartupAssembly)) throw new ArgumentException("--startupAssembly option requires a value.");
if (string.IsNullOrEmpty(StartupProject)) throw new ArgumentException("--startupProject option requires a value.");
if (string.IsNullOrEmpty(ProjectDir)) throw new ArgumentException("--projectDir option requires a value.");
if (string.IsNullOrEmpty(RootNamespace)) throw new ArgumentException("--rootNamespace option requires a value.");
if (string.IsNullOrEmpty(Language)) throw new ArgumentException("--language option requires a value.");
if (string.IsNullOrEmpty(Framework)) throw new ArgumentException("--framework option requires a value.");
if (string.IsNullOrEmpty(WorkingDir)) throw new ArgumentException("--workingDir option requires a value.");
}
}
}
140 changes: 8 additions & 132 deletions src/Atlas.Provider.Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,145 +9,21 @@ static class Program
{
static int Main(string[] args)
{
string? assembly = null;
string? project = null;
string? startupAssembly = null;
string? startupProject = null;
string? projectDir = null;
string? rootNamespace = null;
string? language = null;
string? framework = null;
string? workingDir = null;
bool nullable = true;
var positionalArgs = new List<string>();

for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "--assembly":
if (i + 1 < args.Length)
{
assembly = args[++i];
}
else
{
Console.WriteLine("Error: --assembly option requires a value.");
}
break;
case "--project":
if (i + 1 < args.Length)
{
project = args[++i];
}
else
{
Console.WriteLine("Error: --project option requires a value.");
}
break;
case "--startupAssembly":
if (i + 1 < args.Length)
{
startupAssembly = args[++i];
}
else
{
Console.WriteLine("Error: --startupAssembly option requires a value.");
}
break;
case "--startupProject":
if (i + 1 < args.Length)
{
startupProject = args[++i];
}
else
{
Console.WriteLine("Error: --startupProject option requires a value.");
}
break;
case "--projectDir":
if (i + 1 < args.Length)
{
projectDir = args[++i];
}
else
{
Console.WriteLine("Error: --projectDir option requires a value.");
}
break;
case "--rootNamespace":
if (i + 1 < args.Length)
{
rootNamespace = args[++i];
}
else
{
Console.WriteLine("Error: --rootNamespace option requires a value.");
}
break;
case "--language":
if (i + 1 < args.Length)
{
language = args[++i];
}
else
{
Console.WriteLine("Error: --language option requires a value.");
}
break;
case "--framework":
if (i + 1 < args.Length)
{
framework = args[++i];
}
else
{
Console.WriteLine("Error: --framework option requires a value.");
}
break;
case "--workingDir":
if (i + 1 < args.Length)
{
workingDir = args[++i];
}
else
{
Console.WriteLine("Error: --workingDir option requires a value.");
}
break;
case "--nullable":
if (i + 1 < args.Length)
{
if (args[++i] == "true")
{
nullable = true;
}
else if (args[i] == "false")
{
nullable = false;
}
}
break;
default:
positionalArgs.Add(args[i]);
break;
}
}

try
{
var options = new Options(args);
// prevent any output from being written to the console, including warn, info, error etc messages from EF Core
var originalOut = Console.Out;
Console.SetOut(TextWriter.Null);
using var executor = new EFDesign(
assembly,
startupAssembly,
projectDir,
options.Assembly,
options.StartupAssembly,
options.ProjectDir,
null,
rootNamespace,
language,
nullable,
args
options.RootNamespace,
options.Language,
options.Nullable,
options.PositionalArgs?.ToArray()
);
var types = executor.GetContextTypes();
foreach (var type in types)
Expand Down

0 comments on commit 459501f

Please sign in to comment.