natemcmaster / CommandLineUtils

Command line parsing and utilities for .NET

Home Page:https://natemcmaster.github.io/CommandLineUtils/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

VersionOptionFromAssemblyAttributes - no documentation for template

digitaldias opened this issue · comments

I'm setting up a small app and wanted to customize the version string returned by --version.
I need help finding where in the documentation the template argument's content should look like.
Currently, I have this:

app.VersionOptionFromAssemblyAttributes("", Assembly.GetExecutingAssembly());

Which, of course, won't work! :)

Can someone please advise?

Checkout how app.VersionOptionFromAssemblyAttributes is implemented. It's a helper method that just calls .VersionOption. If you want to customize things, use .VersionOption instead

/// <summary>
/// Finds <see cref="AssemblyInformationalVersionAttribute"/> on <paramref name="assembly"/> and uses that
/// to set <see cref="CommandLineApplication.OptionVersion"/>.
/// <para>
/// Uses the Version that is part of the <see cref="AssemblyName"/> of the specified assembly
/// if no <see cref="AssemblyInformationalVersionAttribute"/> is applied.
/// </para>
/// </summary>
/// <param name="app"></param>
/// <param name="assembly"></param>
/// <exception cref="ArgumentNullException">Either <paramref name="app"/> or <paramref name="assembly"/> is <c>null</c>.</exception>
public static CommandOption VersionOptionFromAssemblyAttributes(this CommandLineApplication app, Assembly assembly)
=> VersionOptionFromAssemblyAttributes(app, Strings.DefaultVersionTemplate, assembly);
/// <summary>
/// Finds <see cref="AssemblyInformationalVersionAttribute"/> on <paramref name="assembly"/> and uses that
/// to set <see cref="CommandLineApplication.OptionVersion"/>.
/// <para>
/// Uses the Version that is part of the <see cref="AssemblyName"/> of the specified assembly
/// if no <see cref="AssemblyInformationalVersionAttribute"/> is applied.
/// </para>
/// </summary>
/// <param name="app"></param>
/// <param name="template"></param>
/// <param name="assembly"></param>
/// <exception cref="ArgumentNullException">Either <paramref name="app"/> or <paramref name="assembly"/> is <c>null</c>.</exception>
public static CommandOption VersionOptionFromAssemblyAttributes(this CommandLineApplication app, string template, Assembly assembly)
=> app.VersionOption(template, GetInformationalVersion(assembly));
private static string? GetInformationalVersion(Assembly assembly)
{
var infoVersion = assembly
?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion;
return string.IsNullOrWhiteSpace(infoVersion)
? assembly?.GetName().Version.ToString()
: infoVersion;
}

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

Closing due to inactivity.
If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.