Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to System.CommandLine for tools #7672

Merged
merged 12 commits into from
Oct 29, 2024
3 changes: 2 additions & 1 deletion tools/DocGen/DocGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.24517.1" />
</ItemGroup>

<ItemGroup>
Expand Down
99 changes: 49 additions & 50 deletions tools/DocGen/Program.cs
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);
2 changes: 1 addition & 1 deletion tools/DocGen/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"DocGen": {
"commandName": "Project",
"commandLineArgs": "/path/to/docs --config --dbsize --jsonrpc --metrics"
"commandLineArgs": "/path/to/docs --config --jsonrpc --metrics"
rubo marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
2 changes: 1 addition & 1 deletion tools/HiveCompare/HiveCompare/HiveCompare.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.24517.1" />
</ItemGroup>

</Project>
59 changes: 23 additions & 36 deletions tools/HiveCompare/HiveCompare/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using HiveCompare.Models;
using McMaster.Extensions.CommandLineUtils;
using System.CommandLine;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;

Expand All @@ -13,53 +13,40 @@ internal class Program

private static void Main(string[] args)
{
CommandLineApplication cli = CreateCommandLineInterface();
try
CliOption<string> firstFileOption = new("--first-file", "-f")
{
cli.Execute(args);
}
catch (CommandParsingException)
Description = "The first file to be used for comparison",
Required = true,
HelpName = "path"
};
CliOption<string> secondFileOption = new("--second-file", "-s")
{
cli.ShowHelp();
}
}

static CommandLineApplication CreateCommandLineInterface()
{
CommandLineApplication cli = new() { Name = "HiveCompare" };
cli.HelpOption("-?|-h|--help");
CommandOption firstFileOption = cli.Option("-f|--first-file", "first file to be used for comparison", CommandOptionType.SingleValue);
CommandOption secondFileOption = cli.Option("-s|--second-file", "second file to be used for comparison", CommandOptionType.SingleValue);
Description = "The second file to be used for comparison",
Required = true,
HelpName = "path"
};
CliRootCommand rootCommand = [firstFileOption, secondFileOption];

cli.OnExecute(() =>
rootCommand.SetAction(parseResult =>
{
bool HasRequiredOption(CommandOption option)
static bool RequiredFileExists(string? filePath)
{
if (option.HasValue() && !string.IsNullOrEmpty(option.Value())) return true;
if (File.Exists(filePath)) return true;

cli.ShowHelp();
Console.WriteLine($"Could not find file '{filePath}'.");
return false;
}

bool RequiredFileExists(CommandOption option)
{
if (File.Exists(option.Value())) return true;

Console.WriteLine($"Could not find file '{option.Value()}'.");
return false;

}
string? firstFileValue = parseResult.GetValue(firstFileOption);
string? secondFileValue = parseResult.GetValue(secondFileOption);

return HasRequiredOption(firstFileOption) && HasRequiredOption(secondFileOption)
? RequiredFileExists(firstFileOption) && RequiredFileExists(secondFileOption)
? ParseTests(firstFileOption.Value()!, secondFileOption.Value()!)
? 0
: 4
: 2
: 1;
return RequiredFileExists(firstFileValue) && RequiredFileExists(secondFileValue)
? ParseTests(firstFileValue!, secondFileValue!) ? 0 : 4
: 2;
});

return cli;
CliConfiguration cli = new(rootCommand);
cli.Invoke(args);
}

private static bool ParseTests(string firstFile, string secondFile)
Expand Down
Loading
Loading