Skip to content

Commit

Permalink
chore: Improve help to display properly
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Sep 18, 2024
1 parent 662293d commit 5d03c36
Show file tree
Hide file tree
Showing 21 changed files with 129 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<RootPath Condition="'$(SolutionDir)' != ''">$(SolutionDir)</RootPath>
<RootPath Condition="'$(SolutionDir)' == ''">$(MSBuildThisFileDirectory)\</RootPath>
<LibplanetVersion>5.2.0</LibplanetVersion>
<JSSoftCommandsVersion>7.0.0-pr.36</JSSoftCommandsVersion>
<JSSoftCommandsVersion>7.0.0-pr.40</JSSoftCommandsVersion>
<JSSoftCommunicationVersion>2.0.6</JSSoftCommunicationVersion>
<JSSoftConfigurationsVersion>1.0.0</JSSoftConfigurationsVersion>
<UseRidGraph>true</UseRidGraph>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

namespace LibplanetConsole.Clients.Executable;

[CommandSummary("Run a client or provide related tools to connect to the node.")]
internal sealed class EntryCommandContext(params ICommand[] commands)
: CommandContextBase(commands)
{
protected override void OnEmptyExecute()
{
if (GetCommand(["help"]) is IExecutable executable)
{
executable.Execute();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.ComponentModel;
using LibplanetConsole.Common.Commands;

namespace LibplanetConsole.Clients.Executable.EntryCommands;

[Category("Tools")]
internal sealed class KeyCommand : KeyCommandBase
{
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
using System.ComponentModel;
using JSSoft.Commands;
using LibplanetConsole.Common;
using LibplanetConsole.Frameworks;

namespace LibplanetConsole.Clients.Executable.EntryCommands;

[CommandSummary("Creates a schema of the application settings.")]
[Category("Tools")]
internal sealed class SchemaCommand : CommandBase
{
[CommandProperty("output")]
public string OutputPath { get; set; } = string.Empty;

protected override void OnExecute()
{
var schemaBuilder = new ApplicationSettingsSchemaBuilder();
var json = schemaBuilder.Build();
var outputPath = OutputPath;
if (outputPath == string.Empty)
{
Out.WriteLine(json);
}
else
{
var directory = Path.GetDirectoryName(outputPath)
?? throw new InvalidOperationException("The directory is not found.");
if (Directory.Exists(directory) is false)
{
Directory.CreateDirectory(directory);
}

File.WriteAllText(outputPath, json);
}
var colorizedString = JsonUtility.ToColorizedString(json);
Out.WriteLine(colorizedString);
}
}
4 changes: 2 additions & 2 deletions src/client/LibplanetConsole.Clients.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

var commands = new ICommand[]
{
new InitializeCommand(),
new StartCommand(),
new RunCommand(),
new KeyCommand(),
new SchemaCommand(),
new StartCommand(),
new InitializeCommand(),
};
var commandContext = new EntryCommandContext(commands);
await commandContext.ExecuteAsync(args);
8 changes: 4 additions & 4 deletions src/common/LibplanetConsole.Common/Commands/KeyCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public void New(
}

[CommandMethod]
[CommandSummary("Displays the public key of the private key.")]
[CommandSummary("Displays the public key from the given private key.")]
public void Public(
[CommandSummary("The private key.")]
[CommandSummary("Indicates the private key that corresponds to the public key.")]
string privateKey)
{
var key = AppPrivateKey.Parse(privateKey);
Expand All @@ -48,9 +48,9 @@ public void Public(
}

[CommandMethod]
[CommandSummary("Displays the address of the private key or public key.")]
[CommandSummary("Displays the public key from the given private or public key")]
public void Address(
[CommandSummary("The private key or public key.")]
[CommandSummary("Indicates the private or public key that corresponds to the address.")]
string key)
{
var info = new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace LibplanetConsole.Consoles.Executable;
internal sealed record class ApplicationSettings
{
[CommandProperty]
[CommandSummary("The endpoint of the console to run." +
"If omitted, one of the random ports will be used.")]
[CommandSummary("The endpoint of the libplanet-console. " +
"If omitted, a random endpoint is used.")]
[AppEndPoint]
public string EndPoint { get; init; } = string.Empty;

Expand All @@ -20,16 +20,15 @@ internal sealed record class ApplicationSettings
#else
[CommandProperty(InitValue = 4)]
#endif
[CommandSummary("The number of nodes to run.\n" +
"If omitted, the default value is 4.\n" +
"If --nodes option is set, this option is ignored.")]
[CommandSummary("The number of nodes to run. If omitted, 4 nodes are run.\n" +
"Mutually exclusive with '--nodes' option.")]
[CommandPropertyExclusion(nameof(Nodes))]
[JsonIgnore]
public int NodeCount { get; init; }

[CommandProperty]
[CommandSummary("The private keys of the nodes to run.\n" +
"Example: --nodes \"key1,key2,...\"")]
[CommandSummary("The private keys of the nodes to run. ex) --nodes \"key1,key2,...\"\n" +
"Mutually exclusive with '--node-count' option.")]
[CommandPropertyExclusion(nameof(NodeCount))]
[JsonIgnore]
public string[] Nodes { get; init; } = [];
Expand All @@ -39,26 +38,19 @@ internal sealed record class ApplicationSettings
#else
[CommandProperty(InitValue = 2)]
#endif
[CommandSummary("The number of clients to run.\n" +
"If omitted, the default value is 2.\n" +
"If --clients option is set, this option is ignored.")]
[CommandSummary("The number of clients to run. If omitted, 2 clients are run.\n" +
"Mutually exclusive with '--clients' option.")]
[CommandPropertyExclusion(nameof(Clients))]
[JsonIgnore]
public int ClientCount { get; init; }

[CommandProperty(InitValue = new string[] { })]
[CommandSummary("The private keys of the clients to run.\n" +
"Example: --clients \"key1,key2,...\"")]
[CommandSummary("The private keys of the clients to run. ex) --clients \"key1,key2,...\"\n" +
"Mutually exclusive with '--client-count' option.")]
[CommandPropertyExclusion(nameof(ClientCount))]
[JsonIgnore]
public string[] Clients { get; init; } = [];

[CommandProperty]
[CommandSummary("The directory path to store data of each node. " +
"If omitted, the data is stored in memory.")]
[JsonIgnore]
public string RepositoryPath { get; init; } = string.Empty;

[CommandProperty]
[CommandSummary("The directory path to store log.")]
public string LogPath { get; set; } = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

namespace LibplanetConsole.Consoles.Executable;

[CommandSummary("Run nodes and clients or provide related tools.")]
internal sealed class EntryCommandContext(params ICommand[] commands)
: CommandContextBase(commands)
{
protected override void OnEmptyExecute()
{
if (GetCommand(["help"]) is IExecutable executable)
{
executable.Execute();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.ComponentModel;
using LibplanetConsole.Common.Commands;

namespace LibplanetConsole.Consoles.Executable.EntryCommands;

[Category("Tools")]
internal sealed class GenesisCommand : GenesisCommandBase
{
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using JSSoft.Commands;
using LibplanetConsole.Common;
using LibplanetConsole.Common.DataAnnotations;
Expand All @@ -19,24 +20,14 @@ public InitializeCommand()
[CommandSummary("The directory path used to initialize a repository.")]
[Path(
Type = PathType.Directory, ExistsType = PathExistsType.NotExistOrEmpty)]
public string OutputPath { get; set; } = string.Empty;
public string RepositoryPath { get; set; } = string.Empty;

[CommandProperty]
[CommandSummary("The endpoint of the libplanet-console. " +
"If omitted, a random endpoint is used.")]
[AppEndPoint]
public string EndPoint { get; set; } = string.Empty;

[CommandProperty]
[CommandSummary("The private key of the genesis block. " +
"if omitted, a random private key is used.")]
[AppPrivateKey]
public string GenesisKey { get; set; } = string.Empty;

[CommandProperty("date-time")]
[CommandSummary("The timestamp of the genesis block. ex) \"2021-01-01T00:00:00Z\"")]
public DateTimeOffset DateTimeOffset { get; set; }

[CommandProperty(InitValue = 4)]
[CommandSummary("The number of nodes to create. If omitted, 4 nodes are created.\n" +
"Mutually exclusive with '--nodes' option.")]
Expand Down Expand Up @@ -67,15 +58,28 @@ public InitializeCommand()
[CommandSummary("If set, the command does not output any information.")]
public bool Quiet { get; set; }

[CommandProperty]
[CommandSummary("The private key of the genesis block. " +
"if omitted, a random private key is used.")]
[AppPrivateKey]
[Category("Genesis")]
public string GenesisKey { get; set; } = string.Empty;

[CommandProperty("date-time")]
[CommandSummary("The timestamp of the genesis block. ex) \"2021-01-01T00:00:00Z\"")]
[Category("Genesis")]
public DateTimeOffset DateTimeOffset { get; set; }

protected override void OnExecute()
{
var genesisKey = AppPrivateKey.ParseOrRandom(GenesisKey);
var endPoint = AppEndPoint.ParseOrNext(EndPoint);
var prevEndPoint = EndPoint != string.Empty ? endPoint : null;
var nodeOptions = GetNodeOptions(ref prevEndPoint);
var clientOptions = GetClientOptions(ref prevEndPoint);
var outputPath = Path.GetFullPath(OutputPath);
var dateTimeOffset = DateTimeOffset.UtcNow;
var outputPath = Path.GetFullPath(RepositoryPath);
var dateTimeOffset = DateTimeOffset != DateTimeOffset.MinValue
? DateTimeOffset : DateTimeOffset.UtcNow;
var repository = new Repository(endPoint, nodeOptions, clientOptions)
{
Genesis = BlockUtility.SerializeBlock(BlockUtility.CreateGenesisBlock(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.ComponentModel;
using LibplanetConsole.Common.Commands;

namespace LibplanetConsole.Consoles.Executable.EntryCommands;

[Category("Tools")]
internal sealed class KeyCommand : KeyCommandBase
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace LibplanetConsole.Consoles.Executable.EntryCommands;

[CommandSummary("Run the Libplanet console.")]
[CommandExample("run --end-point localhost:5000 --node-count 4 --client-count 2")]
internal sealed class RunCommand : CommandAsyncBase, ICustomCommandDescriptor
{
private readonly ApplicationSettingsCollection _settingsCollection = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using JSSoft.Commands;
using LibplanetConsole.DataAnnotations;

namespace LibplanetConsole.Consoles.Executable.EntryCommands;

[CommandSummary("Run the Libplanet console with repository path.")]
[CommandSummary("Run the libplanet-console using the given repository path.")]
internal sealed class StartCommand : CommandAsyncBase
{
[CommandPropertyRequired]
[CommandSummary("The path of the repository.")]
[Path(Type = PathType.Directory, ExistsType = PathExistsType.Exist)]
public string RepositoryPath { get; set; } = string.Empty;

protected override async Task OnExecuteAsync(CancellationToken cancellationToken)
Expand Down
4 changes: 2 additions & 2 deletions src/console/LibplanetConsole.Consoles.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

var commands = new ICommand[]
{
new RunCommand(),
new KeyCommand(),
new InitializeCommand(),
new StartCommand(),
new RunCommand(),
new KeyCommand(),
new GenesisCommand(),
};
var commandContext = new EntryCommandContext(commands);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace LibplanetConsole.Consoles.Commands;

[Export(typeof(ICommand))]
[method: ImportingConstructor]
[CommandSummary("Sends a transaction to store simple string.")]
[CommandSummary("Sends a transaction using a simple string.")]
internal sealed class TxCommand(ApplicationBase application) : CommandAsyncBase
{
[CommandPropertyRequired]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

namespace LibplanetConsole.Nodes.Executable;

[CommandSummary("Run a node or provide related tools.")]
internal sealed class EntryCommandContext(params ICommand[] commands)
: CommandContextBase(commands)
{
protected override void OnEmptyExecute()
{
if (GetCommand(["help"]) is IExecutable executable)
{
executable.Execute();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.ComponentModel;
using LibplanetConsole.Common.Commands;

namespace LibplanetConsole.Nodes.Executable.EntryCommands;

[Category("Tools")]
internal sealed class GenesisCommand : GenesisCommandBase
{
}
Loading

0 comments on commit 5d03c36

Please sign in to comment.