BrianPugh / cyclopts

Intuitive, easy CLIs based on python type hints.
Apache License 2.0
337 stars 8 forks source link

how to set the help message of app that register sub-app #207

Closed OrHayat closed 3 months ago

OrHayat commented 3 months ago

i think thats a bug/missing feature but i might tried to do something wrong

#!/bin/python3
from scripts.build import build_cli

import cyclopts
app= cyclopts.App(name="test1")
app.command(build_cli,name="application1")

def main():
    app()

if __name__ == "__main__":
    main()
 > ./cli.py  -h
Usage: test1 COMMAND

╭─ Commands ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ application1                                                                                                                                                                              │
│ --help,-h     Display this message and exit.                                                                                                                                              │
│ --version     Display application version.                                                                                                                                                │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

i wanted to add help message to application1

#!/bin/python3
from vlz_core_scripts.connector_builder.build_connector import connector_build_cli

import cyclopts
app= cyclopts.App(name="test1")
app.command(connector_build_cli,name="application1",help="app1 is used for doing some stuff")

def main():
    app()

if __name__ == "__main__":
    main()

but i get this error

./cli.py -h Traceback (most recent call last): File "/home/orhayat/volumez/vlz_core_scripts/./cli.py", line 6, in app.command(connector_build_cli,name="application1",help="app1 is used for doing stuff....") File "/home/orhayat/.local/lib/python3.10/site-packages/cyclopts/core.py", line 567, in command raise ValueError("Cannot supplied additional configuration when registering a sub-App.") ValueError: Cannot supplied additional configuration when registering a sub-App.

BrianPugh commented 3 months ago

you can supply the help when instantiating the subapp.

from cyclopts import App, Parameter

app = App(name="test1")
buildcli = App(name="application1", help="Help string for application1.")
app.command(buildcli)

def main():
    app()

if __name__ == "__main__":
    main()
$ python issue-207.py
Usage: test1 COMMAND

╭─ Commands ─────────────────────────────────────────────────────╮
│ application1  Help string for application1.                    │
│ --help,-h     Display this message and exit.                   │
│ --version     Display application version.                     │
╰────────────────────────────────────────────────────────────────╯

Alternatively, if no explicit help string is provided, the docstring from the registered default function for the subapp will be used:

from cyclopts import App, Parameter

app = App(name="test1")
buildcli = App(name="application1")
app.command(buildcli)

@buildcli.default
def buildcli_default_function():
    """Help string for application1.

    You can make the description longer in subsequent lines.
    """

def main():
    app()

if __name__ == "__main__":
    main()

This results in the same output:

$ python issue-207.py
Usage: test1 COMMAND

╭─ Commands ─────────────────────────────────────────────────────╮
│ application1  Help string for application1.                    │
│ --help,-h     Display this message and exit.                   │
│ --version     Display application version.                     │
╰────────────────────────────────────────────────────────────────╯

This is documented more in the help section of the docs. The way of specifying help for a subapp in your example is specifically disallowed because it leads to a particularly ambiguous situation.

# Not allowed!
app.command(subapp, help="My Help.")

If the check wasn't in place, this would overwrite the help string in subapp; in some situtations this is fine, in others it is not.