Skip to content

Commit

Permalink
Customizations for CodegenCS:
Browse files Browse the repository at this point in the history
- Support for "--p:myoption", "-p:myoption", "/p:myoption"
- HelpBuilder.Default: OptionsSection() only after SubcommandsSection() (since most options are specific to the subcommand, not global options)
- HelpBuilder: do not trim second column (our custom HelpBuilder will use linebreaks at Options to force linebreaks in the help text)
- HelpBuilder: GetUsage as public virtual to allow our custom HelpBuilder to override
  • Loading branch information
Drizin committed Aug 14, 2022
1 parent 4b60585 commit b78690b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ public void Explicitly_configured_default_values_can_be_bound_by_name_to_constru
instance.StringOption.Should().Be("the default");
}


public class MyClass
{
public string MyOption { get; set; }
}

[Fact]
public void TestPOptions()
{
var option = new Option<string>("-p:myoption", () => "the default");

var command = new Command("the-command");
command.AddOption(option);
var binder = new ModelBinder(typeof(MyClass));

var parser = new Parser(command);
var bindingContext = new InvocationContext(parser.Parse("-p:myoption=test")).BindingContext;

var instance = (MyClass)binder.CreateInstance(bindingContext);

instance.MyOption.Should().Be("test");
}

[Theory]
[InlineData(typeof(string), "--value hello", "hello")]
[InlineData(typeof(int), "--value 123", 123)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ static int IndexAfterPrefix(string alias)
{
switch (alias[0])
{
case '-' when alias.Length > 2 && alias[1] == 'p' && alias[2] == ':':
return 3;
case '-' when alias.Length > 1 && alias[1] == '-':
return 2;
case '-':
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Help/HelpBuilder.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ public static IEnumerable<HelpSectionDelegate> GetLayout()
yield return SynopsisSection();
yield return CommandUsageSection();
yield return CommandArgumentsSection();
yield return OptionsSection();
yield return SubcommandsSection();
yield return OptionsSection();
yield return AdditionalArgumentsSection();
}

Expand Down
9 changes: 5 additions & 4 deletions src/System.CommandLine/Help/HelpBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void CustomizeLayout(Func<HelpContext, IEnumerable<HelpSectionDelegate>>
_getLayout = getLayout ?? throw new ArgumentNullException(nameof(getLayout));
}

private string GetUsage(Command command)
public virtual string GetUsage(Command command)
{
return string.Join(" ", GetUsageParts().Where(x => !string.IsNullOrWhiteSpace(x)));

Expand Down Expand Up @@ -141,12 +141,13 @@ IEnumerable<string> GetUsageParts()
}

displayOptionTitle = displayOptionTitle || command.Options.Any(x => !x.IsHidden);

if (displayOptionTitle)
{
yield return LocalizationResources.HelpUsageOptions();
yield return LocalizationResources.HelpUsageOptions();
}


if (!command.TreatUnmatchedTokensAsErrors)
{
yield return LocalizationResources.HelpUsageAdditionalArguments();
Expand Down Expand Up @@ -460,7 +461,7 @@ TwoColumnHelpRow GetCommandArgumentRow(Argument argument)
? $"[{GetArgumentDefaultValue(context.Command, argument, true, context)}]"
: "";

var secondColumnText = $"{argumentDescription} {defaultValueDescription}".Trim();
var secondColumnText = $"{argumentDescription} {defaultValueDescription}";

return new TwoColumnHelpRow(firstColumnText, secondColumnText);
}
Expand Down
30 changes: 29 additions & 1 deletion src/System.CommandLine/Parsing/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ internal static int GetPrefixLength(this string alias)
{
if (alias[0] == '-')
{
if (alias.Length > 3 && alias[1] == '-' && alias[2] == 'p' && alias[3] == ':')
return 4;
if (alias.Length > 2 && alias[1] == 'p' && alias[2] == ':')
return 3;
return alias.Length > 1 && alias[1] == '-'
? 2
: 1;
}

if (alias[0] == '/')
{
if (alias.Length > 2 && alias[1] == 'p' && alias[2] == ':')
return 3;
return 1;
}

Expand All @@ -61,9 +67,24 @@ internal static (string? Prefix, string Alias) SplitPrefix(this string rawAlias)
{
return ("--", rawAlias.Substring(2));
}
if (rawAlias.Length > 3 && rawAlias[1] == '-' && rawAlias[2] == 'p' && rawAlias[3] == ':')
{
return ("--p:", rawAlias.Substring(4));
}
if (rawAlias.Length > 2 && rawAlias[1] == 'p' && rawAlias[2] == ':')
{
return ("-p:", rawAlias.Substring(3));
}

return ("-", rawAlias.Substring(1));
}
else if (rawAlias[0] == '/')
{
if (rawAlias.Length > 2 && rawAlias[1] == 'p' && rawAlias[2] == ':')
{
return ("/p:", rawAlias.Substring(3));
}
}

return (null, rawAlias);
}
Expand Down Expand Up @@ -345,7 +366,14 @@ internal static bool TrySplitIntoSubtokens(
out string first,
out string? rest)
{
var i = arg.AsSpan().IndexOfAny(':', '=');
int i = -1;
if (arg.StartsWith("--p:") || arg.StartsWith("-p:") || arg.StartsWith("/p:"))
{
i = arg.AsSpan().IndexOf('=');
}

if (i < 0)
i = arg.AsSpan().IndexOfAny(':', '=');

if (i >= 0)
{
Expand Down

0 comments on commit b78690b

Please sign in to comment.