Click: Export JSON Data of Click Structure

Created on 23 Nov 2015  路  5Comments  路  Source: pallets/click

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.

Most helpful comment

@chrisngyn and I will be working on this!

All 5 comments

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.

@chrisngyn and I will be working on this!

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.

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()

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

inhumantsar picture inhumantsar  路  3Comments

arlyon picture arlyon  路  3Comments

kshitij10496 picture kshitij10496  路  4Comments

dhstack picture dhstack  路  4Comments

ShahriyarR picture ShahriyarR  路  3Comments