Overview

Usage

First, add AppArgs.cs to your project or reference the assembly you've built. Or better yet, install the Nuget package.

Add a using statement to your Program.cs file:

using MadProps.AppArgs;

Define a class to represent your application arguments as public properties:

public class Foo
{
    public bool IsAwesome { get; set; }
    public int HowAwesome { get; set; }
}

Now use the As<T> extension method to parse your program's arguments into an instance of Foo:

class Program
{
    static void Main(string[] args)
    {
        var foo = args.As<Foo>();
    }
}

You can now execute your program like so:

myapp.exe /isawesome /howawesome=100

... or using just enough characters to uniquely identify the arguments:

myapp.exe /i /h=100

Required Arguments

If an argument is required for your application, give it a Required attribute (found in the System.ComponentModel.DataAnnotations namespace, like so:

public class Foo
{
    [Required]
    public bool IsAwesome { get; set; }
    public int HowAwesome { get; set; }
}

If the user omits the "/isawesome" argument, an ArgumentException will be thrown from the As<T> method.

Alternate Names

If you want your HowAwesome property to be populated from an argument simply called "/level", you can do so using the ShortName property of the Display attribute (also found in the System.ComponentModel.DataAnnotations namespace), like so:

public class Foo
{
    [Required]
    public bool IsAwesome { get; set; }

    [Display(ShortName = "level")]
    public int HowAwesome { get; set; }
}

Argument Descriptions

If you want to be able to show a helpful description of each argument (see the next section, "Showing Help"), you can do so two ways:

  1. Add a Description attribute (from the System.ComponentModel namespace)
  2. Use the Description property of the aforementioned Display attribute

Here's an example using both techniques:

public class Foo
{
    [Required]
    [Description("Whether the app is awesome")]
    public bool IsAwesome { get; set; }

    [Display(ShortName = "level", Description="How awesome the app is")]
    public int HowAwesome { get; set; }
}

Showing Help

To generate a string that you can display to the user if they get an argument wrong, use the AppArgs.HelpFor<T> static method. I like to do something like this:

try
{
    var foo = args.As<Foo>();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    Console.WriteLine();
    Console.WriteLine(AppArgs.HelpFor<Foo>());
}

If the user omits the required "/isawesome" switch now, we'll see this output:

<pre> Argument missing: 'isawesome'

MyApp /isawesome [/level=value]

/isawesome Whether the app is awesome /level How awesome the app is </pre>

As you can see, required arguments are not enclosed in square brackets in the help text, and are listed before optional arguments.

Other Kinds of Arguments

You can also use MadProps.AppArgs to parse URIs or query strings. Consider:

// from a URI
var u = new Uri("http://example.com/path?isawesome=true&level=100");
var foo = u.As<Foo>();

// from a Query String
var qs = "isawesome=true&level=100";
var foo = qs.As<Foo>();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.