Add flag shell completion (values for --output and --visibility)
authorMikael Berthe <mikael@lilotux.net>
Sun, 30 Apr 2017 15:24:08 +0200
changeset 35 61ed03c3f134
parent 34 c604b1c786fd
child 36 c601c0d7b3b4
Add flag shell completion (values for --output and --visibility)
cmd/root.go
cmd/status.go
cmd/toot.go
--- a/cmd/root.go	Sun Apr 30 11:11:38 2017 +0200
+++ b/cmd/root.go	Sun Apr 30 15:24:08 2017 +0200
@@ -30,6 +30,16 @@
 var outputFormat string
 var outputTemplate, outputTemplateFile string
 
+// Shell completion functions
+const shellComplFunc = `
+__madonctl_visibility() {
+	COMPREPLY=( direct private unlisted public )
+}
+__madonctl_output() {
+	COMPREPLY=( plain json yaml template )
+}
+`
+
 // RootCmd represents the base command when called without any subcommands
 var RootCmd = &cobra.Command{
 	Use:               AppName,
@@ -84,6 +94,7 @@
   madonctl accounts --account-id 1 followers --template '{{.acct}}{{"\n"}}'
   madonctl config whoami
   madonctl timeline :mastodon`,
+	BashCompletionFunction: shellComplFunc,
 }
 
 // Execute adds all child commands to the root command sets flags appropriately.
@@ -121,6 +132,12 @@
 	viper.BindPFlag("login", RootCmd.PersistentFlags().Lookup("login"))
 	viper.BindPFlag("password", RootCmd.PersistentFlags().Lookup("password"))
 	viper.BindPFlag("token", RootCmd.PersistentFlags().Lookup("token"))
+
+	// Flag completion
+	annotation := make(map[string][]string)
+	annotation[cobra.BashCompCustom] = []string{"__madonctl_output"}
+
+	RootCmd.PersistentFlags().Lookup("output").Annotations = annotation
 }
 
 func checkOutputFormat(cmd *cobra.Command, args []string) error {
--- a/cmd/status.go	Sun Apr 30 11:11:38 2017 +0200
+++ b/cmd/status.go	Sun Apr 30 15:24:08 2017 +0200
@@ -59,6 +59,12 @@
 	statusPostSubcommand.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media file name")
 	statusPostSubcommand.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)")
 	statusPostSubcommand.Flags().IntVarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
+
+	// Flag completion
+	annotation := make(map[string][]string)
+	annotation[cobra.BashCompCustom] = []string{"__madonctl_visibility"}
+
+	statusPostSubcommand.Flags().Lookup("visibility").Annotations = annotation
 }
 
 // statusCmd represents the status command
--- a/cmd/toot.go	Sun Apr 30 11:11:38 2017 +0200
+++ b/cmd/toot.go	Sun Apr 30 15:24:08 2017 +0200
@@ -25,6 +25,12 @@
 	tootAliasCmd.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media attachment file name")
 	tootAliasCmd.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)")
 	tootAliasCmd.Flags().IntVarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
+
+	// Flag completion
+	annotation := make(map[string][]string)
+	annotation[cobra.BashCompCustom] = []string{"__madonctl_visibility"}
+
+	tootAliasCmd.Flags().Lookup("visibility").Annotations = annotation
 }
 
 var tootAliasCmd = &cobra.Command{