-
Notifications
You must be signed in to change notification settings - Fork 20
Use prog_name for usage #24
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
Conversation
|
@florimondmanca I've marked this as Draft still but see description above for approval regarding testcase removal. (Adding the comment here since I am not sure how GitHub does notifications for draft PRs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@max-frank 👍 Thanks! I think we're almost there — the diff looks good, we only need to update the failing test.
I think we can drop test_custom_multicommand_name(), and instead add a parametrization on the other test to test against both an explicitly-named multi-command, as well as one where no explicit name was passed:
@pytest.mark.parametrize("multi", [
pytest.param(MultiCLI("multi", help="Multi help"), id="explicit-name"),
pytest.param(MultiCLI(help="Multi help"), id="no-name"),
])
def test_custom_multicommand(multi):
...WDYT?
|
@max-frank To address your points…
So overall I'd say this is all good. I'm suggesting keeping the "explicit multicommand name" in mostly as a protection against regressions, but I'd agree it's not as important either now that we use |
With this do you mean the passing On another note I took another look at click and I am now fairly certain that sub commands will always have a name. (As you said the sort would have caused a bug by now otherwise). For entrypoint (top) level commands it seems click itself does not seem to care if they have a name or not. Default behavior seems to be to use the invoking scripts name. Meaning for entrypoint scripts it will be the scripts name. For module invocation e.g. something like this: if __name__ == "__main__":
from .cli import cli
cli()invoked with So with this in mind how do do you want to go about keeping "explicit multicommand name". Rather than adding logic that fails on missing top level name I'd suggest to use a meaningful default fallback instead. So something like this @@ -19,10 +19,14 @@ def replace_command_docs(**options: Any) -> Iterator[str]:
module = options["module"]
command = options["command"]
- prog_name = options.get("prog_name", command)
+ prog_name = options.get("prog_name", None)
depth = int(options.get("depth", 0))
command_obj = load_command(module, command)
+ name = command_obj.name
+
+ # alternatively instead of the import attr (command)
+ # we could also default to "root" similar to click.testing
+ prog_name = prog_name or name or command
return make_command_docs(prog_name=prog_name, command=command_obj, level=depth)With this change I would remove the failing |
Due to code changes it is now impossible for custom multi commands without names to trigger the tested exception.
If command.name is not set (should only happen if command object is initialized manually) the input command option will be used
|
I have now added the above suggested change in the |
florimondmanca
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@max-frank Taking this over for the next minor release, I updated it to take into account the latest additions. This is great work, thank you!
This PR ensures the
prog_name(introduced by #8 and initially requested in #6) is also used in the usage sections.This is achieved by properly setting the
info_namefor the click.Context used during documentation rendering. Setting theinfo_nameallows click to properly populate thectx.command_pathproperty and thus_docs.py line 77now actually combines the command path and the pieces correctly. In the previous versionctx.command_pathwas always empty. This makes the custom command path building logic obsolete.Now as a side effect this produces one failing test:
The test obviously fails as the exception is not raised anymore in the usage portion. Now I am unsure if this test can be removed or not. For entrypoint level
MultiCli()commands we get a guaranteed name populated either through theprog_nameoption directly or indirectly via thecommandoption. This is regardless whether the actualMultiCliwas instantiated with a name or not.Now while I strongly believe this makes it impossible for entrypoint level multi commands to cause any name issues I am not a 100% sure about multi commands which are sub commands. If such a sub command is possible then a check+raise would have to be added to the recursive call.
Here
command.nameis invoked. Now from what I can see and know about click it is not possible to add a multi command to a click group without assigning a name to it. e.g., add_command will raise aTypeErrorif both the function inputnameand inputcmd.nameareNone.Please verify the correctness of my "findings" and advise if the failing test can be removed and the PR is ok as is or if it is possible to get a sub command without name. Once you have verified this feel free to remove the test or apply changes yourself or I can also do so myself if you give notify me with a comment.