Skip to content

Commit 826adc5

Browse files
authored
POC Dynamic shell completion for fqbn flag (#1431)
* make fqbn flag required, so that the completion suggests it when pressing <tab><tab> for better explanation see https://github.com/spf13/cobra/blob/master/shell_completions.md#mark-flags-as-required * use newer and supported bashv2 completion in order to have dynamic completion * add dynamic completion for `compile -b` command to list available board * Revert "make fqbn flag required, so that the completion suggests it when pressing <tab><tab>" This reverts commit cc81265. * make Internalization happy
1 parent 58ddae7 commit 826adc5

File tree

5 files changed

+68
-46
lines changed

5 files changed

+68
-46
lines changed

Diff for: cli/compile/compile.go

+20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
"github.com/arduino/arduino-cli/cli/errorcodes"
3333
"github.com/arduino/arduino-cli/cli/instance"
34+
"github.com/arduino/arduino-cli/commands/board"
3435
"github.com/arduino/arduino-cli/commands/compile"
3536
"github.com/arduino/arduino-cli/commands/upload"
3637
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -82,6 +83,9 @@ func NewCommand() *cobra.Command {
8283
}
8384

8485
command.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
86+
command.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
87+
return getBoards(toComplete), cobra.ShellCompDirectiveDefault
88+
})
8589
command.Flags().BoolVar(&showProperties, "show-properties", false, tr("Show all build properties used instead of compiling."))
8690
command.Flags().BoolVar(&preprocess, "preprocess", false, tr("Print preprocessed code to stdout instead of compiling."))
8791
command.Flags().StringVar(&buildCachePath, "build-cache-path", "", tr("Builds of 'core.a' are saved into this path to be cached and reused."))
@@ -272,3 +276,19 @@ func (r *compileResult) String() string {
272276
// The output is already printed via os.Stdout/os.Stdin
273277
return ""
274278
}
279+
280+
func getBoards(toComplete string) []string {
281+
// from listall.go TODO optimize
282+
inst := instance.CreateAndInit()
283+
284+
list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
285+
Instance: inst,
286+
SearchArgs: nil,
287+
IncludeHiddenBoards: false,
288+
})
289+
var res []string
290+
for _, i := range list.GetBoards() {
291+
res = append(res, i.Fqbn)
292+
}
293+
return res
294+
}

Diff for: cli/completion/completion.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ func NewCommand() *cobra.Command {
4747
}
4848

