-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.cs
More file actions
107 lines (93 loc) · 3.5 KB
/
CommandLine.cs
File metadata and controls
107 lines (93 loc) · 3.5 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
using System.CommandLine;
using System.CommandLine.Parsing;
namespace ptr727.LanguageTags.Create;
internal sealed class CommandLine
{
private readonly Option<LogEventLevel> _logLevelOption = CreateLogLevelOption();
private readonly Option<string> _logFileOption = CreateLogFileOption();
private readonly Option<bool> _logFileClearOption = CreateLogFileClearOption();
private readonly Option<DirectoryInfo> _codePathOption = CreateCodePathOption();
private static readonly FrozenSet<string> s_cliBypassList = FrozenSet.Create(
StringComparer.OrdinalIgnoreCase,
"--help",
"--version"
);
internal CommandLine(string[] args)
{
Root = CreateRootCommand();
Result = Root.Parse(args);
}
internal RootCommand Root { get; init; }
internal ParseResult Result { get; init; }
internal RootCommand CreateRootCommand()
{
RootCommand rootCommand = new("Download and generate language tag data and code")
{
_logLevelOption,
_logFileOption,
_logFileClearOption,
_codePathOption,
};
rootCommand.SetAction(
(parseResult, cancellationToken) =>
{
Program program = new(CreateOptions(parseResult), cancellationToken);
return program.ExecuteAsync();
}
);
return rootCommand;
}
internal Options CreateOptions(ParseResult parseResult) =>
new()
{
LogOptions = new LoggerFactory.Options
{
Level = parseResult.GetValue(_logLevelOption),
File = parseResult.GetValue(_logFileOption) ?? string.Empty,
FileClear = parseResult.GetValue(_logFileClearOption),
},
CodePath = parseResult.GetValue(_codePathOption)!,
};
private static Option<bool> CreateLogFileClearOption() =>
new("--logfile-clear", "-c")
{
Description = "Clear the log file before writing (default: false).",
Recursive = true,
};
private static Option<LogEventLevel> CreateLogLevelOption() =>
new("--loglevel", "-l")
{
Description = "Set the log level (default: Information).",
DefaultValueFactory = _ => LogEventLevel.Information,
Recursive = true,
};
private static Option<string> CreateLogFileOption()
{
Option<string> option = new("--logfile", "-f")
{
Description = "Write logs to the specified file (optional).",
Recursive = true,
};
return option.AcceptLegalFileNamesOnly();
}
private static Option<DirectoryInfo> CreateCodePathOption()
{
Option<DirectoryInfo> option = new("--codepath", "-p")
{
Description = "Path to the solution directory.",
Required = true,
};
return option.AcceptExistingOnly();
}
internal static bool BypassStartup(ParseResult parseResult) =>
parseResult.Errors.Count > 0
|| parseResult.CommandResult.Children.Any(symbolResult =>
symbolResult is OptionResult optionResult
&& s_cliBypassList.Contains(optionResult.Option.Name)
);
internal sealed class Options
{
internal required LoggerFactory.Options LogOptions { get; init; }
internal required DirectoryInfo CodePath { get; init; }
}
}