-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to
System.CommandLine
for tools (#7672)
Co-authored-by: Lautaro Emanuel <31224949+emlautarom1@users.noreply.github.com>
- Loading branch information
1 parent
32e9120
commit 8277892
Showing
12 changed files
with
459 additions
and
440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,65 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.ComponentModel; | ||
using System.CommandLine; | ||
using Nethermind.DocGen; | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
|
||
var app = new CommandApp<AppCommand>(); | ||
|
||
app.Run(args); | ||
|
||
public sealed class AppCommand : Command<AppSettings> | ||
CliOption<bool> configOption = new("--config") { Description = "Generate configuration options docs" }; | ||
CliOption<bool> dbSizeOption = new("--dbsize") { Description = "Generate DB sizes" }; | ||
CliOption<string> dbSizeSourceOption = new("--dbsize-src") | ||
{ | ||
public override int Execute(CommandContext context, AppSettings settings) | ||
{ | ||
if (settings.DocsPath is null) | ||
{ | ||
AnsiConsole.MarkupLine("[red]The path to the docs is not specified[/]"); | ||
return 1; | ||
} | ||
|
||
if (!Directory.Exists(settings.DocsPath)) | ||
{ | ||
AnsiConsole.MarkupLine("[red]No docs not found at the path specified[/]"); | ||
return 1; | ||
} | ||
|
||
if (settings.GenerateConfig) | ||
ConfigGenerator.Generate(settings.DocsPath); | ||
|
||
if (settings.GenerateDBSize) | ||
DBSizeGenerator.Generate(settings.DocsPath, settings.DBSizeSourcePath); | ||
|
||
if (settings.GenerateJsonRpc) | ||
JsonRpcGenerator.Generate(settings.DocsPath); | ||
Description = "The path to the directory with DB size files", | ||
HelpName = "path" | ||
}; | ||
CliArgument<string> docsDirArg = new("docs-dir") | ||
{ | ||
Description = "The path to the docs directory", | ||
HelpName = "path" | ||
}; | ||
CliOption<bool> jsonRpcOption = new("--jsonrpc") { Description = "Generate JSON-RPC API docs" }; | ||
CliOption<bool> metricsOption = new("--metrics") { Description = "Generate metrics options docs" }; | ||
|
||
if (settings.GenerateMetrics) | ||
MetricsGenerator.Generate(settings.DocsPath); | ||
dbSizeOption.Validators.Add(optionResult => | ||
{ | ||
if (optionResult.Parent?.GetValue(dbSizeSourceOption) is null) | ||
optionResult.AddError($"{dbSizeSourceOption.Name} must be specified when {dbSizeOption.Name} is set"); | ||
}); | ||
|
||
CliRootCommand rootCommand = | ||
[ | ||
configOption, | ||
dbSizeOption, | ||
dbSizeSourceOption, | ||
docsDirArg, | ||
jsonRpcOption, | ||
metricsOption | ||
]; | ||
rootCommand.SetAction(parseResult => | ||
{ | ||
var docsPath = parseResult.GetValue(docsDirArg)!; | ||
return 0; | ||
if (!Directory.Exists(docsPath)) | ||
{ | ||
AnsiConsole.MarkupLine("[red]The specified docs directory not found[/]"); | ||
return 1; | ||
} | ||
} | ||
public sealed class AppSettings : CommandSettings | ||
{ | ||
[Description("Path to the directory with DB size files")] | ||
[CommandOption("--dbsize-src")] | ||
public string? DBSizeSourcePath { get; init; } | ||
if (parseResult.GetValue(configOption)) | ||
ConfigGenerator.Generate(docsPath); | ||
if (parseResult.GetValue(dbSizeOption)) | ||
DBSizeGenerator.Generate(docsPath, parseResult.GetValue(dbSizeSourceOption)); | ||
[Description("Path to the docs")] | ||
[CommandArgument(0, "[docspath]")] | ||
public string? DocsPath { get; init; } | ||
if (parseResult.GetValue(jsonRpcOption)) | ||
JsonRpcGenerator.Generate(docsPath); | ||
[CommandOption("--config")] | ||
public bool GenerateConfig { get; init; } | ||
if (parseResult.GetValue(metricsOption)) | ||
MetricsGenerator.Generate(docsPath); | ||
[CommandOption("--dbsize")] | ||
public bool GenerateDBSize { get; init; } | ||
return 0; | ||
}); | ||
|
||
[CommandOption("--jsonrpc")] | ||
public bool GenerateJsonRpc { get; init; } | ||
CliConfiguration cli = new(rootCommand); | ||
|
||
[CommandOption("--metrics")] | ||
public bool GenerateMetrics { get; init; } | ||
} | ||
return cli.Invoke(args); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.