forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArguments.cs
More file actions
132 lines (122 loc) · 4.68 KB
/
Copy pathArguments.cs
File metadata and controls
132 lines (122 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Text;
using NpgsqlRest;
using NpgsqlRest.HttpFiles;
using NpgsqlRest.TsClient;
using static NpgsqlRestClient.Config;
namespace NpgsqlRestClient;
public static class Arguments
{
public static bool Parse(string[] args)
{
if (args.Any(a => a.ToLowerInvariant() is "-v" or "--version" or "-h" or "--help") is false)
{
return true;
}
if (args.Any(a => a.ToLowerInvariant() is "-h" or "--help") is true)
{
Line("Usage:");
Line([
("npgsqlrest", "Run with the default configuration files: appsettings.json (required) and appsettings.Development.json (optional)."),
("npgsqlrest [files...]", "Run with the custom configuration files. All configuration files are required."),
("npgsqlrest [file1 -o file2...]", "Use the -o switch to mark the next configuration file as optional. The first file after the -o switch is optional."),
("npgsqlrest [file1 --optional file2...]", "Use --optional switch to mark the next configuration file as optional. The first file after the --optional switch is optional."),
(" ", " "),
("npgsqlrest -v, --version", "Show version information."),
("npgsqlrest -h, --help", "Show command line help."),
(" ", " "),
("Note:", "Values in the later file will override the values in the previous one."),
(" ", " "),
("Example:", "npgsqlrest appsettings.json appsettings.Development.json"),
("Example:", "npgsqlrest appsettings.json -o appsettings.Development.json"),
]);
}
if (args.Any(a => a.ToLowerInvariant() is "-v" or "--version") is true)
{
Line("Versions:");
Line([
("Client Build", System.Reflection.Assembly.GetAssembly(typeof(Program))?.GetName()?.Version?.ToString() ?? "-"),
("Npgsql", System.Reflection.Assembly.GetAssembly(typeof(NpgsqlRestOptions))?.GetName()?.Version?.ToString() ?? "-"),
("NpgsqlRest.HttpFiles", System.Reflection.Assembly.GetAssembly(typeof(HttpFileOptions))?.GetName()?.Version?.ToString() ?? "-"),
("NpgsqlRest.TsClient", System.Reflection.Assembly.GetAssembly(typeof(TsClientOptions))?.GetName()?.Version?.ToString() ?? "-"),
(" ", " "),
("CurrentDirectory", CurrentDir)
]);
NL();
}
return false;
}
public static IEnumerable<(string fileName, bool optional)> EnumerateConfigFiles(string[] args)
{
bool nextIsOptional = false;
foreach(var arg in args)
{
if (arg.StartsWith('-'))
{
if (arg.ToLowerInvariant() is "-o" or "--optional")
{
nextIsOptional = true;
}
else
{
throw new ArgumentException($"Unknown parameter {arg}");
}
}
else
{
yield return (arg, nextIsOptional);
nextIsOptional = false;
}
}
}
private static void NL() => Console.WriteLine();
private static void Line(string line, ConsoleColor? color = null)
{
if (color is not null)
{
Console.ForegroundColor = color.Value;
}
Console.WriteLine(line);
if (color is not null)
{
Console.ResetColor();
}
}
private static void Write(string line, ConsoleColor? color = null)
{
if (color is not null)
{
Console.ForegroundColor = color.Value;
}
Console.Write(line);
if (color is not null)
{
Console.ResetColor();
}
}
private static void Line((string str1, string str2)[] lines)
{
var pos = lines.Select(l => l.str1.Length).Max() + 1;
int consoleWidth = Console.WindowWidth;
foreach (var (str1, str2) in lines)
{
Write(str1, ConsoleColor.Yellow);
Console.CursorLeft = pos;
var words = str2.Split(' ');
var line = new StringBuilder(words[0]);
for (int i = 1; i < words.Length; i++)
{
if (line.Length + words[i].Length >= consoleWidth - pos)
{
Line(line.ToString());
line.Clear();
Console.CursorLeft = pos - 1;
}
line.Append(' ' + words[i]);
}
if (line.Length > 0)
{
Line(line.ToString());
}
}
}
}