Skip to content

Commit

Permalink
Shorten boolean command line option notation (#8179)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo authored Feb 16, 2025
1 parent 0e57cb1 commit 8c496c8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion scripts/nethermind.service
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ User=nethermind
Group=nethermind
EnvironmentFile=/home/nethermind/.env
WorkingDirectory=/home/nethermind
ExecStart=/home/nethermind/build/nethermind -dd /home/nethermind/data
ExecStart=/home/nethermind/build/nethermind --data-dir /home/nethermind/data
Restart=on-failure
LimitNOFILE=1000000

Expand Down
42 changes: 22 additions & 20 deletions src/Nethermind/Nethermind.Runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ async Task<int> RunAsync(ParseResult parseResult, PluginLoader pluginLoader, Can

void AddConfigurationOptions(CliCommand command)
{
static CliOption CreateOption<T>(string name, Type configType) =>
new CliOption<T>(
$"--{ConfigExtensions.GetCategoryName(configType)}.{name}",
$"--{ConfigExtensions.GetCategoryName(configType)}-{name}".ToLowerInvariant());

IEnumerable<Type> configTypes = TypeDiscovery
.FindNethermindBasedTypes(typeof(IConfig))
.Where(ct => ct.IsInterface);
Expand All @@ -249,27 +254,25 @@ void AddConfigurationOptions(CliCommand command)

bool categoryHidden = typeLevel?.HiddenFromDocs == true;

foreach (PropertyInfo propertyInfo in
foreach (PropertyInfo prop in
configType.GetProperties(BindingFlags.Public | BindingFlags.Instance).OrderBy(p => p.Name))
{
ConfigItemAttribute? configItemAttribute = propertyInfo.GetCustomAttribute<ConfigItemAttribute>();
ConfigItemAttribute? configItemAttribute = prop.GetCustomAttribute<ConfigItemAttribute>();

if (configItemAttribute?.DisabledForCli != true)
{
bool hidden = categoryHidden || configItemAttribute?.HiddenFromDocs == true;

command.Add(new CliOption<string>(
$"--{ConfigExtensions.GetCategoryName(configType)}.{propertyInfo.Name}",
$"--{ConfigExtensions.GetCategoryName(configType)}-{propertyInfo.Name}".ToLowerInvariant())
{
Description = configItemAttribute?.Description,
HelpName = "value",
Hidden = hidden
});
CliOption option = prop.PropertyType == typeof(bool)
? CreateOption<bool>(prop.Name, configType)
: CreateOption<string>(prop.Name, configType);
option.Description = configItemAttribute?.Description;
option.HelpName = "value";
option.Hidden = categoryHidden || configItemAttribute?.HiddenFromDocs == true;

command.Add(option);
}

if (configItemAttribute?.IsPortOption == true)
ConfigExtensions.AddPortOptionName(configType, propertyInfo.Name);
ConfigExtensions.AddPortOptionName(configType, prop.Name);
}
}
}
Expand Down Expand Up @@ -311,9 +314,9 @@ CliConfiguration ConfigureCli()

if (versionOption is not null)
{
versionOption.Action = new AnonymousCliAction(r =>
versionOption.Action = new AnonymousCliAction(parseResult =>
{
Console.WriteLine($"""
parseResult.Configuration.Output.WriteLine($"""
Version: {ProductInfo.Version}
Commit: {ProductInfo.Commit}
Build date: {ProductInfo.BuildTimestamp:u}
Expand Down Expand Up @@ -386,7 +389,10 @@ IConfigProvider CreateConfigProvider(ParseResult parseResult)
{
if (child is OptionResult result)
{
var value = result.GetValueOrDefault<string>();
var isBoolean = result.Option.GetType().GenericTypeArguments.SingleOrDefault() == typeof(bool);
var value = isBoolean
? result.GetValueOrDefault<bool>().ToString().ToLowerInvariant()
: result.GetValueOrDefault<string>();

if (value is not null)
configArgs.Add(result.Option.Name.TrimStart('-'), value);
Expand Down Expand Up @@ -435,10 +441,6 @@ IConfigProvider CreateConfigProvider(ParseResult parseResult)
logger.Warn($"'{name}.cfg' is deprecated. Use '{name}' instead.");
}
}
else
{
configFile = configFile.GetApplicationResourcePath();
}

// Resolve the full path for logging purposes
configFile = Path.GetFullPath(configFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"Energy Web": {
"commandName": "Project",
"commandLineArgs": "-c energyweb --data-dir .data --jsonrpc-enabled true",
"commandLineArgs": "-c energyweb --data-dir .data --jsonrpc-enabled",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down Expand Up @@ -107,7 +107,7 @@
},
"Volta": {
"commandName": "Project",
"commandLineArgs": "-c volta --data-dir .data --jsonrpc-enabled true",
"commandLineArgs": "-c volta --data-dir .data --jsonrpc-enabled",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
12 changes: 8 additions & 4 deletions tools/DocGen/ConfigGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ private static void WriteMarkdown(StreamWriter file, Type configType)
if (!props.Any())
return;

static (string, string) GetValue(PropertyInfo prop) =>
prop.PropertyType == typeof(bool) ? ("true|false", "[true|false]") : ("<value>", "<value>");

var moduleName = configType.Name[1..].Replace("Config", null);

file.WriteLine($"""
Expand All @@ -100,27 +103,28 @@ private static void WriteMarkdown(StreamWriter file, Type configType)
continue;

var description = itemAttr.Description.Replace("\n", "\n ").TrimEnd(' ');
(string value, string cliValue) = GetValue(prop);

file.Write($$"""
- #### `{{moduleName}}.{{prop.Name}}` \{#{{moduleName.ToLowerInvariant()}}-{{prop.Name.ToLowerInvariant()}}\}
<Tabs groupId="usage">
<TabItem value="cli" label="CLI">
```
--{{moduleName.ToLowerInvariant()}}-{{prop.Name.ToLowerInvariant()}} <value>
--{{moduleName}}.{{prop.Name}} <value>
--{{moduleName.ToLowerInvariant()}}-{{prop.Name.ToLowerInvariant()}} {{cliValue}}
--{{moduleName}}.{{prop.Name}} {{cliValue}}
```
</TabItem>
<TabItem value="env" label="Environment variable">
```
NETHERMIND_{{moduleName.ToUpperInvariant()}}CONFIG_{{prop.Name.ToUpperInvariant()}}=<value>
NETHERMIND_{{moduleName.ToUpperInvariant()}}CONFIG_{{prop.Name.ToUpperInvariant()}}={{value}}
```
</TabItem>
<TabItem value="config" label="Configuration file">
```json
{
"{{moduleName}}": {
"{{prop.Name}}": <value>
"{{prop.Name}}": {{value}}
}
}
```
Expand Down

0 comments on commit 8c496c8

Please sign in to comment.