Comments (2)
You can do this today without the need for an async version of Match
. Just return a Task<int>
from each Match
function argument instead of int
, like this:
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 Main
method, then you can also do this, where you unify ProgramArguments
and int
via object
:
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 object
, you can use a generic discriminated union from OneOf as shown next:
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 Match
and a switch
then you might as well go with just a switch
as shown in the documentation:
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.
from docopt.net.
I'm going to close this as unnecessary, but happy to reconsider if the presented solutions don't address issue.
from docopt.net.
Related Issues (20)
- Sync PR builds not occurring during CI on AppVeyor
- Argument that is both boolean and string (git exec-path option) HOT 1
- Assembly version is 0.0.0.0 HOT 1
- Source generator fails when arguments classes are named the same but belong to different namespaces
- Source generator fails with arguments classes in separate files
- Nested class using `[DocoptArguments]` fails to build
- Make entire code base null-safe
- Leading dot in generated hint name when project has no namespace
- Bad example or bug in new parser implementation? HOT 2
- Reinstalling docopt.net
- published 0.6.1.9 includes 0.6.1.4 dll HOT 3
- Feature Request: Description in summary comments HOT 2
- Docopt not parsing when running with 'dotnet run' HOT 3
- IsNullOrEmpty on a command throws System.NullReferenceException
- spaces in filenames HOT 3
- Upgrade to netstandard2.0 HOT 1
- run naval_fate HOT 2
- condition to properly build the project and have it work correctly HOT 8
- T4 template doesn't run in "T4DocoptNetHostApp" HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docopt.net.