Skip to content

[bug] Custom command format example wrong in documentation #4197

@akielbas

Description

@akielbas

Describe the bug

Following the example posted here: https://docs.conan.io/2/reference/extensions/custom_commands.html#decorators

Implementation of def output_json seems to be missing call to cli_out_write (does a return instead).

Suggested fix:

import json

from conan.api.conan_api import ConanAPI
from conan.api.output import ConanOutput, cli_out_write
from conan.cli.command import conan_command

def output_json(msg):
    myjson = json.dumps({"greet": msg})
    cli_out_write(myjson)


@conan_command(group="Custom commands", formatters={"json": output_json})
def hello(conan_api: ConanAPI, parser, *args):
    """
    Simple command to print "Hello World!" line
    """
    msg = "Hello World!"
    ConanOutput().info(msg)
    return msg

How to reproduce it

Implement a custom command from example extensions/commands/greet/cmd_hello.py with following contents:

import json

from conan.api.conan_api import ConanAPI
from conan.api.output import ConanOutput
from conan.cli.command import conan_command

def output_json(msg):
    return json.dumps({"greet": msg})


@conan_command(group="Custom commands", formatters={"json": output_json})
def hello(conan_api: ConanAPI, parser, *args):
    """
    Simple command to print "Hello World!" line
    """
    msg = "Hello World!"
    ConanOutput().info(msg)
    return msg

Then run conan greet:hello --format json. There is no JSON output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions