Skip to content

Added tab completion for mbed cli on bash #450

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

Merged
merged 10 commits into from
Apr 18, 2017

Conversation

mbartling
Copy link
Contributor

FYI, this is only the bash file for tab completion. It is expected to go in some DIR/etc/bash_completion.d/ directory. For example it goes into /usr/local/etc/bash_completion.d/ on my mac.

Can someone help with the installation?

@SeppoTakalo
Copy link

700 lines of script for just a completion???
Sound like a lot, compared to what I did for clitest.py https://github.com/ARMmbed/mbed-clitest/blob/master/bash_completion/clitest

it looks like same piece of code is repeating over and over again.

@screamerbg
Copy link
Contributor

@mbartling Please check @SeppoTakalo's comment. Perhaps his script can be reused.

@mbartling
Copy link
Contributor Author

There is a lot of repetition in the code, but I did this for clarity vs brevity. @theotherjimmy what do you think?

@mbartling
Copy link
Contributor Author

Also @SeppoTakalo, most of the lines look like this:

__mbedcomp "
    --help
    --all
    --message
    --verbose
    --very_verbose
    "

So the script looks way bigger than it actually is

@theotherjimmy
Copy link
Contributor

@SeppoTakalo You did in ~77 lines about 1/10th of the amount of tab completion in this PR. If you look, things like you snippet is repeated for sync, update, etc. I don't think that his code being long should block this PR either. If it needs to be re-factored to be smaller/easier to maintain, then we can do that in the future too.

@screamerbg
Copy link
Contributor

@mbartling Thanks for the PR. I agree with the simplicity vs brevity approach too. We still need to be aware about maintaining the list of options in the bash file. Perhaps this could be generated somehow?

@mbartling
Copy link
Contributor Author

mbartling commented Mar 10, 2017

@screamerbg May be we should generate this file using the mbed-cli help? I mentally generated this script via

for each command{
  for each subcommand{
    if '--' in subcommand
      commands[command]['--*'].append(subcommand)
    elif '-' in subcommand
      commands[command]['-*'].append(subcommand)
    # Some extra logic to handle the prev command (targets, toolchains, etc)
   }
}

@mbartling
Copy link
Contributor Author

This is what I have so far for a generation script

#!/usr/bin/env python

from collections import defaultdict
import re
import subprocess

commandRegex = r"^\s+(?P<command>\w+)\s+(?P<helptxt>[a-zA-Z ]*)$"  # This one extracts single commands and the help txt
subcommandRegex = r"^\s+(?P<command1>-+[\w_-]+(?P<modifier1>\s+[A-Z_-]+){0,1}(?P<command2>,\s+-+[\w_-]+(?P<modifier2>\s+[A-Z_-]+){0,1}){0,1})\s+(?P<helptxt>.*)$" # Gets just about everything

def getHelpTxt(command=None):
    if command:
        p = subprocess.Popen(["mbed", command, "-h"], stdout=subprocess.PIPE)
    else:
        p = subprocess.Popen(["mbed", "-h"], stdout=subprocess.PIPE)
    out, err = p.communicate()
    return out

if __name__ == '__main__':
    commands = defaultdict(defaultdict(list))
    helpTxt = getHelpTxt()
    #print helpTxt
    for line in helpTxt.split('\n'):
        match = re.search(commandRegex, line)
        if match:
            print "have match"
            print match.groupdict()

Both the regexes work

@screamerbg
Copy link
Contributor

@mbartling This is great! Have you tried the generated output?

@mbartling
Copy link
Contributor Author

Still working on it. Going to be out of town most of next week, but it shouldn't take too much longer to word for word generate the bash completion script

@screamerbg screamerbg mentioned this pull request Mar 22, 2017
@BlackstoneEngineering
Copy link

LGTM?

@screamerbg
Copy link
Contributor

@mbartling Can you add a paragraph and a link in README.md about tab competition and point to the extended documentation under tools/bash_completion/templates/README.md ?

@screamerbg
Copy link
Contributor

@mbartling To clarify - I'd like some instructions that will allow every user to take advantage of this. E.g. generate file, put it in folder A on system X or in folder B on system Y.

@mbartling
Copy link
Contributor Author

Will do this now

@mbartling mbartling closed this Mar 23, 2017
@mbartling mbartling reopened this Mar 23, 2017
@screamerbg
Copy link
Contributor

@mbartling Do you feel ready for this to be merged and released in 1.1?

@mbartling
Copy link
Contributor Author

Yes to the current mbed bash completion script, but no to the generator. Still waiting on #460 and ARMmbed/mbed-os#3993

@theotherjimmy
Copy link
Contributor

@mbartling To be clear, Is this PR ready to be merged? If not, what are we waiting on?

@mbartling
Copy link
Contributor Author

This PR can be merged as is, but the generator is not usable/integratabtle until #460 and ARMmbed/mbed-os#3993 get merged

@screamerbg
Copy link
Contributor

@mbartling Thanks for the PR. LGTM

@screamerbg screamerbg merged commit c45e406 into ARMmbed:master Apr 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants