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
"Lazy" loading of commands? #320
Comments
Currently, there is no support for lazy-loading subcommands. Why do you ask? Have you noticed performance issues with large apps? |
Hi Nate. I finally got around to organizing my response, I wanted to provide an example of what I'm trying to achieve before going into more detail. Here's a single file ConsoleApp that I've been using as a playground for learning about your library (see earlier revisions), it is a WIP, but I think it demonstrates what I'm getting at. Bottom line, I am not seeing a negative performance impact yet, and I do would not expect to see one for "simple" subcommands (but I haven't tested that). Instead my concern is about how/when more complicated subcommands of type In my case, I'm looking to build a CLI that interacts with my team's systems, and therefore would depend on packages, clients, and service classes that we have already written. Not all of these dependencies are lightweight :), so as this CLI tool grows in scope, it would be nice to defer instantiating these dependencies until they are needed. Of course, it might be possible to defer resolving/constructing these dependencies ourselves until a particular subcommand's CommandLineApplication is invoked. However, this seems redundant when I've attempted to do this in the CustomSubcommand.cs gist, with the For background: This is how I understood the My reasoning is that by using It seems then the preferred (ideal?) behaviour is and only construct the chain of Example: Take a CLI too called
The idea is, at runtime the library would never need to construct a Possible ways forward: I'm relatively new with this framework, but my first thought is some variant of I'm happy to discuss this further, take feedback, and help with implementing a solution to this if it's something you/we think is worthwhile. |
Thanks for the details, @wtyneb. I got a little lost reading the sample code, but the points you were making about deferring
This can trigger initializations during a .Dispose() call, which is just wrong.The other possible mistake is here:
I've corrected both of these in #332. Can you take a look at let me know if this is addressing the main problem you've described? |
Those changes look good, it definitely makes sense to avoid activating It seems like I'm trying to get you library to do something that it wasn't designed to do. While My ideal would to have the root Basically, I want the CLI structure to be more dynamic and driven by the subcommands, instead of having the root application contain everything. Earlier on I did attempt to initialize child I want to continue using this framework, because I really like the way the framework handles option/argument parsing and allowing nesting of subcommands via a relation of objects. Maybe this discussion needs its own thread, since I think this is about more than just "lazy" loading of subcommands. |
Correct. Using attributes to defining a command line API means it is completely declarative. The builder API is a little more flexible, but it still does not allow you to change the definition of the command line app while parsing is happening. Splitting subcommands into separate classes which are decoupled from the root application should be possible. The subcommands samples doesn't do a good job off this. Maybe I should right more samples. In the meantime, here are two real-world apps with a structure that might be sort of what you're looking for.
|
By the way, I'll leave this open until I release the 2.5.1 patch, but if you want to start a new thread for more discussion about structuring apps with many subcommands, feel free to do so 😄 |
2.5.1 is released. |
Is there a recommended way of loading things like sub-commands lazily?
My thinking is that if I'm frequently invoking a CLI tool with many commands, each with many subcommands, it would be preferable to only "load" the sub-commands of the parent command?
I suppose it could be done with some custom parsing or otherwise not linking the commands and sub-commands together with
app.Command<T>(...)
, but I think that kind-of defeats the purpose of the command/sub-command structure.Is there another way that I'm not seeing?
I've spent a bit of time with this library, and so far I really like the builder syntax and the lack of excessive "magic"! Makes it easier to approach. Thank you!
The text was updated successfully, but these errors were encountered: