Skip to content

Configuring AtleX.CommandLineParameters

Alex Kamsteeg edited this page Jun 30, 2021 · 3 revisions

Introduction

AtleX.CommandLineArguments features a rich and easy to use configuration system with which you can control its behaviour.

Creating a configuration

There are two ways of configuring Atlex.CommandLineArguments. The first is to create a strongly typed configuration by extending CommandLineArgumentsConfiguration. This is especially useful for organisations that wish to standardise their configuration, since they can wrap it up in a NuGet package and use that throughout the organisation.

public class MyConfiguration : CommandLineArgumentsConfiguration
  {
    public MyConfiguration()
    {
      this.HelpWriter = new WindowsStyleHelpWriter();
      this.Parser = new WindowsStyleCommandLineArgumentsParser();

      this.Validators.Add(new RequiredArgumentValidator());

      this.TypeParsers.Add(new BoolTypeParser());
      this.TypeParsers.Add(new StringTypeParser());
    }
  }

The second one is by just instantiating the CommandLineArgumentsConfiguration directly and configuring it.

var configuration = new CommandLineArgumentsConfiguration();

This creates a new configuration with the built-in validators and type parsers. You can add custom ones by calling .Add().

configuration.Add(new CustomValidator()); // Adds a custom IArgumentValidator implementation
configuration.Add(new CustomTypeParser()); // Adds a custom ITypeParser implementation

There are overrides of .Add() available that accept enumerables of IArgumentValidator and ITypeParser.

Using the configuration

Once the configuration is created you must assign is to CommandLineArguments.Configuration before calling CommandLineArguments.TryParse().

CommandLineArguments.Configuration = new MyConfiguration; // or assign the result of the ConfigurationBuilder
if (!CommandLineArguments.TryParse(args, out MyArguments arguments))
{
}
Clone this wiki locally