Skip to content
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

Export JSON Data of Click Structure #461

Closed
mitsuhiko opened this issue Nov 23, 2015 · 5 comments · Fixed by #1623
Closed

Export JSON Data of Click Structure #461

mitsuhiko opened this issue Nov 23, 2015 · 5 comments · Fixed by #1623
Assignees
Milestone

Comments

@mitsuhiko
Copy link
Contributor

For external tools it would be nice if one could export the entire data of a click command into a JSON format. For instance that way one could generate externally hosted documentation pages for commands that use better formatting than what --help generates.

@mitsuhiko mitsuhiko added this to the Click 6.0 milestone Nov 23, 2015
@davidism davidism removed this from the Click 6.0 milestone Feb 5, 2018
@davidism
Copy link
Member

davidism commented Jul 2, 2020

If we were to do this in Click core, as opposed to a separate project, I'd imagine we'd add a to_info_dict() method to BaseCommand, Parameter, and ParamType, overriding as necessary. Is there something specific that's not easy to introspect currently, though? And how detailed should the dict be? Should it include every parameter? Computed attributes like auto_envvar_prefix?

A separate project could take advantage of other libraries like Marshmallow to give the serialized data more structure as well. I guess I'm not opposed to it, and implementing it here in some form doesn't prevent another project from extending it or offering a different method.

@YKo20010
Copy link
Contributor

YKo20010 commented Jul 2, 2020

@chrisngyn and I will be working on this!

@davidism
Copy link
Member

davidism commented Jul 2, 2020

For example, group.to_info_dict() would put the group name, help, etc. in, then call command.to_info_dict() for each command returned by list_commands(), and put that list under a "commands" key. So any given object would build itself plus anything under it, and a subclass could override what happens.

I think there are some other open issues about being able to traverse the structure without triggering prompts and validation, since that functionality exists but isn't perfect. As a first pass, don't worry about these, note them down and they can be addressed separately.

@davidism
Copy link
Member

#1623 implements to_info_dict() methods on the core objects. This outputs information that could be useful for user-facing docs. Use the following to generate an entire CLI structure:

with click.Context(cli) as ctx:
    info = ctx.to_info_dict()

@nicholascapo
Copy link

Very excited for this feature, do we know when the next release will be? The last one (v7.1.2) was in April.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants