You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by pa...@apache.org on 2016/09/07 22:20:18 UTC

[1/3] incubator-mynewt-newt git commit: MYNEWT-128

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop e287dae75 -> 40c8f6811


MYNEWT-128

fix the autocomplete when we have a short flag without a space


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/8634519a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/8634519a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/8634519a

Branch: refs/heads/develop
Commit: 8634519aafc65892304c888cf3f7f4c4032e28de
Parents: bcff03d
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Wed Sep 7 14:37:29 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Wed Sep 7 14:39:36 2016 -0700

----------------------------------------------------------------------
 newt/cli/complete_cmd.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8634519a/newt/cli/complete_cmd.go
----------------------------------------------------------------------
diff --git a/newt/cli/complete_cmd.go b/newt/cli/complete_cmd.go
index 957ee4c..dd113da 100644
--- a/newt/cli/complete_cmd.go
+++ b/newt/cli/complete_cmd.go
@@ -149,7 +149,7 @@ func completeRunCmd(cmd *cobra.Command, args []string) {
 		!strings.HasPrefix(extra_str, "--")
 
 	showLong := strings.HasPrefix(extra_str, "--") ||
-		strings.HasPrefix(extra_str, "-")
+		extra_str == "-"
 
 	if showLong {
 		r := regexp.MustCompile("^--[^\\W]+")


[3/3] incubator-mynewt-newt git commit: MYNEWT-126

Posted by pa...@apache.org.
MYNEWT-126

add target options for show


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/40c8f681
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/40c8f681
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/40c8f681

Branch: refs/heads/develop
Commit: 40c8f6811b4880ab99c014e7e70e5d91002bf304
Parents: 8634519
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Wed Sep 7 15:13:21 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Wed Sep 7 15:13:21 2016 -0700

----------------------------------------------------------------------
 newt/cli/target_cmds.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/40c8f681/newt/cli/target_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/target_cmds.go b/newt/cli/target_cmds.go
index f818ff6..b9ebada 100644
--- a/newt/cli/target_cmds.go
+++ b/newt/cli/target_cmds.go
@@ -360,7 +360,7 @@ func AddTargetCommands(cmd *cobra.Command) {
 		Example: showHelpEx,
 		Run:     targetShowCmd,
 	}
-
+	showCmd.ValidArgs = targetList()
 	targetCmd.AddCommand(showCmd)
 
 	setHelpText := "Set a target variable (<var-name>) on target " +


[2/3] incubator-mynewt-newt git commit: MYNEWT-126

Posted by pa...@apache.org.
MYNEWT-126

add autocompletion to newt.

To get autocompletion working in your shell you have to tell bash

complete -C "newt complete" newt

which tells the mac autocomplete code to use newt (sub command newt complete) to get autocompletions.

If you execute newt complete from the command line it just prints out a message saying its for bash


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/bcff03dd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/bcff03dd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/bcff03dd

Branch: refs/heads/develop
Commit: bcff03ddc971723094c0a8e1eda7049134ab6c39
Parents: e287dae
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Wed Sep 7 14:22:51 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Wed Sep 7 14:39:36 2016 -0700

----------------------------------------------------------------------
 newt/cli/build_cmds.go   |  11 ++-
 newt/cli/complete_cmd.go | 210 ++++++++++++++++++++++++++++++++++++++++++
 newt/cli/image_cmds.go   |   2 +
 newt/cli/run_cmds.go     |   1 +
 newt/newt.go             |  26 ++++++
 5 files changed, 249 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/bcff03dd/newt/cli/build_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/build_cmds.go b/newt/cli/build_cmds.go
index bc83c8d..d6d7034 100644
--- a/newt/cli/build_cmds.go
+++ b/newt/cli/build_cmds.go
@@ -309,6 +309,7 @@ func AddBuildCommands(cmd *cobra.Command) {
 		Run:   buildRunCmd,
 	}
 
+	buildCmd.ValidArgs = targetList()
 	cmd.AddCommand(buildCmd)
 
 	cleanCmd := &cobra.Command{
@@ -317,6 +318,7 @@ func AddBuildCommands(cmd *cobra.Command) {
 		Run:   cleanRunCmd,
 	}
 
+	cleanCmd.ValidArgs = append(targetList(), "all")
 	cmd.AddCommand(cleanCmd)
 
 	testCmd := &cobra.Command{
@@ -324,7 +326,7 @@ func AddBuildCommands(cmd *cobra.Command) {
 		Short: "Executes unit tests for one or more packages",
 		Run:   testRunCmd,
 	}
-
+	testCmd.ValidArgs = append(packageList(), "all")
 	cmd.AddCommand(testCmd)
 
 	loadHelpText := "Load app image to target for <target-name>."
@@ -335,6 +337,8 @@ func AddBuildCommands(cmd *cobra.Command) {
 		Long:  loadHelpText,
 		Run:   loadRunCmd,
 	}
+
+	loadCmd.ValidArgs = targetList()
 	cmd.AddCommand(loadCmd)
 
 	debugHelpText := "Open debugger session for <target-name>."
@@ -345,6 +349,8 @@ func AddBuildCommands(cmd *cobra.Command) {
 		Long:  debugHelpText,
 		Run:   debugRunCmd,
 	}
+
+	debugCmd.ValidArgs = targetList()
 	cmd.AddCommand(debugCmd)
 
 	sizeHelpText := "Calculate the size of target components specified by " +
@@ -356,5 +362,8 @@ func AddBuildCommands(cmd *cobra.Command) {
 		Long:  sizeHelpText,
 		Run:   sizeRunCmd,
 	}
+
+	sizeCmd.ValidArgs = targetList()
 	cmd.AddCommand(sizeCmd)
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/bcff03dd/newt/cli/complete_cmd.go
----------------------------------------------------------------------
diff --git a/newt/cli/complete_cmd.go b/newt/cli/complete_cmd.go
new file mode 100644
index 0000000..957ee4c
--- /dev/null
+++ b/newt/cli/complete_cmd.go
@@ -0,0 +1,210 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package cli
+
+import (
+	"fmt"
+	"os"
+	"regexp"
+	"strings"
+
+	"mynewt.apache.org/newt/newt/pkg"
+	"mynewt.apache.org/newt/newt/project"
+	"mynewt.apache.org/newt/newt/target"
+
+	"github.com/spf13/cobra"
+	"github.com/spf13/pflag"
+)
+
+func targetList() []string {
+	_ = project.Initialize()
+	targetNames := []string{}
+	for name, _ := range target.GetTargets() {
+		// Don't display the special unittest target; this is used
+		// internally by newt, so the user doesn't need to know about it.
+		// XXX: This is a hack; come up with a better solution for unit
+		// testing.
+		if !strings.HasSuffix(name, "/unittest") {
+			targetNames = append(targetNames, strings.TrimPrefix(name, "targets/"))
+		}
+	}
+	return targetNames
+}
+
+/* return a list of all packages */
+func packageList() []string {
+	var list []string
+	for _, repoHash := range project.GetProject().PackageList() {
+		for _, pack := range *repoHash {
+			lclPack := pack.(*pkg.LocalPackage)
+
+			if pkgIsTestable(lclPack) {
+				list = append(list, lclPack.FullName())
+			}
+		}
+	}
+	return list
+}
+
+func isValueInList(value string, list []string) int {
+	for i, v := range list {
+		if v == value {
+			return i
+		}
+	}
+	return -1
+}
+
+func completeRunCmd(cmd *cobra.Command, args []string) {
+	cmd_line := os.Getenv("COMP_LINE")
+
+	if cmd_line == "" {
+		fmt.Println("This command is intended to be used as part of " +
+			" bash autocomplete.  Its not intended to be called directly from " +
+			" the command line ")
+		return
+	}
+
+	root_cmd := cmd.Root()
+
+	args = strings.Split(cmd_line, " ")
+	found_cmd, _, _ := root_cmd.Find(args[1:])
+	if found_cmd == nil {
+		return
+	}
+
+	/* this is worth a long comment.  We have to find a command line
+	 * with the flags removed.  To do this, I look at the command
+	 * path for the command without flags, and remove anything that
+	 * doesn't match */
+	found_args := strings.Split(found_cmd.CommandPath(), " ")
+	last_arg := found_args[len(found_args)-1]
+
+	/* what is remaining after the last parsed argument */
+	ind := strings.Index(cmd_line, last_arg)
+	ind += len(last_arg)
+	extra_str := cmd_line[ind:]
+
+	if len(extra_str) == 0 {
+		/* this matched an exact command with no space afterwards.  There
+		 * is no autocomplete except this command (to add a space) */
+		fmt.Println(found_cmd.Name())
+		return
+	}
+
+	/* skip flags for now. This just removes them */
+	/* skip over complete flags. So the current bash autocomplete will
+	 * not complete flags */
+	cmd.Flags().VisitAll(func(flag *pflag.Flag) {
+		flg := fmt.Sprintf("--%s", flag.Name)
+		if flag.Value.Type() == "bool" {
+			/* skip the flag */
+			r := regexp.MustCompile(flg + "[\\W]+")
+			extra_str = r.ReplaceAllString(extra_str, "")
+		} else if flag.Value.Type() == "string" {
+			/* skip the string and the next word */
+			r := regexp.MustCompile(flg + "[\\W]+[^\\W]+[\\W]+")
+			extra_str = r.ReplaceAllString(extra_str, "")
+		}
+
+		sflg := fmt.Sprintf("-%s", flag.Shorthand)
+		if flag.Value.Type() == "bool" {
+			/* skip the flag */
+			r := regexp.MustCompile(sflg + "[\\W]+")
+			extra_str = r.ReplaceAllString(extra_str, "")
+		} else if flag.Value.Type() == "string" {
+			/* skip the string and the next word */
+			r := regexp.MustCompile(sflg + "[\\W]+[^\\W]+[\\W]+")
+			extra_str = r.ReplaceAllString(extra_str, "")
+		}
+	})
+
+	if len(extra_str) == 0 {
+		/* this matched an exact command with no space afterwards.  There
+		 * is no autocomplete except this command (to add a space) */
+		return
+	}
+
+	extra_str = strings.TrimLeft(extra_str, " ")
+
+	/* give flag hints if the person asks for them */
+	showShort := strings.HasPrefix(extra_str, "-") &&
+		!strings.HasPrefix(extra_str, "--")
+
+	showLong := strings.HasPrefix(extra_str, "--") ||
+		strings.HasPrefix(extra_str, "-")
+
+	if showLong {
+		r := regexp.MustCompile("^--[^\\W]+")
+		partial_flag := r.FindString(extra_str)
+		cmd.Flags().VisitAll(func(flag *pflag.Flag) {
+			flg := fmt.Sprintf("--%s", flag.Name)
+			if strings.HasPrefix(flg, partial_flag) {
+				fmt.Println(flg)
+			}
+		})
+	}
+
+	if showShort {
+		r := regexp.MustCompile("^-[^\\W]+")
+		partial_flag := r.FindString(extra_str)
+		cmd.Flags().VisitAll(func(flag *pflag.Flag) {
+			if len(flag.Shorthand) > 0 {
+				flg := fmt.Sprintf("-%s", flag.Shorthand)
+				if strings.HasPrefix(flg, partial_flag) {
+					fmt.Println(flg)
+				}
+			}
+		})
+	}
+
+	/* dump out valid arguments */
+	for _, c := range found_cmd.ValidArgs {
+		if strings.HasPrefix(c, extra_str) {
+			fmt.Printf("%s\n", c)
+		}
+	}
+
+	/* dump out possible sub commands */
+	for _, child_cmd := range found_cmd.Commands() {
+		if strings.HasPrefix(child_cmd.Name(), extra_str) {
+			fmt.Printf("%s\n", child_cmd.Name())
+		}
+	}
+}
+
+func AddCompleteCommands(cmd *cobra.Command) {
+	completeShortHelp := "Performs Bash Autocompletion (-C)"
+
+	completeLongHelp := completeShortHelp + ".\n\n" +
+		" this command reads environment variables COMP_LINE and COMP_POINT " +
+		" and will send completion options out stdout as one option per line  "
+
+	completeCmd := &cobra.Command{
+		Use:   "complete",
+		Short: completeShortHelp,
+		Long:  completeLongHelp,
+		Run:   completeRunCmd,
+	}
+
+	/* silence errors on the complete command because we have partial flags */
+	completeCmd.SilenceErrors = true
+	cmd.AddCommand(completeCmd)
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/bcff03dd/newt/cli/image_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/image_cmds.go b/newt/cli/image_cmds.go
index e3da963..50cb7ac 100644
--- a/newt/cli/image_cmds.go
+++ b/newt/cli/image_cmds.go
@@ -148,5 +148,7 @@ func AddImageCommands(cmd *cobra.Command) {
 		Example: createImageHelpEx,
 		Run:     createImageRunCmd,
 	}
+
+	createImageCmd.ValidArgs = targetList()
 	cmd.AddCommand(createImageCmd)
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/bcff03dd/newt/cli/run_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/run_cmds.go b/newt/cli/run_cmds.go
index c8a1078..401563d 100644
--- a/newt/cli/run_cmds.go
+++ b/newt/cli/run_cmds.go
@@ -119,5 +119,6 @@ func AddRunCommands(cmd *cobra.Command) {
 		Example: runHelpEx,
 		Run:     runRunCmd,
 	}
+	runCmd.ValidArgs = targetList()
 	cmd.AddCommand(runCmd)
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/bcff03dd/newt/newt.go
----------------------------------------------------------------------
diff --git a/newt/newt.go b/newt/newt.go
index 7624e2b..03fb8de 100644
--- a/newt/newt.go
+++ b/newt/newt.go
@@ -21,6 +21,7 @@ package main
 
 import (
 	"fmt"
+	"os"
 
 	log "github.com/Sirupsen/logrus"
 	"github.com/spf13/cobra"
@@ -113,7 +114,14 @@ func newtCmd() *cobra.Command {
 }
 
 func main() {
+
 	cmd := newtCmd()
+
+	/* some of the setup code logs which messes with autocomplete */
+	hold_lvl := log.GetLevel()
+	log.SetLevel(log.FatalLevel)
+
+	cli.AddCompleteCommands(cmd)
 	cli.AddProjectCommands(cmd)
 	cli.AddTargetCommands(cmd)
 	cli.AddBuildCommands(cmd)
@@ -121,5 +129,23 @@ func main() {
 	cli.AddRunCommands(cmd)
 	cli.AddValsCommands(cmd)
 
+	/* only pass the first two args to check for complete command */
+	if len(os.Args) > 2 {
+		cmd.SilenceErrors = true
+		cmd.SilenceUsage = true
+		bc, _, err := cmd.Find(os.Args[1:2])
+		tmpArgs := os.Args
+		os.Args = tmpArgs[0:2]
+
+		if err == nil && bc.Name() == "complete" {
+			bc.Execute()
+			return
+		}
+		os.Args = tmpArgs
+		cmd.SilenceErrors = false
+		cmd.SilenceUsage = false
+	}
+
+	log.SetLevel(hold_lvl)
 	cmd.Execute()
 }