From 2df261e42b49ceb1b3b6d71932a5837045d1b461 Mon Sep 17 00:00:00 2001
From: Nick Craig-Wood <nick@craig-wood.com>
Date: Wed, 3 Aug 2016 20:41:08 +0100
Subject: [PATCH] Add genautocomplete command to make bash completion script.

---
 rclone.go | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/rclone.go b/rclone.go
index f1e4ede08..a0daaaba4 100644
--- a/rclone.go
+++ b/rclone.go
@@ -82,7 +82,8 @@ func init() {
 	rootCmd.AddCommand(copyCmd, syncCmd, moveCmd, lsCmd, lsdCmd,
 		lslCmd, md5sumCmd, sha1sumCmd, sizeCmd, mkdirCmd,
 		rmdirCmd, purgeCmd, deleteCmd, checkCmd, dedupeCmd,
-		configCmd, authorizeCmd, cleanupCmd, memtestCmd, versionCmd)
+		genautocompleteCmd, configCmd, authorizeCmd,
+		cleanupCmd, memtestCmd, versionCmd)
 	dedupeCmd.Flags().VarP(&dedupeMode, "dedupe-mode", "", "Dedupe mode interactive|skip|first|newest|oldest|rename.")
 	cobra.OnInitialize(initConfig)
 }
@@ -479,6 +480,38 @@ var configCmd = &cobra.Command{
 	},
 }
 
+var genautocompleteCmd = &cobra.Command{
+	Use:   "genautocomplete [output_file]",
+	Short: `Output bash completion script for rclone.`,
+	Long: `
+Generates a bash shell autocompletion script for rclone.
+
+This writes to /etc/bash_completion.d/rclone by default so will
+probably need to be run with sudo or as root, eg
+
+    sudo rclone genautocomplete
+
+Logout and login again to use the autocompletion scripts, or source
+them directly
+
+    . /etc/bash_completion
+
+If you supply a command line argument the script will be written
+there.
+`,
+	Run: func(cmd *cobra.Command, args []string) {
+		checkArgs(0, 1, cmd, args)
+		out := "/etc/bash_completion.d/rclone"
+		if len(args) > 0 {
+			out = args[0]
+		}
+		err := rootCmd.GenBashCompletionFile(out)
+		if err != nil {
+			log.Fatal(err)
+		}
+	},
+}
+
 var authorizeCmd = &cobra.Command{
 	Use:   "authorize",
 	Short: `Remote authorization.`,