Fluent

The Fluent API is designed to simplify the process of chaining processors, connecting operators, and disposing of resources. It has built in support for shimming Line operators to work with Row data. It uses sensible defaults, and will auto convert between Line and Row where possible. As illustrated in Basics.Processors, manually coding these steps can be tedious.
Use the Pnyx class to access the Fluent API. It uses state-base logic for wiring together sources, filters, transforms, buffering, and destinations. You can learn more about the internal State Machine, but can be ignored for simple projects. At a high level, the state-machine permits method calls in the following sequence:
  • Input - 1 call for reading input
  • Line - any number of calls for operation on Line data, which is assumed input
  • Row/Object/NameValuePair - any number of calls for operating on Row/Object/NameValuePair data, but only after converting from Line
  • Output - 1 call for writing
There are a couple of important details. Line and Row operations can be mixed and matched, so long as a conversion is used, like parseCsv to convert to Row or print to convert to Line. For input and output, Pnyx expects only 1 call. However, it supports multiple inputs via the cat method, and multiple outputs via the tee method.

Builder Pattern

Most of the methods on Pnyx return the Pnyx object, which makes method chaining simple.

await using Pnyx p = new Pnyx();
p.readString("a,b,c,d")
 .parseCsv()
 .print("$4|$3|$2|$1")
 .writeStdout();

// outputs: d|c|b|a

Standard Input and Output

Pnyx has a setting for defaulting input and output to read from StandardIn and StandardOut. Use this setting when building a command line application with Pnyx for sensible defaults. The following two examples are equivalent. As you can see, when stdIoDefault is true, Pnyx automatically reads from StandardIn and writes to StandardOut.

Explicitly Read/Write from STDIO/STDOUT
// echos STDIN to STDOUT
await using Pnyx p = new Pnyx();
p.readStdin();
p.writeStdout();
Implicitly Read/Write from STDIO/STDOUT
// echos STDIN to STDOUT
await using Pnyx p = new Pnyx();
p.setSettings(stdIoDefault: true);

Command Line Arguments

Another useful feature of Pnyx is the ability to read command line arguments. This is done by calling the setCommandLineArgs, readArgs, writeArg methods. When command line arguments are provided, stdIoDefault setting is ignored in favor of defaulting input/output to the command line arguments. Pnyx has methods for explicitly read/writing for command line argument. By default, however, Pnyx reads from the first argument and writes to the second argument, which alleviates explicitly calling readArgs and writeArg for most usages. The following two examples are equivalent.

Explicitly Read First Argument and Write to Second Argument
// echoes input file (arg #1) to output file (arg #2)
await using Pnyx p = new Pnyx();
p.setCommandLineArgs(args);
p.readArg(1);
p.writeArg(2);
Implicitly Read First Argument and Write to Second Argument
// echoes input file (arg #1) to output file (arg #2)
await using Pnyx p = new Pnyx();
p.setCommandLineArgs(args);

Next

Suggested next step:
  • Reference, explore all methods of Pnyx
  • Line, examples of Line operations
  • Row, examples of Row operations
  • Object, examples of Object operations
  • NameValuePair, examples of Name-Value Pair operations