Skip to content

Parameter Types

contributors edited this page Oct 3, 2026 · 7 revisions

The library supports all* parameter types. The difference lies in how they are specified, and where.

This article introduces how to use various parameter types in commands.

* When specifying 'all', that is relative to the implementation of TypeParser types for non-standard implementations.

Base Parameters

By default, non-optional parameters are considered a required argument, without any special syntax. They are converted to, from a single input argument.

new Command((string arg1, int arg2, bool arg3) => { }, "name");
// 'command arg1 2 true' is valid
[Name("command")]
public void Command(string arg1, int arg2, bool arg3)
{
}

Important

The library will automatically convert the input arguments to the parameter types, if possible. If the conversion fails, the command will not be executed and returns a failed ParseResult, whose Exception is a CommandParsingException.

Nullable Parameters

Nullable parameters are supported, and are considered non-optional arguments that accept null as a value.

new Command((string arg1, int? arg2, bool arg3) => { }, "name");
// 'command arg1 null true' is valid
[Name("command")]
public void Command(string arg1, int? arg2, bool arg3)
{
}

Optional Parameters

Optional parameters are supported, and are considered optional arguments that can be omitted.

new Command((string arg1, int arg2 = 2, bool arg3 = true) => { }, "name");
// 'command arg1' is valid
[Name("command")]
public void Command(string arg1, int arg2 = 2, bool arg3 = true)
{
}

Deconstructible Parameters

Deconstructible parameters are unique in that they are redirected to the signature of a type constructor:

public class ConstructedType
{
    public string Arg1 { get; set; }
    public int Arg2 { get; set; }
    public bool Arg3 { get; set; }
    
    public ConstructedType(string arg1, int arg2, bool arg3)
    {
        Arg1 = arg1;
        Arg2 = arg2;
        Arg3 = arg3;
    }
}

These parameters are specified with the Deconstruct attribute.

new Command(([Deconstruct] ConstructedType complex) => { }, "name");
// 'command arg1 2 true' is valid
[Name("command")]
public void Command([Deconstruct] ConstructedType complex)
{
}

Deconstructible parameter constructors are an extension of the commands' own parsing logic. In this example, string, int, and bool are added as individual parameters to the command.

Note

When the target type does not have a public constructor containing any parameters, a ComponentFormatException will be thrown on creation. It is possible to define which constructor is used by specifying IgnoreAttribute on other constructors.

Important

When the application is trimmed or compiled with Native AOT, the constructor of a deconstructed type is removed unless it is used elsewhere, because the library can only reach it through reflection. Preserve it by adding DynamicDependencyAttribute to the command, otherwise creating the command throws a ComponentFormatException.

[Name("command")]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CustomType))]
public void Command([Deconstruct] CustomType arg)
{
}

Remainder

When a parameter is marked with RemainderAttribute, an attribute implementing IRemainderBinding or with params, it will consume all remaining arguments. This is useful for commands that accept a variable number of arguments.

Remainder parameters behave differently depending on the type of the parameter:

  • If the parameter is a string, object or T, it will consume all remaining arguments as a single string.
  • If the parameter is any implementation of Array, it will consume all remaining arguments as an array of T.
new Command((string arg1, [Remainder] string arg2) => { }, "name");
// 'command 1 2 3 4 5 6' is valid
[Name("command")]
public void Command(params int[] args)
{
}

Note

A IRemainderBinding attribute can only occur on the last parameter of the command signature. If it occurs on any other parameter, a ComponentFormatException will be thrown on creation.

Resources

In some cases it is functionally preferred to have a resource, not necessarily part of the command query, to be injected into the command signature as a parameter. When the IContext used for execution implements IResourceContext, the GetResource method will be used to retrieve a resource bound to the state of the command execution into the parameter marked with ResourceAttribute or any other attribute marked with IResourceBinding.

new Command(([Resource] T resource) => { }, "name");
// 'command' is valid when context is `IResourceContext` returning a resource of type `T`
[Name("command")]
public void Command([Resource] T resource)
{
}

T in this context is a parsible type. The value returned from GetResource will be parsed using the TypeParser registered for the type T.

Note

Injecting a resource into the command signature is exclusive, and can only be done once. If there are multiple occurrences of IResourceBinding on the command signature, a ComponentFormatException will be thrown on creation.

Naming Parameters

Parameters can be named by using the NameAttribute or another custom attribute implementing INameBinding. This is useful for commands that have camel-case parameter names, but should have lowercase naming to the caller.

new Command(([Name("arg1")] string Arg1, [Name("arg2")] int Arg2, [Name("arg3")] bool Arg3) => { }, "name");
// 'command arg1 2 arg3' is valid
[Name("command")]
public void Command([Name("arg1")] string Arg1, [Name("arg2")] int Arg2, [Name("arg3")] bool Arg3)
{   
}

Named Arguments and Flags

Every parameter can be provided by position, or by name. When an argument is named, it is matched to the parameter with that exact name, either the name from NameAttribute or the name in the signature. Matching is case-sensitive, so -a and -A are different arguments.

Input is read following POSIX utility conventions, extended with GNU-style long options:

Input Result
--name value name is "value"
--name=value name is "value"
--flag followed by another option, or at the end of the input flag is true
-a a is true. Short options are always flags, and never take a value
-abc a, b and c are each true
-- Ends option parsing, every following argument is positional
- and negative numbers like -5 or -.5 Positional arguments, never options
new Command(([Name("dev-cert")] bool devCert = false, int port = 8080) => { }, "serve");
// 'serve', 'serve --dev-cert', 'serve --port 5000 --dev-cert' and 'serve --dev-cert --port=5000' are valid
[Name("serve")]
public void Serve([Name("dev-cert")] bool devCert = false, int port = 8080)
{
}

Tip

Flags resolve to true when present, so a bool parameter that acts as a flag should be optional with a default of false.

Note

A long option takes the next argument as its value, so --verbose file.txt assigns "file.txt" to verbose. Place flags last, use --verbose=true, or end the options with --, such as --verbose -- file.txt.

Clone this wiki locally