<jupyter_start><jupyter_text>Welcome Spokane .NET User Group!<jupyter_code>//Not explicitly needed 
#r "System.CommandLine"
//Other NuGet packages: #r "nuget:<package name>"

using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.IO;

public enum OutputType
{
    Unknown,
    Json,
    Csv,
    Yaml
}
var rootCommand = new RootCommand()
{
    new Option<FileInfo>("--input-file", "The file to convert"),
    new Option<DirectoryInfo>("--output-directory", "The output directory to covert files into."),
    new Option<OutputType>("--output-type", () => OutputType.Yaml, "The conversion type.")
};
rootCommand.Handler = CommandHandler.Create<FileInfo, DirectoryInfo, OutputType, IConsole>(Handler);

static void Handler(FileInfo inputFile, DirectoryInfo outputDirectory, OutputType outputType, IConsole console)
{
    console.Out.Write("Invoked Handler");
    console.Out.Write($"  Input File: {inputFile?.FullName}");
    console.Out.Write($"  Ouput Directory: {outputDirectory?.FullName}");
    console.Out.Write($"  Output Type: {outputType}");
}

rootCommand.Invoke("--help");
rootCommand.Invoke("--output-type Csv");
rootCommand.Invoke("--input-file ./file1 --output-directory ./output  --output-type Csv");
//Aliases
var rootCommand = new RootCommand()
{
    new Option<FileInfo>(new[] {"--input-file", "-i"}, "The file to convert"),
    new Option<DirectoryInfo>(new[] {"--output-directory", "-o"}, "The output directory to covert files into."),
    new Option<OutputType>(new[] {"--output-type", "-t"}, () => OutputType.Yaml, "The conversion type.")
};
rootCommand.Handler = CommandHandler.Create<FileInfo, DirectoryInfo, OutputType, IConsole>(Handler);

rootCommand.Invoke("--help");
//Model binding
public class ConversionOptions
{
    public FileInfo InputFile { get; set; }
    public DirectoryInfo OutputDirectory { get; set; }
    public OutputType OutputType { get; set; }
}

public record ConversionOptions (
    FileInfo InputFile,
    DirectoryInfo OutputDirectory,
    OutputType OutputType)
{ }

var rootCommand = new RootCommand()
{
    new Option<FileInfo>(new[] {"--input-file", "-i"}, "The file to convert"),
    new Option<DirectoryInfo>(new[] {"--output-directory", "-o"}, "The output directory to covert files into."),
    new Option<OutputType>(new[] {"--output-type", "-t"}, () => OutputType.Yaml, "The conversion type.")
};
rootCommand.Handler = CommandHandler.Create<ConversionOptions, IConsole>(Handler);

static void Handler(ConversionOptions options, IConsole console)
    => console.Out.Write($"Invoked Model Binding Handler: {options}");

rootCommand.Invoke("-i file -o output -t Csv");
// Parse delegates
var rootCommand = new RootCommand()
{
    new Option<int>(new[] {"--num-spaces"}, (ArgumentResult argumentResult) => {
        string token = argumentResult.Tokens.FirstOrDefault()?.Value;
        return token switch {
            "one" => 1,
            "two" => 2,
            "three" => 3,
            _ => 4
        };
    }),
};
rootCommand.Handler = CommandHandler.Create<int, IConsole>(Handler);

static void Handler(int numSpaces, IConsole console)
  => console.Out.Write($"Num Spaces: {numSpaces}");

rootCommand.Invoke("--num-spaces three");
// Custom suggestions
var rootCommand = new RootCommand()
{
    new Option<int>(new[] {"--num-spaces"}, (ArgumentResult argumentResult) => {
        string token = argumentResult.Tokens.FirstOrDefault()?.Value;
        return token switch {
            "one" => 1,
            "two" => 2,
            "three" => 3,
            _ => 4
        };
    })
    .AddSuggestions("three", "two", "one"),
};

rootCommand.Parse("--num-spaces t").GetSuggestions()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15354938271604937, 'fraction_numerical': 0.0023148148148148147, 'mean_word_length': 3.5326340326340326, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '62634', 'license': 'MIT', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1137, 'n_words': 282, 'path': '2021.04.13-System.CommandLine/SystemCommandLineSamples.ipynb', 'repo_name': 'IntelliTect-Samples/SpokaneNETUserGroupSamples', 'size': '3888'}