facebookarchive / python-nubia

A command-line and interactive shell framework.
Other
1.59k stars 96 forks source link

Make command available exlusively to interactive mode #71

Open byt3bl33d3r opened 4 years ago

byt3bl33d3r commented 4 years ago

Hello!

I was wondering if there was a way of specifying a command to only be available in interactive mode? It seems like I might be able to do this by pulling some hackery in the Context.on_cli() method but I was wondering if there's a better way?

Cheers

AndreasBackx commented 4 years ago

You should be able to do this when implementing the Command class itself, but it's in the internal API. What @command decorators do is they add information to the function which is picked up on startup and then converted into an AutoCommand (a subclass of Command). So if you would like to implement this, I would add parameters to the @command decorator that defines whether cli/interactive is allowed. Then these would be passed on AutoCommand and in the respective methods it can then be disabled. Maybe some other stuff needs to be done to see it's not picked up, but that's what I have in mind.

def command(
    name_or_function=None, help=None, aliases=None, exclusive_arguments=None,
+   cli=True, interactive=True,
):

Which would allow for disabling it for either like so:

@command(cli=False)
def my_command():
    pass
byt3bl33d3r commented 4 years ago

Thanks for that, is this something you think you'd be interested in merging if I submitted a PR? I feel like it would be useful for a lot of situations.

AhmedSoliman commented 4 years ago

Thanks @AndreasBackx for sharing the context. @byt3bl33d3r yeah, we don't mind having this, I think it's a great feature if implemented reliably.

AhmedSoliman commented 3 years ago

@byt3bl33d3r have got a chance to work on this?

ddutt commented 2 years ago

This would be very useful to have if you can in addition then support dynamic command completions for those commands that are interactive only. From what I remember, to help bash, the current model of completions only supports statically defined lists.

If this can be enabled as well, then I can look at adding support as well, and would appreciate info on how to add dynamic completion then.