Defining the application arguments

Previous: Defining the application options | Table of contents | Next: Auto-generated usage help

Now that we have defined our options, the next step is to define the arguments for the application.

The Backup application accepts two arguments:
  1. The directory to be backed up. The directory specified must exist.
  2. The file to backup to. This file does not need to exist, but it should be a .ZIP file.

Unlike options, arguments do not have names, and so are identified in the order in which they are defined.

To declare a new argument, we use the AddArgument method of the ConsoleProgram class.

app.AddArgument();

Arguments have similar methods to options for specifying validators and handling values.
Let's add a path validator to the first argument, and assign its value to a static string field called BackupDirectory.

private static string BackupDirectory;

app.AddArgument()
    .ValidateWith(new PathValidator(PathType.Directory))
    .AssignTo(() => BackupDirectory);

Similarly, we add another argument for the backup file path. This also uses a path validator, but does not need to check whether the file already exists.

private static string BackupFilePath;

app.AddArgument()
    .ValidateWith(new PathValidator(checkIfExists: false))
    .AssignTo(() => BackupFilePath);

Performing a custom validation

Another rule for the backup file path argument is that it should be a .ZIP file, i.e. its extension must be .ZIP. Since ConsoleFx doesn't have a built-in validator to check file extensions, we have to use the CustomValidator class to perform a custom validation.

The CustomValidator constructor accepts a Func<string, bool> delegate parameter, which contains the custom validation logic. The argument string value is passed to this delegate and it should return whether the validation succeeded or not.

In this case, we want to extract the extension from the given backup file path and check that it is .ZIP.

app.AddArgument()
    .ValidateWith(new PathValidator(checkIfExists: false))
    .ValidateWith(new CustomValidator(arg => Path.GetExtension(arg).Equals(".zip", StringComparison.OrdinalIgnoreCase)))
    .AssignTo(() => BackupFilePath);

Full code at this time

using System;
using System.Collections.Generic;
using System.IO;

using ConsoleFx;
using ConsoleFx.Programs.Simple;
using ConsoleFx.Validators;

namespace Backup
{
    internal static class Program
    {
        private static int Main()
        {
            var app = new ConsoleProgram(Handler);
            app.AddOption("verbose", "v")
                .Flag(() => VerboseOutput);
            app.AddOption("type", "t", expectedParameters: 1)
                .ValidateWith(EnumValidator.Create<BackupType>(errorMessage: "Invalid backup type. Specify Full or Incremental"))
                .AssignTo(() => BackupType);
            app.AddOption("exclude", "e", maxOccurences: int.MaxValue, minParameters: 1, maxParameters: int.MaxValue)
                .ValidateWith(ParameterIndex.All, new RegexValidator(@"^[\w.*?]+$"))
                .AddToList(() => Exclusions);
            app.AddArgument()
                .ValidateWith(new PathValidator(PathType.Directory))
                .AssignTo(() => BackupDirectory);
            app.AddArgument()
                .ValidateWith(new PathValidator(checkIfExists: false))
                .ValidateWith(new CustomValidator(arg => Path.GetExtension(arg).Equals(".zip", StringComparison.OrdinalIgnoreCase)))
                .AssignTo(() => BackupFilePath);
            return app.Run();
        }

        private static int Handler()
        {
            return 0;
        }

        private static bool VerboseOutput;
        private static BackupType BackupType;
        private static readonly List<string> Exclusions = new List<string>();
        private static string BackupDirectory;
        private static string BackupFilePath;
    }

    public enum BackupType
    {
        Full,
        Incremental,
    }
}

Previous: Defining the application options | Table of contents | Next: Auto-generated usage help

Last edited Nov 23, 2011 at 8:40 PM by jeevanjj, version 2

Comments

No comments yet.