Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Last active May 30, 2025 19:08
Show Gist options
  • Save atifaziz/6db7e9c00f83a2783d28437fe7c991d1 to your computer and use it in GitHub Desktop.
Save atifaziz/6db7e9c00f83a2783d28437fe7c991d1 to your computer and use it in GitHub Desktop.
<Query Kind="Statements">
<NuGetReference Version="0.8.1">docopt.net</NuGetReference>
<Namespace>DocoptNet</Namespace>
</Query>
const string docopt = """
Usage:
test.exe (-h | --help)
test.exe cmd [-abc] INPUT
""";
var parser =
Docopt.CreateParser(docopt)
.WithVersion($"App v{Assembly.GetExecutingAssembly().GetName().Version}");
var results =
from args in new string[][]
{
[], // Error
["-h"], // Help
["--help"], // Help
[""], // Error
["foo"], // Error
["cmd"], // Error
["cmd", "foo"], // OK
["cmd", "-ac", "foo"], // OK
["cmd", "-a", "-c", "foo"], // OK
}
let result =
parser.Parse(args)
.Match(static args => (Outcome: "Args" , Arg: (object)args),
static result => (Outcome: "Help" , Arg: result),
static result => (Outcome: "Version", Arg: result),
static result => (Outcome: "Error" , Arg: result))
select new
{
Args = args,
result.Outcome,
Result = result.Arg,
};
results. Dump();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment