Altinn.Authorization.CommandLine.Azure 1.0.0

dotnet add package Altinn.Authorization.CommandLine.Azure --version 1.0.0
                    
NuGet\Install-Package Altinn.Authorization.CommandLine.Azure -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Altinn.Authorization.CommandLine.Azure" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Altinn.Authorization.CommandLine.Azure" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Altinn.Authorization.CommandLine.Azure" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Altinn.Authorization.CommandLine.Azure --version 1.0.0
                    
#r "nuget: Altinn.Authorization.CommandLine.Azure, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Altinn.Authorization.CommandLine.Azure@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Altinn.Authorization.CommandLine.Azure&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Altinn.Authorization.CommandLine.Azure&version=1.0.0
                    
Install as a Cake Tool

Altinn.Authorization.CommandLine

Altinn.Authorization.CommandLine is a small framework for building hosted command line applications. It combines System.CommandLine, Microsoft.Extensions.Hosting, dependency injection, and Spectre.Console-backed output behind a simple application builder.

Installation

Install the package with the .NET CLI:

dotnet add package Altinn.Authorization.CommandLine

Or with the NuGet Package Manager Console:

Install-Package Altinn.Authorization.CommandLine

Usage

Create a builder, register any application services, build the CliApplication, add commands, and run the application with the process arguments.

using Altinn.Authorization.CommandLine;
using Microsoft.Extensions.Logging;

var builder = CliApplication.CreateBuilder("Example command line application");
var cli = builder.Build();

cli.AddCommand("hello", "Writes a greeting", (
    [Argument] string name,
    ILogger<Program> logger) =>
{
    logger.LogInformation("Hello, {name}!", name);
});

return await cli.RunAsync(args);

Commands can also be implemented as handler types. The handler is constructed from the application service provider for each invocation and disposed after it runs.

using Altinn.Authorization.CommandLine;
using Altinn.Authorization.CommandLine.Results;
using Microsoft.Extensions.Logging;
using Spectre.Console;

var builder = CliApplication.CreateBuilder("Example command line application");
var cli = builder.Build();

cli.AddCommand<GreetCommand>("hello", "Writes a greeting");

return await cli.RunAsync(args);

internal sealed class GreetCommand(ILogger<GreetCommand> logger)
{
    public GreetingResult Invoke(
        [Option("--times", "-t")] int times = 5,
        [Argument] string message = "Hello, world!")
    {
        for (var i = 0; i < times; i++)
        {
            logger.LogInformation("Greeting: {message}", message);
        }

        return new GreetingResult(message);
    }
}

internal sealed class GreetingResult(string message)
    : ICommandResult
{
    public Task Execute(CommandInvocationContext context, CancellationToken cancellationToken = default)
    {
        context.Console.WriteLine(message);
        return Task.CompletedTask;
    }
}

Parameter Binding

Handler parameters are bound by the command handler factory:

  • [Argument] binds a parameter from a command line argument.
  • [Option] binds a parameter from a command line option. The default option name is --{parameterName}.
  • CommandInvocationContext, CancellationToken, IConsole, ParseResult, and IServiceProvider are injected from the current invocation.
  • Service parameters are resolved from dependency injection when the service provider reports that the parameter type is registered.
  • Custom parameter attributes can implement IFromArgumentMetadata, IFromOptionMetadata, or IFromServiceMetadata for specialized binding.

Parameters with default values are treated as optional. Nullable parameters are also optional according to the parameter nullability metadata.

Return Values

Handlers can return:

  • void, Task, or ValueTask.
  • T, Task<T>, or ValueTask<T> when an ICommandResultHandlerResolver can resolve a handler for T.
  • ICommandResult, which is executed directly.

An int result handler is registered by default and maps the returned value to the command return code.

Middleware

Commands expose a middleware pipeline through ICommandBuilder.Middleware.

cli.Middleware.Add(async (context, next, cancellationToken) =>
{
    var started = DateTimeOffset.UtcNow;
    await next(context, cancellationToken);
    var elapsed = DateTimeOffset.UtcNow - started;
    context.Console.MarkupLineInterpolated($"Completed in {elapsed.TotalMilliseconds:N0} ms");
});

