-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MatchAsync to IParser<T>.IResult
#187
Comments
You can do this today without the need for an async version of return await
ProgramArguments.CreateParser()
.WithVersion("Naval Fate 2.0")
.Parse(args)
.Match(Main,
result => { WriteLine(result.Help); return Task.FromResult(0); },
result => { WriteLine(result.Version); return Task.FromResult(0); },
result => { Error.WriteLine(result.Usage); return Task.FromResult(1); });
static async Task<int> Main(ProgramArguments args)
{
// ...
return 0;
} If you don't have a var result =
ProgramArguments.CreateParser()
.WithVersion("Naval Fate 2.0")
.Parse(args)
.Match(args => (object)args,
result => { WriteLine(result.Help); return 0; },
result => { WriteLine(result.Version); return 0; },
result => { Error.WriteLine(result.Usage); return 1; });
if (result is not ProgramArguments programArguments)
return (int)result;
// continues with async/await...
return 0; If you don't like the cast to var result =
ProgramArguments.CreateParser()
.WithVersion("Naval Fate 2.0")
.Parse(args)
.Match(OneOf<ProgramArguments, int>.FromT0,
result => { WriteLine(result.Help); return OneOf<ProgramArguments, int>.FromT1(0); },
result => { WriteLine(result.Version); return OneOf<ProgramArguments, int>.FromT1(0); },
result => { Error.WriteLine(result.Usage); return OneOf<ProgramArguments, int>.FromT1(1); });
if (result.TryPickT1(out var exitCode, out var programArguments))
return exitCode;
// continues with async/await...
return 0; And if you still don't want to rely on another library, you can introduce your own union: switch (
ProgramArguments.CreateParser()
.WithVersion("Naval Fate 2.0")
.Parse(args)
.Match(args => (ProgramAction)new RunAction(args),
result => { WriteLine(result.Help); return new ExitAction(0); },
result => { WriteLine(result.Version); return new ExitAction(0); },
result => { Error.WriteLine(result.Usage); return new ExitAction(1); }))
{
case ExitAction { ExitCode: var exitCode }:
return exitCode;
case RunAction { Arguments: var args }:
// continues with async/await...
return 0;
}
abstract record ProgramAction;
sealed record RunAction(ProgramArguments Arguments) : ProgramAction;
sealed record ExitAction(int ExitCode) : ProgramAction; But then if you're going with a var parser = Docopt.CreateParser(help).WithVersion("Naval Fate 2.0");
return parser.Parse(args) switch
{
IArgumentsResult<IDictionary<string, ArgValue>> { Arguments: var arguments } => Run(arguments),
IHelpResult => ShowHelp(help),
IVersionResult { Version: var version } => ShowVersion(version),
IInputErrorResult { Usage: var usage } => OnError(usage),
var result => throw new System.Runtime.CompilerServices.SwitchExpressionException(result)
}; Anyway, as you can see, you have plenty of choices. |
I'm going to close this as unnecessary, but happy to reconsider if the presented solutions don't address issue. |
It is common that the executing code is async, and it is not easily doable with the current API.
We need a
Task
/ValueTask
returningFunc
for the first argument, soasync
/await
can be used.This is my current workaround:
The text was updated successfully, but these errors were encountered: