jawher / mow.cli

A versatile library for building CLI applications in Go
MIT License
872 stars 55 forks source link

Expose a model of the command structure #104

Open skyzyx opened 4 years ago

skyzyx commented 4 years ago

It's easy to build out a command structure to get a CLI app up and going fairly quickly (thank you!).

That said, once I've modeled the command structure in code, I'd like to be able to access a representation of what's been set up. Something that exposes the command structure (possibly as a set of nested structs?).

My end goal is to be able to take this exposed model and do things like generate Bash/Zsh/Fish/Powershell auto-completion scripts. By opening this up, it would make it easier for other packages to integrate features of this type.

I haven't yet dug into the code enough to fully understand the internals, but it looks like it would start with exposing the model for Cmd.commands[]. https://github.com/jawher/mow.cli/blob/fbc50e90e1fb96a3ddde67f9ca6289b8054f2f7e/commands.go#L127-L141

skyzyx commented 4 years ago

Ping?

jawher commented 4 years ago

Hi 👋

Sorry for the delay.

I'm playing with different options to expose the CLI's structure (commands, options, arguments) without trying it to the internal data structures.

I'll hopefull have a PoC soon in a separate branch if you are willing to test it before merging it.

The idea would be to have a new package, model for example which contains several structs (Command, Option, Argument) representing a CLI, and have a method to return this representation from the App struct.

skyzyx commented 4 years ago

This would be great.

My end goal is to be able to take this exposed model and do things like generate Bash/Zsh/Fish/Powershell auto-completion scripts. By opening this up, it would make it easier for other packages to integrate features of this type.

In the interim, I've ended up having to duplicate the command structure I'm using for this CLI framework, then hard-code what the responses should be. I think that by exposing the structure of the model, it will become dramatically simpler (and more DRY) to respond to <command><TAB> with the right subcommands and flags.

Thank you!

jawher commented 4 years ago

@skyzyx Thank you for your patience 🤗

I just pushed a branch with a PoC of an app model: https://github.com/jawher/mow.cli/pull/106

I would be grateful if you could play with it and share your feedback.

skyzyx commented 4 years ago

OK. I have some time to play with this today. Any idea how to pull this branch with go.mod?

skyzyx commented 3 years ago

I have tested this, and it works marvelously. Thank you.

Getting a new tag with this functionality would be appreciated.