Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upSupport for partial commands #992
Comments
|
I've made the following hook that does that simple implementation. # hook version 2
def hook_autocomplete(self, params: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
func = self.cmd_func(params.statement.command)
if not func:
basecmd = self.complete(params.statement.command, 0)
nextcmd = self.complete(params.statement.command, 1)
if(basecmd is not None and nextcmd is None):
basecmd = basecmd[:-1]
params.statement = Statement(params.statement.args,
raw=params.statement.raw,
command=basecmd,
arg_list=params.statement.arg_list,
multiline_command=params.statement.multiline_command,
terminator=params.statement.terminator,
suffix=params.statement.suffix,
pipe_to=params.statement.pipe_to,
output=params.statement.output,
output_to=params.statement.output_to)
return paramsSeems to works great but It should also support concatenations of partial commands. |
|
I've never actually used it but I know we have this plugin for abbreviated command support. It seems very similar to what you're describing. |
|
@ralequi Calling Since If you have a known set of aliases you'd like to use for subcommands, you can take advantage of From argparse docs
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
checkout = subparsers.add_parser('checkout', aliases=['co'])
checkout.add_argument('foo')
parser.parse_args(['co', 'bar'])
|
|
Thanks for your replies guys, @anselor that plugin looks great but it seems to do something like I did in that hook. It should work great but unless I'm missing something it would have the same limitation that my hook has, as far as it should not be able to autocomplete sub-commands. Thanks @kmvanbrunt , I supposed a simple hook won't be able to do what I wish. I have to finish some basic functionality on my APP, but I consider this feature very powerful (and mostly required to my clients, as they come from managing cisco/juniper like devices) so I'll look forward to it when I finish that basic func. |
Some shells accept and interpret partial commands and it would be a great feature to be included.
Let's take a look an example:
By default cmd2 includes
quitcommand, which is used to end the cmd-session.In many scenarios, it would be useful to allow the user just write
qto get the same functionality.Of course cmd2 support many ways of doing this, such as alias, macros or shortcuts, however, all of them should be created "manually" for each command.
Of course, there may be conflicts such not always the first letter could be used.
Let's say we have 2 different commands called
stopandshow(that can be found in many shells that support this kind of things).In this scenario, it would be great to automatically cmd2 allow using
shorshoas an alias of the commandshow, in the same way asstorstoas alias ofstop. Of courseshave 2 possible candidates, so it may be considered just an invalid/incomplete command.A possible implementation:
One possible implementation that may even work with subcommands is simple:
1 - the user input a command
2.1 - If the command exists or is an alias execute it as always
2.2 - if the commands does not exists, call autocomplete
3.1 - if the autocomplete returns none or more than 1 results, return invalid command as always
3.2 - if the autocomplete returns exactly one result, try to run that one and store it as if the user has written it down.