Contributing

Contributions to Altinn.Authorization.CommandLine are welcome. Open an issue for bugs or feature requests, or submit a pull request with the proposed changes.

License

Altinn.Authorization.CommandLine is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Altinn.Authorization.CommandLine.Azure:

Package Downloads
Altinn.Authorization.CommandLine.Azure.KeyVault

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 66 8/25/2026

# Changelog

## 1.0.0 (2026-08-21)


### Features

* add argument preprocessor support ([#659](https://github.com/Altinn/altinn-authorization-utils/issues/659)) ([61e0466](https://github.com/Altinn/altinn-authorization-utils/commit/61e0466dff0f745adf1d5349e29df5548dd42b8c))
* add command execution helper ([#654](https://github.com/Altinn/altinn-authorization-utils/issues/654)) ([ccee390](https://github.com/Altinn/altinn-authorization-utils/commit/ccee39086a94f152b062a11de9c942c4ff6b78b0))
* add formatting handling to the cli ([#642](https://github.com/Altinn/altinn-authorization-utils/issues/642)) ([6b26269](https://github.com/Altinn/altinn-authorization-utils/commit/6b26269de06aa2c4b7447d3ac6ca743bd156563a))
* add invocation extensions ([#647](https://github.com/Altinn/altinn-authorization-utils/issues/647)) ([8126203](https://github.com/Altinn/altinn-authorization-utils/commit/81262036272b3e029299fde10782af6eac9d3a08))
* add json pretty printing ([#641](https://github.com/Altinn/altinn-authorization-utils/issues/641)) ([487ec3a](https://github.com/Altinn/altinn-authorization-utils/commit/487ec3ac3c585495c10683b19046d31fe8783af8))
* add simple console pretty-printer ([#640](https://github.com/Altinn/altinn-authorization-utils/issues/640)) ([30c9728](https://github.com/Altinn/altinn-authorization-utils/commit/30c97280112d1783c7251d2a4de4a971726a33a0))
* allow command-factory more binding patters ([#643](https://github.com/Altinn/altinn-authorization-utils/issues/643)) ([0153c3f](https://github.com/Altinn/altinn-authorization-utils/commit/0153c3fdcd192b89f73b89ed3b8a29d07d40c777))
* async state ([#661](https://github.com/Altinn/altinn-authorization-utils/issues/661)) ([b78928d](https://github.com/Altinn/altinn-authorization-utils/commit/b78928d0654d798cb2857628756cf6529e2b8a5e))
* command metadata ([#635](https://github.com/Altinn/altinn-authorization-utils/issues/635)) ([d88702e](https://github.com/Altinn/altinn-authorization-utils/commit/d88702e315cbac055fc7552287f117705c96c886))
* create CommandLine library ([#627](https://github.com/Altinn/altinn-authorization-utils/issues/627)) ([dff0d56](https://github.com/Altinn/altinn-authorization-utils/commit/dff0d56a7469f3f8c8225973a394e406956f4031))
* customize enum argument handling ([#664](https://github.com/Altinn/altinn-authorization-utils/issues/664)) ([467c90f](https://github.com/Altinn/altinn-authorization-utils/commit/467c90f2bd0ca749fee309c58ebe8f423d82e35e))
* execute-command result ([#662](https://github.com/Altinn/altinn-authorization-utils/issues/662)) ([06b62f0](https://github.com/Altinn/altinn-authorization-utils/commit/06b62f0249e6fc8abb5ce9a7508a02f480c16ce8))
* github actions utils ([#663](https://github.com/Altinn/altinn-authorization-utils/issues/663)) ([a732a70](https://github.com/Altinn/altinn-authorization-utils/commit/a732a706394cde3b8aca04495c0256b2eb9951e8))


### Bug Fixes

* ensure timeprovider is registered ([#670](https://github.com/Altinn/altinn-authorization-utils/issues/670)) ([8616fd9](https://github.com/Altinn/altinn-authorization-utils/commit/8616fd9d2413c33a5439e04ede1d5e5c0eb2299c))