Skip to content
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

POC Dynamic shell completion for fqbn flag #1431

Merged
merged 5 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/commands/compile"
"github.com/arduino/arduino-cli/commands/upload"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand Down Expand Up @@ -82,6 +83,9 @@ 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 getBoards(toComplete), 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."))
command.Flags().StringVar(&buildCachePath, "build-cache-path", "", tr("Builds of 'core.a' are saved into this path to be cached and reused."))
Expand Down Expand Up @@ -272,3 +276,19 @@ func (r *compileResult) String() string {
// The output is already printed via os.Stdout/os.Stdin
return ""
}

func getBoards(toComplete string) []string {
// from listall.go TODO optimize
inst := instance.CreateAndInit()

list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
Instance: inst,
SearchArgs: nil,
IncludeHiddenBoards: false,
})
var res []string
for _, i := range list.GetBoards() {
res = append(res, i.Fqbn)
}
return res
}
4 changes: 2 additions & 2 deletions cli/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func NewCommand() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
if completionNoDesc && (args[0] == "bash" || args[0] == "powershell") {
if completionNoDesc && (args[0] == "powershell") {
feedback.Errorf(tr("Error: command description is not supported by %v"), args[0])
os.Exit(errorcodes.ErrGeneric)
}
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
cmd.Root().GenBashCompletionV2(os.Stdout, !completionNoDesc)
break
case "zsh":
if completionNoDesc {
Expand Down
66 changes: 33 additions & 33 deletions i18n/data/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ msgstr "Board version:"
msgid "Bootloader file specified but missing: {0}"
msgstr "Bootloader file specified but missing: {0}"

#: cli/compile/compile.go:87
#: cli/compile/compile.go:91
msgid "Builds of 'core.a' are saved into this path to be cached and reused."
msgstr "Builds of 'core.a' are saved into this path to be cached and reused."

Expand Down Expand Up @@ -378,8 +378,8 @@ msgstr "Command keeps running and prints list of connected boards whenever there
msgid "Compiled sketch not found in %s"
msgstr "Compiled sketch not found in %s"

#: cli/compile/compile.go:73
#: cli/compile/compile.go:74
#: cli/compile/compile.go:75
msgid "Compiles Arduino sketches."
msgstr "Compiles Arduino sketches."

Expand Down Expand Up @@ -710,18 +710,18 @@ msgstr "Error during JSON encoding of the output: %v"

#: cli/burnbootloader/burnbootloader.go:70
#: cli/burnbootloader/burnbootloader.go:83
#: cli/compile/compile.go:195
#: cli/compile/compile.go:201
#: cli/compile/compile.go:211
#: cli/compile/compile.go:243
#: cli/compile/compile.go:199
#: cli/compile/compile.go:205
#: cli/compile/compile.go:215
#: cli/compile/compile.go:247
#: cli/upload/upload.go:95
#: cli/upload/upload.go:101
#: cli/upload/upload.go:117
#: cli/upload/upload.go:144
msgid "Error during Upload: %v"
msgstr "Error during Upload: %v"

#: cli/compile/compile.go:255
#: cli/compile/compile.go:259
msgid "Error during build: %v"
msgstr "Error during build: %v"

Expand Down Expand Up @@ -821,7 +821,7 @@ msgstr "Error listing boards: %v"
msgid "Error listing platforms: %v"
msgstr "Error listing platforms: %v"

#: cli/compile/compile.go:146
#: cli/compile/compile.go:150
msgid "Error opening source code overrides data file: %v"
msgstr "Error opening source code overrides data file: %v"

Expand Down Expand Up @@ -969,7 +969,7 @@ msgstr "Error writing library_index.json.sig"
msgid "Error: command description is not supported by %v"
msgstr "Error: command description is not supported by %v"

#: cli/compile/compile.go:153
#: cli/compile/compile.go:157
msgid "Error: invalid source code overrides data file: %v"
msgstr "Error: invalid source code overrides data file: %v"

Expand Down Expand Up @@ -1073,7 +1073,7 @@ msgstr "Force skip of post-install scripts (if the CLI is running interactively)

#: cli/board/details.go:50
#: cli/burnbootloader/burnbootloader.go:53
#: cli/compile/compile.go:84
#: cli/compile/compile.go:85
#: cli/debug/debug.go:61
#: cli/upload/upload.go:57
msgid "Fully Qualified Board Name, e.g.: arduino:avr:uno"
Expand Down Expand Up @@ -1135,7 +1135,7 @@ msgstr "Id"
msgid "Identification properties:"
msgstr "Identification properties:"

#: cli/compile/compile.go:114
#: cli/compile/compile.go:118
msgid "If set built binaries will be exported to the sketch folder."
msgstr "If set built binaries will be exported to the sketch folder."

Expand Down Expand Up @@ -1286,7 +1286,7 @@ msgstr "Invalid version"
msgid "Invalid vid value: '%s'"
msgstr "Invalid vid value: '%s'"

#: cli/compile/compile.go:109
#: cli/compile/compile.go:113
msgid "Just produce the compilation database, without actually compiling."
msgstr "Just produce the compilation database, without actually compiling."

Expand Down Expand Up @@ -1364,15 +1364,15 @@ msgstr "List all known boards and their corresponding FQBN."
msgid "List connected boards."
msgstr "List connected boards."

#: cli/compile/compile.go:92
#: cli/compile/compile.go:96
msgid "List of custom build properties separated by commas. Or can be used multiple times for multiple properties."
msgstr "List of custom build properties separated by commas. Or can be used multiple times for multiple properties."

#: cli/compile/compile.go:106
#: cli/compile/compile.go:110
msgid "List of custom libraries dir paths separated by commas. Or can be used multiple times for multiple libraries dir paths."
msgstr "List of custom libraries dir paths separated by commas. Or can be used multiple times for multiple libraries dir paths."

#: cli/compile/compile.go:104
#: cli/compile/compile.go:108
msgid "List of paths to libraries root folders. Libraries set this way have top priority in case of conflicts. Can be used multiple times for different libraries."
msgstr "List of paths to libraries root folders. Libraries set this way have top priority in case of conflicts. Can be used multiple times for different libraries."

Expand Down Expand Up @@ -1551,41 +1551,41 @@ msgstr "Official Arduino board:"
msgid "Option:"
msgstr "Option:"

#: cli/compile/compile.go:96
#: cli/compile/compile.go:100
msgid "Optional, can be: %s. Used to tell gcc which warning level to use (-W flag)."
msgstr "Optional, can be: %s. Used to tell gcc which warning level to use (-W flag)."

#: cli/compile/compile.go:110
#: cli/compile/compile.go:114
msgid "Optional, cleanup the build folder and do not use any cached build."
msgstr "Optional, cleanup the build folder and do not use any cached build."

#: cli/compile/compile.go:107
#: cli/compile/compile.go:111
msgid "Optional, optimize compile output for debugging, rather than for release."
msgstr "Optional, optimize compile output for debugging, rather than for release."

#: cli/compile/compile.go:98
#: cli/compile/compile.go:102
msgid "Optional, suppresses almost every output."
msgstr "Optional, suppresses almost every output."

#: cli/compile/compile.go:97
#: cli/compile/compile.go:101
#: cli/upload/upload.go:62
msgid "Optional, turns on verbose mode."
msgstr "Optional, turns on verbose mode."

#: cli/compile/compile.go:108
#: cli/compile/compile.go:112
#: cli/upload/upload.go:63
msgid "Optional, use the specified programmer to upload."
msgstr "Optional, use the specified programmer to upload."

#: cli/compile/compile.go:115
#: cli/compile/compile.go:119
msgid "Optional. Path to a .json file that contains a set of replacements of the sketch source code."
msgstr "Optional. Path to a .json file that contains a set of replacements of the sketch source code."

#: commands/daemon/monitor.go:72
msgid "OutputRate in Null monitor must be a float64"
msgstr "OutputRate in Null monitor must be a float64"

#: cli/compile/compile.go:94
#: cli/compile/compile.go:98
msgid "Override a build property with a custom value. Can be used multiple times for multiple properties."
msgstr "Override a build property with a custom value. Can be used multiple times for multiple properties."

Expand Down Expand Up @@ -1628,7 +1628,7 @@ msgstr "Paragraph: %s"
msgid "Path to the file where logs will be written."
msgstr "Path to the file where logs will be written."

#: cli/compile/compile.go:90
#: cli/compile/compile.go:94
msgid "Path where to save compiled files. If omitted, a directory will be created in the default temporary path of your OS."
msgstr "Path where to save compiled files. If omitted, a directory will be created in the default temporary path of your OS."

Expand Down Expand Up @@ -1702,7 +1702,7 @@ msgstr "Precompiled library in \"{0}\" not found"
msgid "Print details about a board."
msgstr "Print details about a board."

#: cli/compile/compile.go:86
#: cli/compile/compile.go:90
msgid "Print preprocessed code to stdout instead of compiling."
msgstr "Print preprocessed code to stdout instead of compiling."

Expand Down Expand Up @@ -1780,7 +1780,7 @@ msgstr "Running normal build of the core..."
msgid "Running recipe: {0}"
msgstr "Running recipe: {0}"

#: cli/compile/compile.go:88
#: cli/compile/compile.go:92
msgid "Save build artifacts in this directory."
msgstr "Save build artifacts in this directory."

Expand Down Expand Up @@ -1831,7 +1831,7 @@ msgstr "Settings key doesn't exist"
msgid "Show all available core versions."
msgstr "Show all available core versions."

#: cli/compile/compile.go:85
#: cli/compile/compile.go:89
msgid "Show all build properties used instead of compiling."
msgstr "Show all build properties used instead of compiling."

Expand Down Expand Up @@ -1926,7 +1926,7 @@ msgstr "Sketch too big; see %s for tips on reducing it."
msgid "Sketch uses {0} bytes ({2}%%) of program storage space. Maximum is {1} bytes."
msgstr "Sketch uses {0} bytes ({2}%%) of program storage space. Maximum is {1} bytes."

#: cli/compile/compile.go:136
#: cli/compile/compile.go:140
#: cli/sketch/archive.go:66
#: cli/upload/upload.go:87
msgid "Sketches with .pde extension are deprecated, please rename the following files to .ino:"
Expand Down Expand Up @@ -2205,7 +2205,7 @@ msgstr "Upload port found on %s"
msgid "Upload port protocol, e.g: serial"
msgstr "Upload port protocol, e.g: serial"

#: cli/compile/compile.go:99
#: cli/compile/compile.go:103
msgid "Upload the binary after the compilation."
msgstr "Upload the binary after the compilation."

Expand All @@ -2217,7 +2217,7 @@ msgstr "Upload the bootloader on the board using an external programmer."
msgid "Upload the bootloader."
msgstr "Upload the bootloader."

#: cli/compile/compile.go:217
#: cli/compile/compile.go:221
#: cli/upload/upload.go:123
msgid "Uploading to specified board using %s protocol requires the following info:"
msgstr "Uploading to specified board using %s protocol requires the following info:"
Expand Down Expand Up @@ -2290,7 +2290,7 @@ msgid "VERSION_NUMBER"
msgstr "VERSION_NUMBER"

#: cli/burnbootloader/burnbootloader.go:55
#: cli/compile/compile.go:101
#: cli/compile/compile.go:105
#: cli/upload/upload.go:61
msgid "Verify uploaded binary after the upload."
msgstr "Verify uploaded binary after the upload."
Expand Down Expand Up @@ -2335,7 +2335,7 @@ msgstr "Warning: tool '%s' is not installed. It might not be available for your
msgid "Website: %s"
msgstr "Website: %s"

#: cli/compile/compile.go:102
#: cli/compile/compile.go:106
msgid "When specified, VID/PID specific build properties are used, if board supports them."
msgstr "When specified, VID/PID specific build properties are used, if board supports them."

Expand Down Expand Up @@ -3005,7 +3005,7 @@ msgstr "platform %s is not installed"
msgid "platform not installed"
msgstr "platform not installed"

#: cli/compile/compile.go:120
#: cli/compile/compile.go:124
msgid "please use --build-property instead."
msgstr "please use --build-property instead."

Expand Down
14 changes: 7 additions & 7 deletions i18n/rice-box.go

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions test/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_completion_bash(run_command):
result = run_command(["completion", "bash"])
assert result.ok
assert result.stderr == ""
assert "_arduino-cli_root_command()" in result.stdout
assert "# bash completion V2 for arduino-cli" in result.stdout
assert "__start_arduino-cli()" in result.stdout


Expand Down Expand Up @@ -55,9 +55,11 @@ def test_completion_powershell(run_command):

def test_completion_bash_no_desc(run_command):
result = run_command(["completion", "bash", "--no-descriptions"])
assert not result.ok
assert result.stdout == ""
assert "Error: command description is not supported by bash" in result.stderr
assert result.ok
assert result.stderr == ""
assert "# bash completion V2 for arduino-cli" in result.stdout
assert "__start_arduino-cli()" in result.stdout
assert "__completeNoDesc" in result.stdout


def test_completion_zsh_no_desc(run_command):
Expand Down