Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ConsoleAppFramework/CommandHelpBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,24 @@ internal CommandHelpDefinition CreateCommandHelpDefinition(CommandDescriptor des
var isFlag = item.ParameterType == typeof(bool);

var defaultValue = default(string);
if (item.HasDefaultValue)
if (item.HasDefaultValue())
{
if (option?.DefaultValue != null)
{
defaultValue = option.DefaultValue;
}
else
{
defaultValue = (item.DefaultValue?.ToString() ?? "null");
defaultValue = (item.DefaultValue()?.ToString() ?? "null");
}
if (isFlag)
{
if (item.DefaultValue is true)
if (item.DefaultValue() is true)
{
// bool option with true default value is not flag.
isFlag = false;
}
else if (item.DefaultValue is false)
else if (item.DefaultValue() is false)
{
// false default value should be omitted for flag.
defaultValue = null;
Expand Down
25 changes: 16 additions & 9 deletions src/ConsoleAppFramework/ConsoleAppEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public async Task RunAsync()
if (commandDescriptor.CommandType == CommandType.DefaultCommand && args.Length == 0)
{
var p = commandDescriptor.MethodInfo.GetParameters();
if (p.Any(x => !(x.ParameterType == typeof(ConsoleAppContext) || isService.IsService(x.ParameterType) || x.HasDefaultValue)))
if (p.Any(x => !(x.ParameterType == typeof(ConsoleAppContext) || isService.IsService(x.ParameterType) || x.HasDefaultValue())))
{
options.CommandDescriptors.TryGetHelpMethod(out commandDescriptor);
}
Expand Down Expand Up @@ -294,7 +294,7 @@ bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsO
{
if (optionByIndex.Count <= option.Index)
{
if (!item.HasDefaultValue)
if (!item.HasDefaultValue())
{
throw new InvalidOperationException($"Required argument {option.Index} was not found in specified arguments.");
}
Expand Down Expand Up @@ -346,13 +346,20 @@ bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsO
var elemType = UnwrapCollectionElementType(parameters[i].ParameterType);
if (elemType == typeof(string))
{
if (!(v.StartsWith("\"") && v.EndsWith("\"")))
{
v = "[" + string.Join(",", v.Split(' ', ',').Select(x => "\"" + x + "\"")) + "]";
if (parameters.Length == i + 1)
{
v = "[" + string.Join(",", optionByIndex.Skip(parameters[i].Position).Select(x => "\"" + x.Value + "\"")) + "]";
}
else
{
v = "[" + v + "]";
{
if (!(v.StartsWith("\"") && v.EndsWith("\"")))
{
v = "[" + string.Join(",", v.Split(' ', ',').Select(x => "\"" + x + "\"")) + "]";
}
else
{
v = "[" + v + "]";
}
}
}
else
Expand Down Expand Up @@ -401,9 +408,9 @@ bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsO
}
}

if (item.HasDefaultValue)
if (item.HasDefaultValue())
{
invokeArgs[i] = item.DefaultValue;
invokeArgs[i] = item.DefaultValue();
}
else if (item.ParameterType == typeof(bool))
{
Expand Down
18 changes: 18 additions & 0 deletions src/ConsoleAppFramework/ParameterInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Linq;
using System.Reflection;

namespace ConsoleAppFramework
{
public static class ParameterInfoExtensions
{
public static bool HasDefaultValue(this ParameterInfo pi)
=> pi.HasDefaultValue
|| pi.CustomAttributes.Any(a => a.AttributeType == typeof(ParamArrayAttribute));

public static object? DefaultValue(this ParameterInfo pi)
=> pi.HasDefaultValue
? pi.DefaultValue
: Array.CreateInstance(pi.ParameterType.GetElementType()!, 0);
}
}