8000 add dynamic completion by umbynos · Pull Request #1509 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content

add dynamic completion #1509

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 22 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9d61a6e
add completion for `-b` or `--fqbn` for every command
umbynos Oct 18, 2021
b7263bf
add completion for `-l` or `--protocol`
umbynos Oct 18, 2021
b6ed62f
the previous implementation was working only with a board connected t…
umbynos Oct 5, 2021
3e57b18
add static completion for `--log-level, `--log-format` and `--format`
umbynos Oct 6, 2021
0c0823f
add completion for `-P` or `--programmer` & fix typo
umbynos Oct 6, 2021
fe4ab51
add completion for `core uninstall`
umbynos Oct 6, 2021
fc82c12
add completion for `core install` and `core download`
umbynos Oct 6, 2021
e79ae01
add completion for `lib uninstall`
umbynos Oct 6, 2021
6d560b2
add completion for `lib install`, `lib download`
umbynos Oct 7, 2021
3486d94
add completion for `lib examples`
umbynos Oct 7, 2021
28dfdc1
add completion for `config add`, `config remove`, `config delete` and…
umbynos Oct 7, 2021
27d300c
add completion for `lib deps`
umbynos Oct 7, 2021
d7be814
add tests
umbynos Oct 12, 2021
4559c25
enhance the completion for `config add` and `config remove`
umbynos Oct 13, 2021
6b1a01c
add description completion suggestion for core, lib, fqbn, programmer
umbynos Oct 13, 2021
5462031
add completion also for `-p` or `--port` flag
umbynos Oct 13, 2021
fe2cf65
remove the `toComplete` parameter from all the completion functions
umbynos Oct 13, 2021
2a953a9
fixes after rebase
umbynos Oct 13, 2021
7235896
update docs
umbynos Oct 13, 2021
d6c4732
add `-b` or `--fqbn` completion for the monitor command and tests
umbynos Oct 14, 2021
c5ee4d0
apply suggestions from code review
umbynos Oct 15, 2021
d6d4502
fixes after rebase
umbynos Oct 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove the toComplete parameter from all the completion functions
as of now this parameter is useless because if a string is typed in the terminal it cannot be swapped with a different one.
For example if I write `arduino-cli compile -b avr<TAB><TAB>` the completion function returns all elements starting with `arduino:avr...`.
So the completions are not showed because they cannot be swapped with a string that starts differently.
The only shell which seems to support this seems to be zsh
  • Loading branch information
umbynos committed Oct 18, 2021
commit fe2cf6522809e15aaa7cef3744f4b5c3e5e97e93
16 changes: 8 additions & 8 deletions cli/arguments/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// GetInstalledBoards is an helper function useful to autocomplete.
// It returns a list of fqbn
// it's taken from cli/board/listall.go
func GetInstalledBoards(toComplete string) []string {
func GetInstalledBoards() []string {
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime

list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
Expand All @@ -33,7 +33,7 @@ func GetInstalledBoards(toComplete string) []string {

// GetInstalledProtocols is an helper function useful to autocomplete.
// It returns a list of protocols available based on the installed boards
func GetInstalledProtocols(toComplete string) []string {
func GetInstalledProtocols() []string {
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
pm := commands.GetPackageManager(inst.Id)
boards := pm.InstalledBoards()
Expand All @@ -60,7 +60,7 @@ func GetInstalledProtocols(toComplete string) []string {

// GetInstalledProgrammers is an helper function useful to autocomplete.
// It returns a list of programmers available based on the installed boards
func GetInstalledProgrammers(toComplete string) []string {
func GetInstalledProgrammers() []string {
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
pm := commands.GetPackageManager(inst.Id)

Expand Down Expand Up @@ -91,7 +91,7 @@ func GetInstalledProgrammers(toComplete string) []string {

// GetUninstallableCores is an helper function useful to autocomplete.
// It returns a list of cores which can be uninstalled
func GetUninstallableCores(toComplete string) []string {
func GetUninstallableCores() []string {
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime

platforms, _ := core.GetPlatforms(&rpc.PlatformListRequest{
Expand All @@ -109,7 +109,7 @@ func GetUninstallableCores(toComplete string) []string {

// GetInstallableCores is an helper function useful to autocomplete.
// It returns a list of cores which can be installed/downloaded
func GetInstallableCores(toComplete string) []string {
func GetInstallableCores() []string {
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime

platforms, _ := core.PlatformSearch(&rpc.PlatformSearchRequest{
Expand All @@ -127,7 +127,7 @@ func GetInstallableCores(toComplete string) []string {

// GetUninstallableLibs is an helper function useful to autocomplete.
// It returns a list of libs which can be uninstalled
func GetUninstallableLibs(toComplete string) []string {
func GetUninstallableLibs() []string {
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
libs, _ := lib.LibraryList(context.Background(), &rpc.LibraryListRequest{
Instance: inst,
Expand All @@ -146,7 +146,7 @@ func GetUninstallableLibs(toComplete string) []string {

// GetInstallableLibs is an helper function useful to autocomplete.
// It returns a list of libs which can be installed/downloaded
func GetInstallableLibs(toComplete string) []string {
func GetInstallableLibs() []string {
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime

libs, _ := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
Expand All @@ -164,7 +164,7 @@ func GetInstallableLibs(toComplete string) []string {
// GetConnectedBoards is an helper function useful to autocomplete.
// It returns a list of boards which are currently connected
// Obviously it does not suggests network ports because of the timeout
func GetConnectedBoards(toComplete string) []string {
func GetConnectedBoards() []string {
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime

list, _ := board.List(&rpc.BoardListRequest{
Expand Down
4 changes: 2 additions & 2 deletions cli/arguments/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ type Port struct {
func (p *Port) AddToCommand(cmd *cobra.Command) {
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
cmd.RegisterFlagCompletionFunc("port", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetConnectedBoards(toComplete), cobra.ShellCompDirectiveDefault
return GetConnectedBoards(), cobra.ShellCompDirectiveDefault
})
cmd.Flags().StringVarP(&p.protocol, "protocol", "l", "", tr("Upload port protocol, e.g: serial"))
cmd.RegisterFlagCompletionFunc("protocol", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetInstalledProtocols(toComplete), cobra.ShellCompDirectiveDefault
return GetInstalledProtocols(), cobra.ShellCompDirectiveDefault
})
cmd.Flags().DurationVar(&p.timeout, "discovery-timeout", 5*time.Second, tr("Max time to wait for port discovery, e.g.: 30s, 1m"))
}
Expand Down
2 changes: 1 addition & 1 deletion cli/board/details.go
6D40
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func initDetailsCommand() *cobra.Command {
detailsCommand.Flags().BoolVarP(&showFullDetails, "full", "f", false, tr("Show full board details"))
detailsCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
detailsCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledBoards(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
detailsCommand.Flags().BoolVarP(&listProgrammers, "list-programmers", "", false, tr("Show list of available programmers"))
// detailsCommand.MarkFlagRequired("fqbn") // enable once `board details <fqbn>` is removed
Expand Down
4 changes: 2 additions & 2 deletions cli/burnbootloader/burnbootloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func NewCommand() *cobra.Command {

burnBootloaderCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
burnBootloaderCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledBoards(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
port.AddToCommand(burnBootloaderCommand)
burnBootloaderCommand.Flags().BoolVarP(&verify, "verify", "t", false, tr("Verify uploaded binary after the upload."))
burnBootloaderCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, tr("Turns on verbose mode."))
burnBootloaderCommand.Flags().StringVarP(&programmer, "programmer", "P", "", tr("Use the specified programmer to upload."))
burnBootloaderCommand.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledProgrammers(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
})
burnBootloaderCommand.Flags().BoolVar(&dryRun, "dry-run", false, tr("Do not perform the actual upload, just log out actions"))
burnBootloaderCommand.Flags().MarkHidden("dry-run")
Expand Down
4 changes: 2 additions & 2 deletions cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewCommand() *cobra.Command {

command.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
command.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledBoards(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
command.Flags().BoolVar(&showProperties, "show-properties", false, tr("Show all build properties used instead of compiling."))
command.Flags().BoolVar(&preprocess, "preprocess", false, tr("Print preprocessed code to stdout instead of compiling."))
Expand All @@ -110,7 +110,7 @@ func NewCommand() *cobra.Command {
command.Flags().BoolVar(&optimizeForDebug, "optimize-for-debug", false, tr("Optional, optimize compile output for debugging, rather than for release."))
command.Flags().StringVarP(&programmer, "programmer", "P", "", tr("Optional, use the specified programmer to upload."))
command.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledProgrammers(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
})
command.Flags().BoolVar(&compilationDatabaseOnly, "only-compilation-database", false, tr("Just produce the compilation database, without actually compiling."))
command.Flags().BoolVar(&clean, "clean", false, tr("Optional, cleanup the build folder and do not use any cached build."))
Expand Down
2 changes: 1 addition & 1 deletion cli/config/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func initAddCommand() *cobra.Command {
Args: cobra.MinimumNArgs(2),
Run: runAddCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetConfigurationKeys(toComplete), cobra.ShellCompDirectiveDefault
return GetConfigurationKeys(), cobra.ShellCompDirectiveDefault
},
}
return addCommand
Expand Down
2 changes: 1 addition & 1 deletion cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewCommand() *cobra.Command {

// GetConfigurationKeys is an helper function useful to autocomplete.
// It returns a list of configuration keys which can be changed
func GetConfigurationKeys(toComplete string) []string {
func GetConfigurationKeys() []string {
var res []string
keys := configuration.Settings.AllKeys()
for _, key := range keys {
Expand Down
2 changes: 1 addition & 1 deletion cli/config/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func initRemoveCommand() *cobra.Command {
Args: cobra.MinimumNArgs(2),
Run: runRemoveCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetConfigurationKeys(toComplete), cobra.ShellCompDirectiveDefault
return GetConfigurationKeys(), cobra.ShellCompDirectiveDefault
},
}
return addCommand
Expand Down
2 changes: 1 addition & 1 deletion cli/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func initDownloadCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
Run: runDownloadCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstallableCores(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstallableCores(), cobra.ShellCompDirectiveDefault
},
}
return downloadCommand
Expand Down
2 changes: 1 addition & 1 deletion cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func initInstallCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
Run: runInstallCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstallableCores(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstallableCores(), cobra.ShellCompDirectiveDefault
},
}
AddPostInstallFlagsToCommand(installCommand)
Expand Down
2 changes: 1 addition & 1 deletion cli/core/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func initUninstallCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
Run: runUninstallCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetUninstallableCores(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetUninstallableCores(), cobra.ShellCompDirectiveDefault
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func NewCommand() *cobra.Command {

debugCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
debugCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledBoards(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
port.AddToCommand(debugCommand)
debugCommand.Flags().StringVarP(&programmer, "programmer", "P", "", tr("Programmer to use for debugging"))
debugCommand.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledProgrammers(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
})
debugCommand.Flags().StringVar(&interpreter, "interpreter", "console", tr("Debug interpreter e.g.: %s", "console, mi, mi1, mi2, mi3"))
debugCommand.Flags().StringVarP(&importDir, "input-dir", "", "", tr("Directory containing binaries for debug."))
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/check_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func initDepsCommand() *cobra.Command {
Args: cobra.ExactArgs(1),
Run: runDepsCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetUninstallableLibs(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetUninstallableLibs(), cobra.ShellCompDirectiveDefault
},
}
return depsCommand
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func initDownloadCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
Run: runDownloadCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstallableLibs(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstallableLibs(), cobra.ShellCompDirectiveDefault
},
}
return downloadCommand
Expand Down
4 changes: 2 additions & 2 deletions cli/lib/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func initExamplesCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1),
Run: runExamplesCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetUninstallableLibs(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetUninstallableLibs(), cobra.ShellCompDirectiveDefault
},
}
examplesCommand.Flags().StringVarP(&examplesFlags.fqbn, "fqbn", "b", "", tr("Show libraries for the specified board FQBN."))
examplesCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledBoards(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
return examplesCommand
}
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func initInstallCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
Run: runInstallCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstallableLibs(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstallableLibs(), cobra.ShellCompDirectiveDefault
},
}
installCommand.Flags().BoolVar(&installFlags.noDeps, "no-deps", false, tr("Do not install dependencies."))
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func initUninstallCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
Run: runUninstallCommand,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetUninstallableLibs(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetUninstallableLibs(), cobra.ShellCompDirectiveDefault
},
}
return uninstallCommand
Expand Down
4 changes: 2 additions & 2 deletions cli/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewCommand() *cobra.Command {

uploadCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
uploadCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledBoards(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
port.AddToCommand(uploadCommand)
uploadCommand.Flags().StringVarP(&importDir, "input-dir", "", "", tr("Directory containing binaries to upload."))
Expand All @@ -65,7 +65,7 @@ func NewCommand() *cobra.Command {
uploadCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, tr("Optional, turns on verbose mode."))
uploadCommand.Flags().StringVarP(&programmer, "programmer", "P", "", tr("Optional, use the specified programmer to upload."))
uploadCommand.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledProgrammers(toComplete), cobra.ShellCompDirectiveDefault
return arguments.GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
})
uploadCommand.Flags().BoolVar(&dryRun, "dry-run", false, tr("Do not perform the actual upload, just log out actions"))
uploadCommand.Flags().MarkHidden("dry-run")
Expand Down
0