Skip to content

Commit 58b2a19

Browse files
committed
Fixed problem with help options being treated as potentially supporting parameters. Updated examples to demonstrate handling of unkeyed parameters.
1 parent d0f17c0 commit 58b2a19

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

J4JCommandLine/allocating/Allocator.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Allocator : IAllocator
1313
private readonly IElementKey _prefixer;
1414

1515
private StringComparison _keyComp;
16+
private MasterTextCollection _masterText;
1617

1718
public Allocator(
1819
IElementTerminator terminator,
@@ -31,10 +32,11 @@ public bool Initialize(
3132
MasterTextCollection masterText )
3233
{
3334
_keyComp = keyComp;
35+
_masterText = masterText;
3436

35-
_prefixer.Initialize( keyComp, logger, masterText );
37+
_prefixer.Initialize( keyComp, logger, _masterText );
3638

37-
_terminator.Initialize(keyComp, logger, masterText);
39+
_terminator.Initialize(keyComp, logger, _masterText);
3840

3941
return _prefixer.IsInitialized && _terminator.IsInitialized;
4042
}
@@ -79,10 +81,15 @@ public Allocations AllocateCommandLine( string cmdLine )
7981
if( !retVal.Contains( element ) )
8082
retVal.Add( new Allocation( retVal ) { Key = element } );
8183

82-
// store a reference to the current/active Allocation
83-
curResult = retVal[ element ];
84-
85-
lastElementWasKey = true;
84+
// store a reference to the current/active Allocation unless the
85+
// element is a request for help (because help options cannot have
86+
// parameters)
87+
if( !_masterText.Contains( element, TextUsageType.HelpOptionKey ) )
88+
{
89+
curResult = retVal[ element ];
90+
lastElementWasKey = true;
91+
}
92+
else lastElementWasKey = false;
8693
}
8794
else
8895
{
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
namespace InstancePropertyExample
1+
using System.Collections.Generic;
2+
3+
namespace InstancePropertyExample
24
{
35
public class Configuration
46
{
57
public int IntValue { get; set; }
68
public string TextValue { get; set; }
9+
public List<string> Unkeyed { get; set; }
710
}
811
}

examples/InstancePropertyExample/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static void Main(string[] args)
3535
.SetDescription("a text value")
3636
.SetDefaultValue("some text value");
3737

38+
binder.BindUnkeyed(x => x.Unkeyed);
39+
3840
if (!binder.Parse(args))
3941
{
4042
Environment.ExitCode = 1;
@@ -43,6 +45,10 @@ static void Main(string[] args)
4345

4446
Console.WriteLine($"IntValue is {binder.Value.IntValue}");
4547
Console.WriteLine($"TextValue is {binder.Value.TextValue}");
48+
49+
Console.WriteLine(binder.Value.Unkeyed.Count == 0
50+
? "No unkeyed parameters"
51+
: $"Unkeyed parameters: {string.Join(", ", binder.Value.Unkeyed)}");
4652
}
4753

4854
private static void InitializeServiceProvider()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"InstancePropertyExample": {
4+
"commandName": "Project",
5+
"commandLineArgs": "-i 5 -h abc def"
6+
}
7+
}
8+
}

examples/StaticPropertyExample/Program.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using Alba.CsConsoleFormat;
24
using Autofac;
35
using Autofac.Extensions.DependencyInjection;
46
using J4JSoftware.CommandLine;
@@ -33,6 +35,8 @@ static void Main(string[] args)
3335
.SetDescription( "a text value" )
3436
.SetDefaultValue( "some text value" );
3537

38+
binder.BindUnkeyed( x => Program.Unkeyed );
39+
3640
if( !binder.Parse( args ) )
3741
{
3842
Environment.ExitCode = 1;
@@ -41,11 +45,16 @@ static void Main(string[] args)
4145

4246
Console.WriteLine($"IntValue is {IntValue}");
4347
Console.WriteLine($"TextValue is {TextValue}");
48+
49+
Console.WriteLine( Unkeyed.Count == 0
50+
? "No unkeyed parameters"
51+
: $"Unkeyed parameters: {string.Join( ", ", Unkeyed )}" );
4452
}
4553

4654
public static IServiceProvider ServiceProvider { get; set; }
4755
public static int IntValue { get; set; }
4856
public static string TextValue { get; set; }
57+
public static List<string> Unkeyed { get; set; }
4958

5059
private static void InitializeServiceProvider()
5160
{

examples/StaticPropertyExample/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"ConsoleAppJ4JCmdLine": {
44
"commandName": "Project",
5-
"commandLineArgs": "-i -5 -h"
5+
"commandLineArgs": "-i 5 -h abc def"
66
}
77
}
88
}

0 commit comments

Comments
 (0)