Skip to content

Commit

Permalink
tools: add bash completion for node
Browse files Browse the repository at this point in the history
This commit adds a --completion-bash option to node which can be
sourced to provide bash code completion for node options.

Usage:
$ node --completion-bash  > node_bash_completion
$ source node_bash_completion
$ node --[tab]

PR-URL: #20713
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
  • Loading branch information
danbev authored and targos committed Sep 21, 2018
1 parent b0e86ea commit a22485d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 1 deletion.
11 changes: 11 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ If this flag is passed, the behavior can still be set to not abort through
[`process.setUncaughtExceptionCaptureCallback()`][] (and through usage of the
`domain` module that uses it).

### `--completion-bash`
<!-- YAML
added: REPLACEME
-->

Print source-able bash completion script for Node.js.
```console
$ node --completion-bash > node_bash_completion
$ source node_bash_completion
```

### `--enable-fips`
<!-- YAML
added: v6.0.0
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ the next argument will be used as a script filename.
.It Fl -abort-on-uncaught-exception
Aborting instead of exiting causes a core file to be generated for analysis.
.
.It Fl -completion-bash
Print source-able bash completion script for Node.js.
.
.It Fl -enable-fips
Enable FIPS-compliant crypto at startup.
Requires Node.js to be built with
Expand Down
25 changes: 25 additions & 0 deletions lib/internal/bash_completion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
const { internalBinding } = require('internal/bootstrap/loaders');
const { getOptions } = internalBinding('options');

function print(stream) {
const { options, aliases } = getOptions();
const all_opts = [...options.keys(), ...aliases.keys()];

stream.write(`_node_complete() {
local cur_word options
cur_word="\${COMP_WORDS[COMP_CWORD]}"
if [[ "\${cur_word}" == -* ]] ; then
COMPREPLY=( $(compgen -W '${all_opts.join(' ')}' -- "\${cur_word}") )
return 0
else
COMPREPLY=( $(compgen -f "\${cur_word}") )
return 0
fi
}
complete -F _node_complete node node_g`);
}

module.exports = {
print
};
8 changes: 7 additions & 1 deletion lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,17 @@
NativeModule.require('internal/inspector_async_hook').setup();
}

if (internalBinding('options').getOptions('--help')) {
const options = internalBinding('options');
if (options.getOptions('--help')) {
NativeModule.require('internal/print_help').print(process.stdout);
return;
}

if (options.getOptions('--completion-bash')) {
NativeModule.require('internal/bash_completion').print(process.stdout);
return;
}

if (isMainThread) {
mainThreadSetup.setupChildProcessIpcChannel();
}
Expand Down
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
'lib/zlib.js',
'lib/internal/assert.js',
'lib/internal/async_hooks.js',
'lib/internal/bash_completion.js',
'lib/internal/buffer.js',
'lib/internal/cli_table.js',
'lib/internal/child_process.js',
Expand Down
3 changes: 3 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ PerProcessOptionsParser::PerProcessOptionsParser() {
kAllowedInEnvironment);

AddOption("--security-reverts", "", &PerProcessOptions::security_reverts);
AddOption("--completion-bash",
"print source-able bash completion script",
&PerProcessOptions::print_bash_completion);
AddOption("--help",
"print node command line options",
&PerProcessOptions::print_help);
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class PerProcessOptions {
bool zero_fill_all_buffers = false;

std::vector<std::string> security_reverts;
bool print_bash_completion = false;
bool print_help = false;
bool print_v8_help = false;
bool print_version = false;
Expand Down

0 comments on commit a22485d

Please sign in to comment.