|
| 1 | +--- |
| 2 | +title: How to customize parsing and validation in System.CommandLine |
| 3 | +description: "Learn how to customize parsing and validation with the System.Commandline library." |
| 4 | +ms.date: 06/16/2025 |
| 5 | +no-loc: [System.CommandLine] |
| 6 | +helpviewer_keywords: |
| 7 | + - "command line interface" |
| 8 | + - "command line" |
| 9 | + - "System.CommandLine" |
| 10 | +ms.topic: how-to |
| 11 | +--- |
| 12 | +# How to customize parsing and validation in System.CommandLine |
| 13 | + |
| 14 | +[!INCLUDE [scl-preview](../../../includes/scl-preview.md)] |
| 15 | + |
| 16 | +By default, System.CommandLine provides a set of built-in parsers that can parse many common types: |
| 17 | + |
| 18 | +* `bool` |
| 19 | +* `byte` and `sbyte` |
| 20 | +* `short` and `ushort` |
| 21 | +* `int` and `uint` |
| 22 | +* `long` and `ulong` |
| 23 | +* `float` and `double` |
| 24 | +* `decimal` |
| 25 | +* `DateTime` and `DateTimeOffset` |
| 26 | +* `DateOnly`and `TimeOnly` |
| 27 | +* `Guid`, |
| 28 | +* <xref:System.IO.FileSystemInfo>, <xref:System.IO.FileInfo>, and <xref:System.IO.DirectoryInfo> |
| 29 | +* enums |
| 30 | +* **arrays and lists** of the above types |
| 31 | + |
| 32 | +Other types are not supported, but you can create custom parsers for them. You can also validate the parsed values, which is useful when you want to ensure that the input meets certain criteria. |
| 33 | + |
| 34 | +## Validators |
| 35 | + |
| 36 | +Every option, argument, and command can have one or more validators. Validators are used to ensure that the parsed value meets certain criteria. For example, you can validate that a number is positive, or that a string is not empty. You can also create complex validators that check against multiple conditions. |
| 37 | + |
| 38 | +Every symbol type in System.CommandLine has a `Validators` property that contains a list of validators. The validators are executed after the input is parsed, and they can report an error if the validation fails. |
| 39 | + |
| 40 | +To provide custom validation code, call <xref:System.CommandLine.Option.Validators.Add%2A> on your option or argument (or command), as shown in the following example: |
| 41 | + |
| 42 | +:::code language="csharp" source="snippets/model-binding/csharp/AddValidator.cs" id="delayOption" ::: |
| 43 | + |
| 44 | +System.CommandLine provides a set of built-in validators that can be used to validate common types: |
| 45 | +- `AcceptExistingOnly` - configures given option or argument to accept only values corresponding to an existing file or directory. |
| 46 | +- `AcceptLegalFileNamesOnly` - configures given option or argument to accept only values representing legal file names. |
| 47 | +- `AcceptOnlyFromAmong` - configures given option or argument to accept only values from a specified set of values. |
| 48 | + |
| 49 | +## Custom parsers |
| 50 | + |
| 51 | +Custom parsers are required to parse types with no default parser, such as complex types. They can also be used to parse supported types in a different way than the built-in parsers. |
| 52 | + |
| 53 | +Suppose you have a `Person` type: |
| 54 | + |
| 55 | +:::code language="csharp" source="snippets/model-binding/csharp/ComplexType.cs" id="persontype" ::: |
| 56 | + |
| 57 | +You can just read the values and create an instance of `Person` in the command action: |
| 58 | + |
| 59 | +:::code language="csharp" source="snippets/model-binding/csharp/ComplexType.cs" id="setaction" ::: |
| 60 | + |
| 61 | +With the custom parser, you can get a custom type the same way you get primitive values: |
| 62 | + |
| 63 | +:::code language="csharp" source="snippets/model-binding/csharp/ParseArgument.cs" id="personoption" ::: |
| 64 | + |
| 65 | +If you want to parse as well as validate the input, use the `CustomParser` delegate, as shown in the following example: |
| 66 | + |
| 67 | +:::code language="csharp" source="snippets/model-binding/csharp/ParseArgument.cs" id="delayOption" ::: |
| 68 | + |
| 69 | +Here are some examples of what you can do with `CustomParser` that you can't do with a validator: |
| 70 | + |
| 71 | +* Parsing of other kinds of input strings (for example, parse "1,2,3" into `int[]`). |
| 72 | + |
| 73 | +* Dynamic arity. For example, you have two arguments that are defined as string arrays, and you have to handle a sequence of strings in the command line input. The <xref:System.CommandLine.Parsing.ArgumentResult.OnlyTake%2A?displayProperty=nameWithType> method enables you to dynamically divide up the input strings between the arguments. |
| 74 | + |
| 75 | +## See also |
| 76 | + |
| 77 | +[How to parse and invoke the result](parse-and-invoke.md) |
| 78 | +[System.CommandLine overview](index.md) |
0 commit comments