package cmd import ( "os" "strings" "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ Use: "nib", Short: "capture and crystallize notes, todos, and events", Args: cobra.ArbitraryArgs, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { return cmd.Help() } return runAdd(cmd, args) }, SilenceUsage: true, } func Execute() error { if len(os.Args) > 1 { first := os.Args[1] isFlag := strings.HasPrefix(first, "-") && !strings.Contains(first, " ") if first != "help" && first != "completion" && !isFlag && !isSubcommand(first) { // "--" stops cobra from parsing glyph prefixes like "-" as flags rootCmd.SetArgs(append([]string{"add", "--"}, os.Args[1:]...)) } } return rootCmd.Execute() } func isSubcommand(name string) bool { for _, c := range rootCmd.Commands() { if c.Name() == name { return true } for _, alias := range c.Aliases { if alias == name { return true } } } return false } func init() { rootCmd.AddCommand(addCmd) rootCmd.AddCommand(lsCmd) }