4949
func run(cmd *cobra.Command, args []string) {
50-
if completionNoDesc && (args[0] == "bash" || args[0] == "powershell") {
50+
if completionNoDesc && (args[0] == "powershell") {
5151
feedback.Errorf(tr("Error: command description is not supported by %v"), args[0])
5252
os.Exit(errorcodes.ErrGeneric)
5353
}
5454
switch args[0] {
5555
case "bash":
56-
cmd.Root().GenBashCompletion(os.Stdout)
56+
cmd.Root().GenBashCompletionV2(os.Stdout, !completionNoDesc)
5757
break
5858
case "zsh":
5959
if completionNoDesc {

Diff for: i18n/data/en.po

+33-33
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ msgstr "Board version:"
239239
msgid "Bootloader file specified but missing: {0}"
240240
msgstr "Bootloader file specified but missing: {0}"
241241

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

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

381-
#: cli/compile/compile.go:73
382381
#: cli/compile/compile.go:74
382+
#: cli/compile/compile.go:75
383383
msgid "Compiles Arduino sketches."
384384
msgstr "Compiles Arduino sketches."
385385

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

711711
#: cli/burnbootloader/burnbootloader.go:70
712712
#: cli/burnbootloader/burnbootloader.go:83
713-
#: cli/compile/compile.go:195
714-
#: cli/compile/compile.go:201
715-
#: cli/compile/compile.go:211
716-
#: cli/compile/compile.go:243
713+
#: cli/compile/compile.go:199
714+
#: cli/compile/compile.go:205
715+
#: cli/compile/compile.go:215
716+
#: cli/compile/compile.go:247
717717
#: cli/upload/upload.go:95
718718
#: cli/upload/upload.go:101
719719
#: cli/upload/upload.go:117
720720
#: cli/upload/upload.go:144
721721
msgid "Error during Upload: %v"
722722
msgstr "Error during Upload: %v"
723723

724-
#: cli/compile/compile.go:255
724+
#: cli/compile/compile.go:259
725725
msgid "Error during build: %v"
726726
msgstr "Error during build: %v"
727727

@@ -821,7 +821,7 @@ msgstr "Error listing boards: %v"
821821
msgid "Error listing platforms: %v"
822822
msgstr "Error listing platforms: %v"
823823

824-
#: cli/compile/compile.go:146
824+
#: cli/compile/compile.go:150
825825
msgid "Error opening source code overrides data file: %v"
826826
msgstr "Error opening source code overrides data file: %v"
827827

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

972-
#: cli/compile/compile.go:153
972+
#: cli/compile/compile.go:157
973973
msgid "Error: invalid source code overrides data file: %v"
974974
msgstr "Error: invalid source code overrides data file: %v"
975975

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

10741074
#: cli/board/details.go:50
10751075
#: cli/burnbootloader/burnbootloader.go:53
1076-
#: cli/compile/compile.go:84
1076+
#: cli/compile/compile.go:85
10771077
#: cli/debug/debug.go:61
10781078
#: cli/upload/upload.go:57
10791079
msgid "Fully Qualified Board Name, e.g.: arduino:avr:uno"
@@ -1135,7 +1135,7 @@ msgstr "Id"
11351135
msgid "Identification properties:"
11361136
msgstr "Identification properties:"
11371137

1138-
#: cli/compile/compile.go:114
1138+
#: cli/compile/compile.go:118
11391139
msgid "If set built binaries will be exported to the sketch folder."
11401140
msgstr "If set built binaries will be exported to the sketch folder."
11411141

@@ -1286,7 +1286,7 @@ msgstr "Invalid version"
12861286
msgid "Invalid vid value: '%s'"
12871287
msgstr "Invalid vid value: '%s'"
12881288

1289-
#: cli/compile/compile.go:109
1289+
#: cli/compile/compile.go:113
12901290
msgid "Just produce the compilation database, without actually compiling."
12911291
msgstr "Just produce the compilation database, without actually compiling."
12921292

@@ -1364,15 +1364,15 @@ msgstr "List all known boards and their corresponding FQBN."
13641364
msgid "List connected boards."
13651365
msgstr "List connected boards."
13661366

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

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

1375-
#: cli/compile/compile.go:104
1375+
#: cli/compile/compile.go:108
13761376
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."
13771377
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."
13781378

@@ -1551,41 +1551,41 @@ msgstr "Official Arduino board:"
15511551
msgid "Option:"
15521552
msgstr "Option:"
15531553

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

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

1562-
#: cli/compile/compile.go:107
1562+
#: cli/compile/compile.go:111
15631563
msgid "Optional, optimize compile output for debugging, rather than for release."
15641564
msgstr "Optional, optimize compile output for debugging, rather than for release."
15651565

1566-
#: cli/compile/compile.go:98
1566+
#: cli/compile/compile.go:102
15671567
msgid "Optional, suppresses almost every output."
15681568
msgstr "Optional, suppresses almost every output."
15691569

1570-
#: cli/compile/compile.go:97
1570+
#: cli/compile/compile.go:101
15711571
#: cli/upload/upload.go:62
15721572
msgid "Optional, turns on verbose mode."
15731573
msgstr "Optional, turns on verbose mode."
15741574

1575-
#: cli/compile/compile.go:108
1575+
#: cli/compile/compile.go:112
15761576
#: cli/upload/upload.go:63
15771577
msgid "Optional, use the specified programmer to upload."
15781578
msgstr "Optional, use the specified programmer to upload."
15791579

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

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

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

@@ -1628,7 +1628,7 @@ msgstr "Paragraph: %s"
16281628
msgid "Path to the file where logs will be written."
16291629
msgstr "Path to the file where logs will be written."
16301630

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

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

1705-
#: cli/compile/compile.go:86
1705+
#: cli/compile/compile.go:90
17061706
msgid "Print preprocessed code to stdout instead of compiling."
17071707
msgstr "Print preprocessed code to stdout instead of compiling."
17081708

@@ -1780,7 +1780,7 @@ msgstr "Running normal build of the core..."
17801780
msgid "Running recipe: {0}"
17811781
msgstr "Running recipe: {0}"
17821782

1783-
#: cli/compile/compile.go:88
1783+
#: cli/compile/compile.go:92
17841784
msgid "Save build artifacts in this directory."
17851785
msgstr "Save build artifacts in this directory."
17861786

@@ -1831,7 +1831,7 @@ msgstr "Settings key doesn't exist"
18311831
msgid "Show all available core versions."
18321832
msgstr "Show all available core versions."
18331833

1834-
#: cli/compile/compile.go:85
1834+
#: cli/compile/compile.go:89
18351835
msgid "Show all build properties used instead of compiling."
18361836
msgstr "Show all build properties used instead of compiling."
18371837

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

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

2208-
#: cli/compile/compile.go:99
2208+
#: cli/compile/compile.go:103
22092209
msgid "Upload the binary after the compilation."
22102210
msgstr "Upload the binary after the compilation."
22112211

@@ -2217,7 +2217,7 @@ msgstr "Upload the bootloader on the board using an external programmer."
22172217
msgid "Upload the bootloader."
22182218
msgstr "Upload the bootloader."
22192219

2220-
#: cli/compile/compile.go:217
2220+
#: cli/compile/compile.go:221
22212221
#: cli/upload/upload.go:123
22222222
msgid "Uploading to specified board using %s protocol requires the following info:"
22232223
msgstr "Uploading to specified board using %s protocol requires the following info:"
@@ -2290,7 +2290,7 @@ msgid "VERSION_NUMBER"
22902290
msgstr "VERSION_NUMBER"
22912291

22922292
#: cli/burnbootloader/burnbootloader.go:55
2293-
#: cli/compile/compile.go:101
2293+
#: cli/compile/compile.go:105
22942294
#: cli/upload/upload.go:61
22952295
msgid "Verify uploaded binary after the upload."
22962296
msgstr "Verify uploaded binary after the upload."
@@ -2335,7 +2335,7 @@ msgstr "Warning: tool '%s' is not installed. It might not be available for your
23352335
msgid "Website: %s"
23362336
msgstr "Website: %s"
23372337

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

@@ -3005,7 +3005,7 @@ msgstr "platform %s is not installed"
30053005
msgid "platform not installed"
30063006
msgstr "platform not installed"
30073007

3008-
#: cli/compile/compile.go:120
3008+
#: cli/compile/compile.go:124
30093009
msgid "please use --build-property instead."
30103010
msgstr "please use --build-property instead."
30113011

Diff for: i18n/rice-box.go

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: test/test_completion.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_completion_bash(run_command):
2525
result = run_command(["completion", "bash"])
2626
assert result.ok
2727
assert result.stderr == ""
28-
assert "_arduino-cli_root_command()" in result.stdout
28+
assert "# bash completion V2 for arduino-cli" in result.stdout
2929
assert "__start_arduino-cli()" in result.stdout
3030

3131

@@ -55,9 +55,11 @@ def test_completion_powershell(run_command):
5555

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

6264

6365
def test_completion_zsh_no_desc(run_command):

0 commit comments

Comments
 (0)