click-contrib / click-repl

Subcommand REPL for click apps
MIT License
215 stars 41 forks source link

AttributeError: 'Command' object has no attribute 'commands' #40

Closed zerans closed 6 years ago

zerans commented 6 years ago

In a function called from a setup tools entry point:

@click.command()
def main():
    repl(click.get_current_context(), prompt_kwargs={'history': FileHistory(HISTFILE)})

I get this error:

  File "/Users/lol/Library/Python/2.7/lib/python/site-packages/click_repl/__init__.py", line 169, in repl
    available_commands = group_ctx.command.commands
AttributeError: 'Command' object has no attribute 'commands'

If I do this:

@click.command()
@click.pass_context
def main(ctx):
    ctx.command.commands = []
    repl(ctx, prompt_kwargs={'history': FileHistory(HISTFILE)})

I get a different error, because pop is called incorrectly.

Commenting out lines 171-180 fixes the error. available_commands isn't used anywhere else, so this seems to be dead code?

    if isinstance(group_ctx.command, click.CommandCollection):
        available_commands = {
            cmd_name: cmd_obj
            for source in group_ctx.command.sources
            for cmd_name, cmd_obj in source.commands.items()
        }
    else:
        available_commands = group_ctx.command.commands
    available_commands.pop(repl_command_name, None)
untitaker commented 6 years ago

click-repl only works if you have a command Group: What is the point of using it if there are no commands to execute on the shell?

zerans commented 6 years ago

Same as the other guy's issue. I don't want $ thing repl, I want $ thing to always drop to the REPL immediately. I think maybe click-repl is not the right library for me.

On May 9, 2018, at 11:03 AM, Markus Unterwaditzer notifications@github.com wrote:

click-repl only works if you have a command Group: What is the point of using it if there are no commands to execute on the shell?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/click-contrib/click-repl/issues/40#issuecomment-387825207, or mute the thread https://github.com/notifications/unsubscribe-auth/AZ7bltOJPYn9sh_GdOT5oXdzpMVLoMz9ks5twy-KgaJpZM4T3rtx.

untitaker commented 6 years ago

That is not what I am talking about. I mean that if you have only one command, what would the repl allow you to do?

For my usecase I have a command like this:

my_program # launches repl
my_program foo # a subcommand

You on the other hand seem to have no subcommands, because you have no group. What exactly would the user type in the REPL?

zerans commented 6 years ago

There were other commands not included in the example, sorry. I think I'm misunderstanding how to achieve the goal of having the program drop straight into the REPL without requiring a repl arg. A group with invoke_without_command?

untitaker commented 6 years ago

yes, invoke_without_command, and in the group body:

if not click_ctx.invoked_subcommand: repl(...)

zerans commented 6 years ago

Thank you :)

On May 9, 2018, at 11:39 AM, Markus Unterwaditzer notifications@github.com wrote:

yes, invoke_without_command, and in the group body:

if not click_ctx.invoked_subcommand: repl(...)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/click-contrib/click-repl/issues/40#issuecomment-387835880, or mute the thread https://github.com/notifications/unsubscribe-auth/AZ7blrb9dwBURLkdGQG-bHRlkTdTsylgks5twzfxgaJpZM4T3rtx.

untitaker commented 6 years ago

yw