Skip to content

Customize --version output #2009

@KalleOlaviNiemitalo

Description

@KalleOlaviNiemitalo

I'd like to have --version output some additional information, like in these:

PS C:\> MSBuild.exe /version
Microsoft (R) Build Engine version 15.9.21+g9802d43bc3 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

15.9.21.664
$ gcc --version
gcc.exe (x86_64-posix-sjlj, built by strawberryperl.com project) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

That is however somewhat cumbersome to implement on top of System.Commandline, because then the application:

  • Cannot call CommandLineBuilderExtensions.UseDefaults, which would add another --version option and cause an exception. Must instead call UseEnvironmentVariableDirective etc. one by one.
  • Must hardcode the value of MiddlewareOrderInternal.VersionOption = -1200.
  • Must reimplement the VersionOptionCannotBeCombinedWithOtherArguments validation.

Can we have API like this:

 namespace System.CommandLine.Builder
 {
     partial class CommandLineBuilderExtensions
     {
         public static CommandLineBuilder UseVersionOption(this CommandLineBuilder builder);
         public static CommandLineBuilder UseVersionOption(this CommandLineBuilder builder, params string[] aliases);
+        public static CommandLineBuilder UseVersionOption(this CommandLineBuilder builder, Action<InvocationContext> handler);
+        public static CommandLineBuilder UseVersionOption(this CommandLineBuilder builder, Action<InvocationContext> handler, params string[] aliases);
     }
 }

The provided handler would then be responsible of all output, and could set InvocationContext.ExitCode if desired.

Overloads with Func<InvocationContext, Task> could be added later if needed, but I don't think they would be needed.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions