Altinn.Authorization.CommandLine.Azure.KeyVault
1.3.0
dotnet add package Altinn.Authorization.CommandLine.Azure.KeyVault --version 1.3.0
NuGet\Install-Package Altinn.Authorization.CommandLine.Azure.KeyVault -Version 1.3.0
<PackageReference Include="Altinn.Authorization.CommandLine.Azure.KeyVault" Version="1.3.0" />
<PackageVersion Include="Altinn.Authorization.CommandLine.Azure.KeyVault" Version="1.3.0" />
<PackageReference Include="Altinn.Authorization.CommandLine.Azure.KeyVault" />
paket add Altinn.Authorization.CommandLine.Azure.KeyVault --version 1.3.0
#r "nuget: Altinn.Authorization.CommandLine.Azure.KeyVault, 1.3.0"
#:package Altinn.Authorization.CommandLine.Azure.KeyVault@1.3.0
#addin nuget:?package=Altinn.Authorization.CommandLine.Azure.KeyVault&version=1.3.0
#tool nuget:?package=Altinn.Authorization.CommandLine.Azure.KeyVault&version=1.3.0
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, andIServiceProviderare 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, orIFromServiceMetadatafor specialized binding.
Parameters with default values are treated as optional. Nullable parameters are also optional according to the parameter nullability metadata.
Use Microsoft.Extensions.Primitives.StringValues for whitespace-separated lists:
cli.AddCommand("deploy", "Deploy to environments", (
[Option("--environments")] Microsoft.Extensions.Primitives.StringValues environments) =>
{
foreach (var environment in environments)
{
Console.WriteLine(environment);
}
});
--environments "test prod" and --environments test --environments prod
both bind to two values. Each option occurrence consumes one token; an unquoted
prod in --environments test prod remains a positional argument. Splitting handles
spaces, tabs, and newlines, removes empty entries, and preserves order and
duplicates. The same parsing applies to [Argument] StringValues parameters.
Required lists reject empty or whitespace-only input; a parameter with
= default is optional and binds to an empty list when omitted.
Return Values
Handlers can return:
void,Task, orValueTask.T,Task<T>, orValueTask<T>when anICommandResultHandlerResolvercan resolve a handler forT.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");
});
Value parsing
Typed conversion uses ValueParser in Altinn.Authorization.CommandLine.Parsing.
Arguments and options share this service, and it can also parse raw values without
constructing a command. System.CommandLine handles tokenization, arity, and error
presentation; conversion failures prevent handler invocation.
Built-in conversion supports strings, StringValues, IParsable<T> types, plain
and annotated enums, nullable value types, arrays and common list interfaces, and
filesystem types. A parameter default does not disable explicit-value parsing.
Configure ParsingOptions.AdditionalParsers to customize conversion. Each
IValueParser exposes CanParse(Type) and a generic TryParse<T> method.
Additional parsers run in list order before built-in conversion. A parser that
claims a type reports invalid values through false and an error message;
failures do not fall through to another parser.
ValueParsingContext exposes input values and delegation through TryParse<T>.
Delegating the same type continues with the next parser. Delegating an inner type
starts from the first parser, so nullable values and collection elements use the
same configured conversions. ValueParser reads IOptionsMonitor<ParsingOptions>
at each entry point and keeps that options instance during delegated parsing.
Environment-variable defaults
Options can fall back to an environment variable without handler code:
void Run([Option("--registry", DefaultFromEnvironment = "MY_OCI_REGISTRY")] string? registry = null)
{
// registry is bound from the command line or the environment.
}
An explicit option wins over the environment variable; an unset, empty, or
whitespace-only variable falls back to the parameter default.
A required option without a parameter default requires either command-line input
or an environment value. Environment values use the option's parser, including
custom parsers such as StringValues whitespace splitting. Each variable supplies
one token; ordinary string values retain spaces. Parse errors prevent invocation.
Factories accept a nullable DefaultValueProvider<T> for options and arguments.
Use null when no default is configured, or
DefaultValueProvider.FromValue(value) for a constant (including explicit null).
Environment providers capture nonblank text during option resolution and defer
conversion until the default is requested. For programmatic construction, pass
the captured text and the parser resolved from DI:
var parser = services.GetRequiredService<ValueParser>();
var defaults = new EnvironmentValueDefaultValueProvider<string>(
"MY_OCI_REGISTRY", registryText, parser);
var option = optionFactory.Create<string>(
"--registry", [], "OCI registry", isRequired: true, defaults);
The non-generic DefaultValueProvider describes the provider's ValueType and
color-aware help text. Bind<T>() validates compatibility during command
construction and adapts compatible providers. Incompatible defaults fail before
parsing. Only DefaultValueProvider<T> supplies values, through TryProvideValue;
provision failures are command parse errors. An absent provider differs from a
provider supplying an explicit null.
Environment help always displays [default: $NAME] instead of the captured value,
even when the variable is unset. Parameter fallback values are not displayed in
help; they still apply when the variable is unset, empty, or whitespace-only.
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 | Versions 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. |
-
net10.0
- Altinn.Authorization.CommandLine (>= 1.3.0)
- Altinn.Authorization.CommandLine.Azure (>= 1.3.0)
- Azure.Identity (>= 1.21.0)
- Azure.Security.KeyVault.Secrets (>= 4.11.2)
- CommunityToolkit.Diagnostics (>= 8.4.2)
- Microsoft.Extensions.AsyncState (>= 10.11.0)
- Microsoft.Extensions.Hosting (>= 10.0.13)
- Microsoft.Extensions.Options (>= 10.0.13)
- Microsoft.Extensions.Primitives (>= 10.0.13)
- Nerdbank.Streams (>= 2.14.405)
- Spectre.Console (>= 0.57.2)
- System.CommandLine (>= 2.0.13)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
# Changelog
## [1.3.0](https://github.com/Altinn/altinn-authorization-utils/compare/pkg/CommandLine-v1.2.0...pkg/CommandLine-v1.3.0) (2026-10-23)
### Features
* add configurable value parsing pipeline ([#784](https://github.com/Altinn/altinn-authorization-utils/issues/784)) ([832cca1](https://github.com/Altinn/altinn-authorization-utils/commit/832cca1c7c8114cdd70887c3c31c75eefa9bfcd5))
* add typed default value providers ([#787](https://github.com/Altinn/altinn-authorization-utils/issues/787)) ([7539abc](https://github.com/Altinn/altinn-authorization-utils/commit/7539abcd22d4ea2c6a004c5d3cca95d23a394cc1))
* customize argument arity and contextual colors in help ([#771](https://github.com/Altinn/altinn-authorization-utils/issues/771)) ([0b61457](https://github.com/Altinn/altinn-authorization-utils/commit/0b61457a04076cfe84942f22a9de3ce914635796))
* parse whitespace separated values as StringValues ([#765](https://github.com/Altinn/altinn-authorization-utils/issues/765)) ([65e7180](https://github.com/Altinn/altinn-authorization-utils/commit/65e71805bba963869d5694846b9084732d363ac7))
* pass display context to default value help callbacks ([#780](https://github.com/Altinn/altinn-authorization-utils/issues/780)) ([7f76b07](https://github.com/Altinn/altinn-authorization-utils/commit/7f76b07509a0f7c0862080b4310cbb17bf30f6a3))
* support environment variable option defaults ([#788](https://github.com/Altinn/altinn-authorization-utils/issues/788)) ([372f161](https://github.com/Altinn/altinn-authorization-utils/commit/372f1615f4f82774130e909b3d15cf706159290a))
### Bug Fixes
* deserialize pull_request_target as a pull request event ([#793](https://github.com/Altinn/altinn-authorization-utils/issues/793)) ([1d07e19](https://github.com/Altinn/altinn-authorization-utils/commit/1d07e195fb8f243b5431b238216232adb328958c))
* reject explicit empty StringValues input ([#783](https://github.com/Altinn/altinn-authorization-utils/issues/783)) ([b5fcae2](https://github.com/Altinn/altinn-authorization-utils/commit/b5fcae2bb69e09c54456e483cc3c3a6fd8c07e34))
## [1.2.0](https://github.com/Altinn/altinn-authorization-utils/compare/pkg/CommandLine-v1.1.0...pkg/CommandLine-v1.2.0) (2026-10-13)
### Features
* print exported env vars ([#756](https://github.com/Altinn/altinn-authorization-utils/issues/756)) ([0908ef4](https://github.com/Altinn/altinn-authorization-utils/commit/0908ef4b2e4dc94146ea054677ed3eff5bf57e90))
## [1.1.0](https://github.com/Altinn/altinn-authorization-utils/compare/pkg/CommandLine-v1.0.0...pkg/CommandLine-v1.1.0) (2026-10-08)
### Features
* capture process line output ([#727](https://github.com/Altinn/altinn-authorization-utils/issues/727)) ([8dd068c](https://github.com/Altinn/altinn-authorization-utils/commit/8dd068c3b7540fe605f4ae4f6446a685b64aca68))
* expose github context ([#735](https://github.com/Altinn/altinn-authorization-utils/issues/735)) ([ca97935](https://github.com/Altinn/altinn-authorization-utils/commit/ca97935375eacd89e1ae5bf9d9e37d7bd5a4533a))
### Bug Fixes
* render plain json verbatim ([#728](https://github.com/Altinn/altinn-authorization-utils/issues/728)) ([9ca139a](https://github.com/Altinn/altinn-authorization-utils/commit/9ca139a765da7395279e33c08c24429f5eb5c75b))
## 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))