Skip to content

Piped arguments#

public class Program
{
    static int Main(string[] args) => AppRunner.Run(args);

    public static AppRunner AppRunner => new AppRunner<Program>();

    public void Range(IConsole console, int start, int count, int sleep = 0)
    {
        foreach (var i in Enumerable.Range(start, count))
        {
            console.WriteLine(i);
            if (sleep > 0)
            {
                Thread.Sleep(sleep);
            }
        }
    }

    public void Sum(IConsole console, IEnumerable<int> values)
    {
        int total = 0;
        foreach (var value in values)
        {
            console.WriteLine(total += value);
        }
    }
}
snippet source | anchor

Here we've converted the arguments for Sum into an IEnumerable and added a Range command. You've probably noticed these commands wrap LINQ methods of the same name. We've added an optional sleep option to Range to better mimic a long running stream.

We could have used List, int[], or any other collection type. Using IEnumerable allows the command to start processing before the stream has completed.

Very few console frameworks make it this easy to write streaming console tools.

Let's see it in action:

~
$ dotnet linq.dll Range 1 4 10000 | dotnet linq.dll Sum
1
3
6
10

After outputtting a value, Range sleeps for 10 seconds. We know Sum is streaming because it immediatly outputs the new sum as soon as it receives a value and waits for the next value.