Skip to content

Commit

Permalink
docs: add OaktonAsynCommand docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pondidum authored and jeremydmiller committed Mar 8, 2018
1 parent 56de3bb commit 81f6cb9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion documentation/documentation/commands.md
Expand Up @@ -7,7 +7,7 @@ It is perfectly legal to use the same input class across multiple commands
Oakton commands consist of two parts:

1. A concrete input class that holds all the argument and flag data inputs
1. A concrete class that inherits from `OaktonCommand<T>` where the "T" is the input class in the first bullet point
1. A concrete class that inherits from `OaktonCommand<T>` or `OaktonAsyncCommand<T>` where the "T" is the input class in the first bullet point

Looking again at the `NameCommand` from the <[linkto:documentation/getting_started]> topic:

Expand All @@ -22,6 +22,10 @@ There's only a couple things to note about a command class:
* The `Usages` syntax in the constructor is explained in a section below
* The `[Description]` attribute on the class is strictly for the purpose of providing help text and is not mandatory

If you want to make use of `async/await`, you can inherit from `OaktonAsyncCommand<T>` instead. The only difference is signature of the `Execute()` method:

<[sample:async-command]>


## Argument Usages

Expand Down
6 changes: 4 additions & 2 deletions src/MultipleCommands/Program.cs
@@ -1,6 +1,7 @@
using System;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Threading.Tasks;
using Oakton;

namespace MultipleCommands
Expand All @@ -24,10 +25,11 @@ static int Main(string[] args)

// SAMPLE: git-commands
[Description("Switch branches or restore working tree files")]
public class CheckoutCommand : OaktonCommand<CheckoutInput>
public class CheckoutCommand : OaktonAsyncCommand<CheckoutInput>
{
public override bool Execute(CheckoutInput input)
public override async Task<bool> Execute(CheckoutInput input)
{
await Task.CompletedTask;
return true;
}
}
Expand Down
27 changes: 21 additions & 6 deletions src/quickstart/Program.cs
@@ -1,5 +1,6 @@
using System;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Oakton;

namespace quickstart
Expand All @@ -20,15 +21,15 @@ public class NameInput
{
[Description("The name to be printed to the console output")]
public string Name { get; set; }

[Description("The color of the text. Default is black")]
public ConsoleColor Color { get; set; } = ConsoleColor.Black;

[Description("Optional title preceeding the name")]
public string TitleFlag { get; set; }
}
// ENDSAMPLE

// SAMPLE: NameCommand
[Description("Print somebody's name")]
public class NameCommand : OaktonCommand<NameInput>
Expand All @@ -48,7 +49,7 @@ public override bool Execute(NameInput input)
{
text = input.TitleFlag + " " + text;
}

// This is a little helper in Oakton for getting
// cute with colors in the console output
ConsoleWriter.Write(input.Color, text);
Expand All @@ -73,7 +74,7 @@ public OtherNameCommand()
// and in what order they should be expressed
// by the user
.Arguments(x => x.Name, x => x.Color)

// Optionally, you can provide a white list of valid
// flags in this usage
.ValidFlags(x => x.TitleFlag);
Expand All @@ -85,7 +86,7 @@ public override bool Execute(NameInput input)
return true;
}
}

// SAMPLE: command-alias
[Description("Say my name differently", Name = "different-name")]
public class AliasedCommand : OaktonCommand<NameInput>
Expand All @@ -96,4 +97,18 @@ public override bool Execute(NameInput input)
throw new NotImplementedException();
}
}

// SAMPLE: async-command
public class DoNameThingsCommand : OaktonAsyncCommand<NameInput>
{
public override async Task<bool> Execute(NameInput input)
{
ConsoleWriter.Write(input.Color, "Starting...");
await Task.Delay(TimeSpan.FromSeconds(3));

ConsoleWriter.Write(input.Color, $"Done! Hello {input.Name}");
return true;
}
}
// ENDSAMPLE
}

0 comments on commit 81f6cb9

Please sign in to comment.