Repository: yargs/yargs (js)

Issue:
Generated completion script install instructions ignore custom completion command
**Problem description:**

When using a custom completion command, the generated bash completion script includes installation instructions in a comment at the top which contain the hardcoded `completion` command rather than the actual command name. Thus, they are incorrect when using a custom completion command.

**Relevant code:**

```js
yargs.completion('flintlock') // ...rest
```

**Input:**

```sh
./bin/my-app.js flintlock
```

**Current output (snippet):**

```
###-begin-my-app.js-completions-###
#
# yargs command completion script
#
# Installation: ./bin/my-app.js completion >> ~/.bashrc
#    or ./bin/my-app.js completion >> ~/.bash_profile on OSX.
#
```

**Expected output (snippet):**

```
###-begin-my-app.js-completions-###
#
# yargs command completion script
#
# Installation: ./bin/my-app.js flintlock >> ~/.bashrc
#    or ./bin/my-app.js flintlock >> ~/.bash_profile on OSX.
#
```

**Proposed solution:**

Use a placeholder in the completion script template and replace it with the custom completion command name, similar to the app name and path. PR incoming.


Pull request description:
fix: use correct completion command in generated completion script
Closes #987.

Patch:
diff --git a/completion.sh.hbs b/completion.sh.hbs
index 14445b8..819c8ae 100644
--- a/completion.sh.hbs
+++ b/completion.sh.hbs
@@ -2,8 +2,8 @@
 #
 # yargs command completion script
 #
-# Installation: {{app_path}} completion >> ~/.bashrc
-#    or {{app_path}} completion >> ~/.bash_profile on OSX.
+# Installation: {{app_path}} {{completion_command}} >> ~/.bashrc
+#    or {{app_path}} {{completion_command}} >> ~/.bash_profile on OSX.
 #
 _yargs_completions()
 {
diff --git a/lib/completion.js b/lib/completion.js
index 1cbd796..ad6969a 100644
--- a/lib/completion.js
+++ b/lib/completion.js
@@ -78,7 +78,7 @@ module.exports = function completion (yargs, usage, command) {
   }
 
   // generate the completion script to add to your .bashrc.
-  self.generateCompletionScript = function generateCompletionScript ($0) {
+  self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
     let script = fs.readFileSync(
       path.resolve(__dirname, '../completion.sh.hbs'),
       'utf-8'
@@ -89,6 +89,7 @@ module.exports = function completion (yargs, usage, command) {
     if ($0.match(/\.js$/)) $0 = `./${$0}`
 
     script = script.replace(/{{app_name}}/g, name)
+    script = script.replace(/{{completion_command}}/g, cmd)
     return script.replace(/{{app_path}}/g, $0)
   }
 
diff --git a/yargs.js b/yargs.js
index 3d5d7cd..7f04ab4 100644
--- a/yargs.js
+++ b/yargs.js
@@ -861,7 +861,7 @@ function Yargs (processArgs, cwd, parentRequire) {
   self.showCompletionScript = function ($0) {
     argsert('[string]', [$0], arguments.length)
     $0 = $0 || self.$0
-    _logger.log(completion.generateCompletionScript($0))
+    _logger.log(completion.generateCompletionScript($0, completionCommand))
     return self
   }
 


Test patch:
diff --git a/test/completion.js b/test/completion.js
index eb80c30..75f59ef 100644
--- a/test/completion.js
+++ b/test/completion.js
@@ -172,6 +172,13 @@ describe('Completion', () => {
       r.logs[0].should.match(/ndm --get-yargs-completions/)
     })
 
+    it('replaces completion command variable with custom completion command in script', () => {
+      const r = checkUsage(() => yargs([]).completion('flintlock')
+          .showCompletionScript(), ['ndm'])
+
+      r.logs[0].should.match(/ndm flintlock >>/)
+    })
+
     it('if $0 has a .js extension, a ./ prefix is added', () => {
       const r = checkUsage(() => yargs([])
           .showCompletionScript(), ['test.js'])