-
Notifications
You must be signed in to change notification settings - Fork 480
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
Added support for async programming #390
Conversation
TBH I feel like The issue is, at a certain point, you have a sequence of operations that need to happen in order that can't be effectively further destructed into parts. This opens up the possibility for the underlying library to start doing things async, however, here's my biggest opposition to this... The commandlineparser library does not use external I/O. We don't even hit the filesystem for testing if an argument represents an existing file. One could possibly argue that the parsing routines could be done in parallel, asynchronously, and map the results back into a single result, which would be the Options class populated with the parse results from the command line. That all being said, it looks like a good thing to add, although right now the usage of it is kinda dubious... ;-) |
It is indeed a virus that spreads until the moment when you no longer have to call any asynchronous method. I'm already using it with the modification I've made in a process that runs on Linux micro PC that basically creates a communication server and communicates by serial port with different devices. I think there will be more contexts in which it is really necessary, and more and more. |
Does this process work with the multiple verbs and MapResults extension method? (Funny thing, I actually awoke myself thinking about the MapResults method, of how to do this better) So be aware, I'm trying to figure out a better way to invoke the parser and get results or parsing errors in order to report back to the user. I'm kinda apprehensive on this, but probably unfounded. |
I hit the same issue as OP. |
Have a look to this discussion in how to use async/await in MapResult without the need to change the Codebase of the library.
In C# 7.1 , you can use it in Task Main(). |
I understand this reply is over two years old at this point, but Implementing async/await into the mappings is practically a necessity for this library to stay relevant with modern C#. At the time, it may have been somewhat foreign to .NET, reserved for only niche circumstances, or just considered some syntactic sugar, but is now extremely widely used, and one of the languages most powerful features. For any application that deals with web-based technologies, it is pretty much a requirement, not an option. Having an application that pulls data from the web or database, such as a server, web scraper, etc, etc, having the the program being unresponsive during communication is simply not a possibility. There are indeed some workarounds, but they are a bit ugly, and add extra complexity. I would really love to see this implemented as a core feature in a future release, and thank all the contributors for the fine work they have done. |
I stumbled across this issue today. It might be that the library itself does not do I/O. However, the user code processing the options might do I/O and require async. I just had a case like that. @joseangelmt Thank for the PR. I will try to work around the problem in my code and continue to use async code at the same time. I hope that the PR will be accepted at some point. |
I'm brand new to using this lib, but just wondering if there are any drawbacks to using @moh-hassan 's solution? I just implemented it here and it seems to be working well. |
@joseangelmt
What is the official way for mix and match async/non async parameters? |
@moh-hassan since the passed functions all must return TResult it's implicit that they must all return tasks in an async overload. If the consumer wants to mix and match it's trivial to wrap synchronous methods in a task with Task.FromResult, which is no different to what they'd have to do now when passing a mixture of synchronous and asynchronous methods to the existing MapResult. That said I'm not sure that |
Yes, it can. |
@moh-hassan I agree - in the case of The same can't be said for e.g.
|
b211712
to
746885a
Compare
@joseangelmt , |
Today you'll have it. |
Thanks @joseangelmt for the modified version of PR. |
Quick question: how can I try this very recently merged WithParsedAsync/WithNotParsedAsync stuff? Can is simply install a newer version using dotnet add (I'm a bit now to dotnet :-) but having much fun in the process). |
You can use the nightly develop nuget package CommandLineParser.2.8.0-beta-125.nupkg on CI Appveyor server. How To use: var result = await Parser.Default.ParseArguments<Options>(args)
.WithParsedAsync(RunAsync);
result.WithNotParsed(HandleErrors); //Or WithNotParsedAsync
async Task RunAsync(Options options)
{
// await ....
}
Task HandleErrorsAsync(IEnumerable<Error> arg)
{
//handle errors
} |
@ericnewton76 |
Can the betas/previews be published to nuget? |
I'm waiting for the Nuget key update by the site Admin to publish the package. cc / @ericnewton76 |
@moh-hassan can you send me an email to the address on my profile? |
With C# 7.1 you can create a pure async console application with an
async Task Main()
orasync Task<int> Main()
entry point.The library is not easy to use in this scenario, so I added extension methods that complements all the the existing overloads of
CommandLine.ParserResultExtensions.WithParsed
andCommandLine.ParserResultExtensions.MapResult
extension methods.With this new async extention methods you can create async versions of sampleapps like this..
or like this