Skip to content

Commit 98aa9ae

Browse files
committed
Add [Option(DefaultValue=)]
1 parent 7d4cce1 commit 98aa9ae

File tree

8 files changed

+68
-9
lines changed

8 files changed

+68
-9
lines changed

ConsoleAppFramework.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
3939
ReadMe.md = ReadMe.md
4040
EndProjectSection
4141
EndProject
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAppFramework.Template", "src\ConsoleAppFramework.Template\ConsoleAppFramework.Template.csproj", "{A76D1521-AB66-4B00-8C7B-069052EFB212}"
43+
EndProject
4244
Global
4345
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4446
Debug|Any CPU = Debug|Any CPU
@@ -77,6 +79,10 @@ Global
7779
{6A39E146-8CDF-4B04-88ED-395C56A32722}.Debug|Any CPU.Build.0 = Debug|Any CPU
7880
{6A39E146-8CDF-4B04-88ED-395C56A32722}.Release|Any CPU.ActiveCfg = Release|Any CPU
7981
{6A39E146-8CDF-4B04-88ED-395C56A32722}.Release|Any CPU.Build.0 = Release|Any CPU
82+
{A76D1521-AB66-4B00-8C7B-069052EFB212}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
83+
{A76D1521-AB66-4B00-8C7B-069052EFB212}.Debug|Any CPU.Build.0 = Debug|Any CPU
84+
{A76D1521-AB66-4B00-8C7B-069052EFB212}.Release|Any CPU.ActiveCfg = Release|Any CPU
85+
{A76D1521-AB66-4B00-8C7B-069052EFB212}.Release|Any CPU.Build.0 = Release|Any CPU
8086
EndGlobalSection
8187
GlobalSection(SolutionProperties) = preSolution
8288
HideSolutionNode = FALSE
@@ -90,6 +96,7 @@ Global
9096
{9AC1CAE2-E717-472A-BBFB-0FE5590E5C7A} = {1F399F98-7439-4F05-847B-CC1267B4B7F2}
9197
{2B7CDEFC-3D92-4B72-8898-2494D7B087AD} = {A2CF2984-E8E2-48FC-B5A1-58D74A2467E6}
9298
{6A39E146-8CDF-4B04-88ED-395C56A32722} = {AAD2D900-C305-4449-A9FC-6C7696FFEDFA}
99+
{A76D1521-AB66-4B00-8C7B-069052EFB212} = {1F399F98-7439-4F05-847B-CC1267B4B7F2}
93100
EndGlobalSection
94101
GlobalSection(ExtensibilityGlobals) = postSolution
95102
SolutionGuid = {7F3E353A-C125-4020-8481-11DC6496358C}

sandbox/MultiContainedApp/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static async Task Main(string[] args)
1616
//args = new string[] { "foo", "echo", "help"};
1717
//args = new string[] { "bar.hello2", "help" };
1818

19+
1920
await Host.CreateDefaultBuilder()
2021
.ConfigureLogging(x => x.SetMinimumLevel(LogLevel.Trace))
2122
.RunConsoleAppFrameworkAsync(args, options: new ConsoleAppOptions

sandbox/SingleContainedApp/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public class Program : ConsoleAppBase
222222
static async Task Main(string[] args)
223223
{
224224
//args = new[] { "-m", "a ", "-b", "False" };
225+
args = new[] { "help" };
225226

226227
await Host.CreateDefaultBuilder().RunConsoleAppFrameworkAsync<Program>(args, new ConsoleAppOptions
227228
{
@@ -231,9 +232,12 @@ static async Task Main(string[] args)
231232

232233
}
233234

234-
public void Hello([Option(null, "Message to display.")]string message, bool b)
235+
public void Hello(
236+
int? foo = null,
237+
[Option("", "", DefaultValue = "DateTime.Today")]DateTime? hello = null)
235238
{
236-
Console.WriteLine(message + b);
239+
if (hello == null) hello = DateTime.Now;
240+
Console.WriteLine(hello);
237241
}
238242
}
239243
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace ConsoleAppFramework.Template
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Console.WriteLine("Hello World!");
10+
}
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
using System.Text;
5+
6+
[assembly:InternalsVisibleTo("ConsoleAppFramework.Tests, PublicKey="+
7+
"002400000480000094000000060200000024000052534131000400000100010089b12412c27f5f"+
8+
"aece3868b659bf311f2550c5cc1b4cbbe032a048ec36fa288872d51e8ddfd77a83e835c6ea3940"+
9+
"c331fe89d1ba9146a12abed588f194cd437cfe81252634d49214acf6b11dc9e97cfc6d0f818082"+
10+
"e5bbafb50e890eed19517c199213075b294d5fa59556cd42186041a1a95f8cff3869575bed4de2"+
11+
"dd8f5dc2")]

src/ConsoleAppFramework/CommandHelpBuilder.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public string BuildOptionsMessage(CommandHelpDefinition definition)
214214
sb.Append(opt.Description);
215215

216216
if (opt.IsFlag)
217-
{
217+
{
218218
sb.Append($" (Optional)");
219219
}
220220
else if (opt.DefaultValue != null)
@@ -328,23 +328,36 @@ public CommandHelpDefinition CreateCommandHelpDefinition(MethodInfo method)
328328
var defaultValue = default(string);
329329
if (item.HasDefaultValue)
330330
{
331-
defaultValue = (item.DefaultValue?.ToString() ?? "null");
331+
if (option?.DefaultValue != null)
332+
{
333+
defaultValue = option.DefaultValue;
334+
}
335+
else
336+
{
337+
defaultValue = (item.DefaultValue?.ToString() ?? "null");
338+
}
332339
if (isFlag)
333-
{
340+
{
334341
if (item.DefaultValue is true)
335342
{
336343
// bool option with true default value is not flag.
337344
isFlag = false;
338345
}
339-
else if(item.DefaultValue is false)
340-
{
346+
else if (item.DefaultValue is false)
347+
{
341348
// false default value should be omitted for flag.
342349
defaultValue = null;
343-
}
350+
}
344351
}
345352
}
346353

347-
parameterDefinitions.Add(new CommandOptionHelpDefinition(options.Distinct().ToArray(), description, item.ParameterType.Name, defaultValue, index, isFlag));
354+
var paramTypeName = item.ParameterType.Name;
355+
if (item.ParameterType.IsGenericType && item.ParameterType.GetGenericTypeDefinition() == typeof(Nullable<>))
356+
{
357+
paramTypeName = item.ParameterType.GetGenericArguments()[0].Name + "?";
358+
}
359+
360+
parameterDefinitions.Add(new CommandOptionHelpDefinition(options.Distinct().ToArray(), description, paramTypeName, defaultValue, index, isFlag));
348361
}
349362

350363
return new CommandHelpDefinition(

src/ConsoleAppFramework/OptionAttribute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public class OptionAttribute : Attribute
99
public string? ShortName { get; }
1010
public string? Description { get; }
1111

12+
/// <summary>Override default value on help.</summary>
13+
public string? DefaultValue { get; set; }
14+
1215
public OptionAttribute(int index)
1316
{
1417
this.Index = index;

0 commit comments

Comments
 (0)