Closed clarkperkins closed 4 years ago
i'm currently using this hack for dynamic prompts:
shell = make_click_shell(...)
# save shell object before shell.cmdloop()
click.get_current_context().meta['myshellobj'] = shell
shell.cmdloop()
# update prompt later
click.get_current_context().meta['myshellobj'].prompt = newprompt
works in my use case, which doesn't change the prompt all that often.
I managed to make this work in a very simple manner by changing the get_prompt
method in ClickCmd
:
def get_prompt(self):
try:
return self.ctx.obj['prompt']
except KeyError:
return self.prompt
In this solution, the user can set a value to the prompt by settingctx.obj['prompt']
which will take precedence over self.prompt
.
Does this seem like a good option?
I've decided to implement this by allowing the prompt argument to be either a string or a callable. If it's a callable, it will be called each time a prompt is printed, and the return value will be the prompt. I think that should be flexible enough to fix pretty much anything!
It's implemented in #18.
Awesome! Thanks!
We want to be able for the cmd prompt line to be able to dynamically change during runtime.