Skip to content

Commit 5af9745

Browse files
committed
+ Added more descriptive help for default parameters
1 parent 01990a8 commit 5af9745

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

src/ExampleApp/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FlowCommandLine;
2+
using static FlowCommandLine.FlowCommandParameter;
23

34
await CommandLine.Console ()
45
.Application ( "Console Application", "1.0.0", "Application full description.", "Copyright (c) Macroloft All right Reserved", "conapp" )
@@ -9,10 +10,10 @@ await CommandLine.Console ()
910
Console.WriteLine ( "Param2: " + parameters.Param2 );
1011
},
1112
"Command description",
12-
new List<FlowCommandParameter> {
13-
FlowCommandParameter.CreateRequired(alias: "param1", help: "parameter description"),
14-
FlowCommandParameter.Create(alias: "param2", help: "parameter2 description"),
15-
}
13+
[
14+
CreateDefault(alias: "param1", help: "parameter description"),
15+
Create(alias: "param2", help: "parameter2 description"),
16+
]
1617
)
1718
.RunCommandAsync ();
1819

src/FlowCommandLine/CommandLine.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,30 @@ private void ShowCommandHelp ( string description, List<FlowCommandParameter> pa
251251
}
252252

253253
private void ShowParameters ( List<FlowCommandParameter> commandParameters ) {
254-
if ( commandParameters.Any ( a => a.Required ) ) {
254+
var defaultParameters = commandParameters.Where ( a => a.Default ).ToArray ();
255+
if ( defaultParameters.Any () ) {
256+
var allDefaultParameters = string.Join (
257+
" ",
258+
defaultParameters
259+
.Select (
260+
a =>
261+
string.Join ( '/', new List<string> { a.ShortName, a.FullName, a.PropertyName }.Where ( a => !string.IsNullOrEmpty ( a ) ) )
262+
)
263+
.ToArray ()
264+
);
265+
m_commandLineProvider.WriteLine ( $"Default parameters in follow order: {allDefaultParameters}" );
266+
foreach ( var defaultParameter in defaultParameters ) {
267+
m_commandLineProvider.WriteLine (
268+
" " + string.Join (
269+
'/',
270+
new List<string> { defaultParameter.ShortName, defaultParameter.FullName, defaultParameter.PropertyName }
271+
.Where ( a => !string.IsNullOrEmpty ( a ) )
272+
) + " " + defaultParameter.Description
273+
);
274+
}
275+
m_commandLineProvider.WriteLine ( "" );
276+
}
277+
if ( commandParameters.Any ( a => a.Required && !a.Default ) ) {
255278
m_commandLineProvider.WriteLine ( "The following arguments are available:" );
256279
var requiredParameters = commandParameters
257280
.Where ( a => a.Required )
@@ -291,6 +314,13 @@ private void ShowParameters ( List<FlowCommandParameter> commandParameters ) {
291314
}
292315
}
293316

317+
private void ShowApplicationUsage () {
318+
if ( !string.IsNullOrEmpty ( m_applicationExecutable ) ) {
319+
m_commandLineProvider.WriteLine ( $"usage: {m_applicationExecutable} [<command>] [<parameters>]" );
320+
m_commandLineProvider.WriteLine ( " " );
321+
}
322+
}
323+
294324
/// <summary>
295325
/// Show application help and a list of available commands.
296326
/// </summary>
@@ -303,14 +333,12 @@ private void ShowHelp ( List<string> parts, bool fullInfo = false ) {
303333
m_commandLineProvider.WriteLine ( m_applicationDescription );
304334
m_commandLineProvider.WriteLine ( " " );
305335
}
306-
if ( !string.IsNullOrEmpty ( m_applicationExecutable ) ) {
307-
m_commandLineProvider.WriteLine ( $"usage: {m_applicationExecutable} [<command>] [<parameters>]" );
308-
m_commandLineProvider.WriteLine ( " " );
309-
}
310336

311337
if ( m_commands.Any () || m_asyncCommands.Any () ) {
312338
if ( parts.Any ( IsHelpParameter ) && ShowHelpBySingleCommand ( parts ) ) return;
313339

340+
ShowApplicationUsage ();
341+
314342
var maximumLength = m_commands
315343
.Select ( a => a.Key )
316344
.Concat ( m_asyncCommands.Select ( b => b.Key ) )
@@ -336,6 +364,8 @@ private void ShowHelp ( List<string> parts, bool fullInfo = false ) {
336364
} else {
337365
if ( !parts.Any () ) return;
338366

367+
ShowApplicationUsage ();
368+
339369
var command = parts.First ();
340370
if ( m_commands.ContainsKey ( command ) || m_asyncCommands.ContainsKey ( command ) ) {
341371
m_commandLineProvider.WriteLine ( $" " );
@@ -363,16 +393,23 @@ private void ShowOptionsHelp () {
363393
if ( m_options.Any () ) ShowParameters ( m_options );
364394
}
365395

396+
private void ShowCommandUsage ( string command, bool haveDefault ) {
397+
m_commandLineProvider.WriteLine ( $"usage: {m_applicationExecutable} {command}{( haveDefault ? " [default parameters]" : "" )} [<parameters>]" );
398+
m_commandLineProvider.WriteLine ( " " );
399+
}
400+
366401
private bool ShowHelpBySingleCommand ( List<string> parts ) {
367402
var command = parts.First ();
368403

369404
if ( m_commands.ContainsKey ( command ) ) {
370405
var selectedCommand = m_commands[command];
406+
ShowCommandUsage ( command, selectedCommand.Parameters.Any ( a => a.Default ) );
371407
ShowCommandHelp ( selectedCommand.Description, selectedCommand.Parameters );
372408
return true;
373409
}
374410
if ( m_asyncCommands.ContainsKey ( command ) ) {
375411
var selectedCommand = m_asyncCommands[command];
412+
ShowCommandUsage ( command, selectedCommand.Parameters.Any ( a => a.Default ) );
376413
ShowCommandHelp ( selectedCommand.Description, selectedCommand.Parameters );
377414
return true;
378415
}

src/FlowCommandLine/FlowCommandLine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageTags>command,line,commandline,command-line,parse,parser,parsing,shell</PackageTags>
1717
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
19-
<Version>1.0.11.0</Version>
19+
<Version>1.0.12.0</Version>
2020
<IsTrimmable>true</IsTrimmable>
2121
<PublishTrimmed>true</PublishTrimmed>
2222
<PackageReleaseNotes>

0 commit comments

Comments
 (0)