Skip to content

Provide bash completion #124

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

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions commands/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ func InitCommand() *cobra.Command {
boardCommand.AddCommand(initDetailsCommand())
boardCommand.AddCommand(initListCommand())
boardCommand.AddCommand(initListAllCommand())
boardCommand.AddCommand(initListAllCommandOnlyFqbn())
return boardCommand
}
46 changes: 46 additions & 0 deletions commands/board/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package board

import (
"fmt"
"sort"
"strings"

Expand Down Expand Up @@ -78,3 +79,48 @@ func runListAllCommand(cmd *cobra.Command, args []string) {
sort.Sort(list)
formatter.Print(list)
}

func initListAllCommandOnlyFqbn() *cobra.Command {
listAllCommand := &cobra.Command{
Use: "listallfqbn",
Run: runListAllCommandOnlyFqbn,
Hidden: true,
Args: cobra.ArbitraryArgs,
}
return listAllCommand
}

func userAskForOptions(str string) bool {
// one arg must contain 3 or more colons
if strings.Count(str, ":") >= 3 {
return true
}
return false
}

// runListAllCommand list all installed boards
func runListAllCommandOnlyFqbn(cmd *cobra.Command, args []string) {
pm := commands.InitPackageManager()

for _, targetPackage := range pm.GetPackages().Packages {
for _, platform := range targetPackage.Platforms {
platformRelease := pm.GetInstalledPlatformRelease(platform)
if platformRelease == nil {
continue
}
for _, board := range platformRelease.Boards {
boardname := board.FQBN()
if board.GetConfigOptions() != nil && len(args) > 0 && userAskForOptions(args[0]) && strings.Contains(args[0], boardname) {
for _, option := range board.GetConfigOptions().FirstLevelKeys() {
str := boardname + ":" + option + "="
for _, value := range board.GetConfigOptionValues(option).FirstLevelKeys() {
fmt.Println(str + value)
}
}
} else {
fmt.Println(boardname)
}
}
}
}
}
61 changes: 46 additions & 15 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ import (
properties "github.com/arduino/go-properties-orderedmap"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// InitCommand prepares the command.
func InitCommand() *cobra.Command {

command := &cobra.Command{
Use: "compile",
Short: "Compiles Arduino sketches.",
Expand All @@ -47,9 +49,21 @@ func InitCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1),
Run: run,
}
command.Flags().StringVarP(
&flags.fqbn, "fqbn", "b", "",
"Fully Qualified Board Name, e.g.: arduino:avr:uno")

annotation := make(map[string][]string)
annotation[cobra.BashCompCustom] = []string{"__arduino_cli_boards_listall_fqbn"}

fqbn := &pflag.Flag{
Name: "fqbn",
Shorthand: "b",
DefValue: "",
Value: flags.fqbnWrapper,
Usage: "Fully Qualified Board Name, e.g.: arduino:avr:uno",
Annotations: annotation,
}
command.Flags().AddFlag(fqbn)
command.MarkFlagRequired("fqbn")

command.Flags().BoolVar(
&flags.showProperties, "show-properties", false,
"Show all build properties used instead of compiling.")
Expand Down Expand Up @@ -83,18 +97,35 @@ func InitCommand() *cobra.Command {
return command
}

type localValue struct {
}

func (r localValue) Set(v string) error {
flags.fqbn = v
return nil
}

func (r localValue) String() string {
return flags.fqbn
}

func (r localValue) Type() string {
return "fqbn"
}

var flags struct {
fqbn string // Fully Qualified Board Name, e.g.: arduino:avr:uno.
showProperties bool // Show all build preferences used instead of compiling.
preprocess bool // Print preprocessed code to stdout.
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
buildPath string // Path where to save compiled files.
buildProperties []string // List of custom build properties separated by commas. Or can be used multiple times for multiple properties.
warnings string // Used to tell gcc which warning level to use.
verbose bool // Turns on verbose mode.
quiet bool // Suppresses almost every output.
vidPid string // VID/PID specific build properties.
exportFile string // The compiled binary is written to this file
fqbnWrapper localValue // Fully Qualified Board Name, e.g.: arduino:avr:uno.
fqbn string // Fully Qualified Board Name, e.g.: arduino:avr:uno.
showProperties bool // Show all build preferences used instead of compiling.
preprocess bool // Print preprocessed code to stdout.
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
buildPath string // Path where to save compiled files.
buildProperties []string // List of custom build properties separated by commas. Or can be used multiple times for multiple properties.
warnings string // Used to tell gcc which warning level to use.
verbose bool // Turns on verbose mode.
quiet bool // Suppresses almost every output.
vidPid string // VID/PID specific build properties.
exportFile string // The compiled binary is written to this file
}

func run(cmd *cobra.Command, args []string) {
Expand All @@ -109,7 +140,7 @@ func run(cmd *cobra.Command, args []string) {
os.Exit(commands.ErrGeneric)
}

if flags.fqbn == "" && sketch != nil {
if flags.fqbn == "" && sketch != nil && sketch.Metadata != nil {
flags.fqbn = sketch.Metadata.CPU.Fqbn
}
if flags.fqbn == "" {
Expand Down
44 changes: 44 additions & 0 deletions commands/completion/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of arduino-cli.
*
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
*
* This software is released under the GNU General Public License version 3,
* which covers the main part of arduino-cli.
* The terms of this license can be found at:
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* You can be released from the requirements of the above licenses by purchasing
* a commercial license. Buying such a license is mandatory if you want to modify or
* otherwise use the software for commercial activities involving the Arduino
* software without disclosing the source code of your own applications. To purchase
* a commercial license, send an email to [email protected].
*/

package completion

import (
"os"

"github.com/spf13/cobra"
)

// InitCommand prepares the command.
func InitCommand() *cobra.Command {
// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "generate_autocomplete",
Short: "Generates bash completion scripts",
Long: `To load completion run
# arduino-cli generate_autocomplete > arduino-cli.sh
and then
# source arduino-cli.sh
or add the script to /etc/bash_completion.d/
`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Root().GenBashCompletion(os.Stdout)
},
Hidden: true,
}
return completionCmd
}
26 changes: 21 additions & 5 deletions commands/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/commands/compile"
"github.com/arduino/arduino-cli/commands/completion"
"github.com/arduino/arduino-cli/commands/config"
"github.com/arduino/arduino-cli/commands/core"
"github.com/arduino/arduino-cli/commands/generatedocs"
Expand All @@ -45,14 +46,28 @@ import (
"github.com/spf13/cobra"
)

const (
fqbn_bash_completion_func = `COMP_WORDBREAKS="\"'><;|&( "
__arduino_cli_boards_listall_fqbn()
{
local arduino_cli_output out
if arduino_cli_output=$(arduino-cli board listallfqbn $cur 2>/dev/null); then
out=($(echo "${arduino_cli_output}" | uniq | awk '{print $1}'))
COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) )
fi
}
`
)

// Init prepares the cobra root command.
func Init() *cobra.Command {
command := &cobra.Command{
Use: "arduino-cli",
Short: "Arduino CLI.",
Long: "Arduino Command Line Interface (arduino-cli).",
Example: " " + commands.AppName + " <command> [flags...]",
PersistentPreRun: preRun,
Use: "arduino-cli",
Short: "Arduino CLI.",
Long: "Arduino Command Line Interface (arduino-cli).",
Example: " " + commands.AppName + " <command> [flags...]",
PersistentPreRun: preRun,
BashCompletionFunction: fqbn_bash_completion_func,
}
command.PersistentFlags().BoolVar(&commands.GlobalFlags.Debug, "debug", false, "Enables debug output (super verbose, used to debug the CLI).")
command.PersistentFlags().StringVar(&commands.GlobalFlags.Format, "format", "text", "The output format, can be [text|json].")
Expand All @@ -69,6 +84,7 @@ func Init() *cobra.Command {
command.AddCommand(upload.InitCommand())
// command.AddCommand(validate.InitCommand())
command.AddCommand(version.InitCommand())
command.AddCommand(completion.InitCommand())
return command
}

Expand Down