Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VersionOptionFromAssemblyAttributes - no documentation for template #535

Open
digitaldias opened this issue Jun 11, 2023 · 1 comment
Open
Labels

Comments

@digitaldias
Copy link

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?

@natemcmaster
Copy link
Owner

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants