You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ReadMe.md
+110Lines changed: 110 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -269,6 +269,27 @@ Unfortunately, due to current C# specifications, lambda expressions and [local f
269
269
270
270
In addition to `-h|--help`, there is another special built-in option: `--version`. In default, it displays the `AssemblyInformationalVersion` without source revision or `AssemblyVersion`. You can configure version string by `ConsoleApp.Version`, for example `ConsoleApp.Version = "2001.9.3f14-preview2";`.
271
271
272
+
When a default value exists, that value is displayed by default. If you want to omit this display, add the `[HideDefaultValue]` attribute.
Additionally, if you add the `[Hidden]` attribute, the help for that parameter itself will be hidden.
292
+
272
293
Command
273
294
---
274
295
If you want to register multiple commands or perform complex operations (such as adding filters), instead of using `Run/RunAsync`, obtain the `ConsoleAppBuilder` using `ConsoleApp.Create()`. Call `Add`, `Add<T>`, or `UseFilter<T>` multiple times on the `ConsoleAppBuilder` to register commands and filters, and finally execute the application using `Run` or `RunAsync`.
@@ -574,6 +595,93 @@ If you want to change the deserialization options, you can set `JsonSerializerOp
574
595
575
596
> NOTE: If they are not set when NativeAOT is used, a runtime exception may occur. If they are included in the parsing process, be sure to set source generated options.
576
597
598
+
### GlobalOptions
599
+
600
+
By calling `ConfigureGlobalOptions` on `ConsoleAppBuilder`, you can define global options that are enabled for all commands. For example, `--dry-run` or `--verbose` are suitable as common options.
> NOTE: `(ref builder)` can be written in C# 14 and later. For earlier versions, you need to write `(ref ConsoleApp.GlobalOptionsBuilder builder)`.
623
+
624
+
With `AddGlobalOption<T>`, you can define optional global options, and with `AddRequiredGlobalOption<T>`, you can define required global options. Each of these is also reflected in the command's help.
625
+
626
+
To define name aliases, separate them with `|` like `-v|--verbose`.
627
+
628
+
Unlike command parameters, the supported types are limited to types that can define C# compile-time constants (`string`, `char`, `sbyte`, `byte`, `short`, `int`, `long`, `uint`, `ushort`, `ulong`, `decimal`, `float`, `double`, `Enum`) and their `Nullable<T>` only.
629
+
630
+
Parsed global options can be retrieved from `ConsoleAppContext.GlobalOptions`. Additionally, when combined with DI, you can retrieve them in a typed manner in each command.
To perform custom binding to existing types that do not support `ISpanParsable<T>`, you can create and set up a custom parser. For example, if you want to pass `System.Numerics.Vector3` as a comma-separated string like `1.3,4.12,5.947` and parse it, you can create an `Attribute` with `AttributeTargets.Parameter` that implements `IArgumentParser<T>`'s `static bool TryParse(ReadOnlySpan<char> s, out Vector3 result)` as follows:
@@ -1074,6 +1182,8 @@ When `Microsoft.Extensions.Configuration` is imported, `ConfigureEmptyConfigurat
1074
1182
1075
1183
Furthermore, overloads of `Action<IConfiguration, IServiceCollection> configure` and `Action<IConfiguration, ILoggingBuilder> configure` are added to `ConfigureServices` and `ConfigureLogging`, allowing you to retrieve the Configuration when executing the delegate.
1076
1184
1185
+
There are also overloads that receive ConsoleAppContext. If you want to obtain global options and build with them, you can use these overloads: `Action<ConsoleAppContext, IConfiguration, IServiceCollection>` and `Action<ConsoleAppContext, IConfiguration, ILoggingBuilder>`.
1186
+
1077
1187
without Hosting dependency, I've preferred these import packages.
0 commit comments