Skip to content

Commit a22485d

Browse files
danbevtargos
authored andcommitted
tools: add bash completion for node
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent b0e86ea commit a22485d

File tree

7 files changed

+51
-1
lines changed

7 files changed

+51
-1
lines changed

doc/api/cli.md

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ If this flag is passed, the behavior can still be set to not abort through
5252
[`process.setUncaughtExceptionCaptureCallback()`][] (and through usage of the
5353
`domain` module that uses it).
5454

55+
### `--completion-bash`
56+
<!-- YAML
57+
added: REPLACEME
58+
-->
59+
60+
Print source-able bash completion script for Node.js.
61+
```console
62+
$ node --completion-bash > node_bash_completion
63+
$ source node_bash_completion
64+
```
65+
5566
### `--enable-fips`
5667
<!-- YAML
5768
added: v6.0.0

doc/node.1

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ the next argument will be used as a script filename.
7575
.It Fl -abort-on-uncaught-exception
7676
Aborting instead of exiting causes a core file to be generated for analysis.
7777
.
78+
.It Fl -completion-bash
79+
Print source-able bash completion script for Node.js.
80+
.
7881
.It Fl -enable-fips
7982
Enable FIPS-compliant crypto at startup.
8083
Requires Node.js to be built with

lib/internal/bash_completion.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const { internalBinding } = require('internal/bootstrap/loaders');
3+
const { getOptions } = internalBinding('options');
4+
5+
function print(stream) {
6+
const { options, aliases } = getOptions();
7+
const all_opts = [...options.keys(), ...aliases.keys()];
8+
9+
stream.write(`_node_complete() {
10+
local cur_word options
11+
cur_word="\${COMP_WORDS[COMP_CWORD]}"
12+
if [[ "\${cur_word}" == -* ]] ; then
13+
COMPREPLY=( $(compgen -W '${all_opts.join(' ')}' -- "\${cur_word}") )
14+
return 0
15+
else
16+
COMPREPLY=( $(compgen -f "\${cur_word}") )
17+
return 0
18+
fi
19+
}
20+
complete -F _node_complete node node_g`);
21+
}
22+
23+
module.exports = {
24+
print
25+
};

lib/internal/bootstrap/node.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,17 @@
120120
NativeModule.require('internal/inspector_async_hook').setup();
121121
}
122122

123-
if (internalBinding('options').getOptions('--help')) {
123+
const options = internalBinding('options');
124+
if (options.getOptions('--help')) {
124125
NativeModule.require('internal/print_help').print(process.stdout);
125126
return;
126127
}
127128

129+
if (options.getOptions('--completion-bash')) {
130+
NativeModule.require('internal/bash_completion').print(process.stdout);
131+
return;
132+
}
133+
128134
if (isMainThread) {
129135
mainThreadSetup.setupChildProcessIpcChannel();
130136
}

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
'lib/zlib.js',
8686
'lib/internal/assert.js',
8787
'lib/internal/async_hooks.js',
88+
'lib/internal/bash_completion.js',
8889
'lib/internal/buffer.js',
8990
'lib/internal/cli_table.js',
9091
'lib/internal/child_process.js',

src/node_options.cc

+3
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ PerProcessOptionsParser::PerProcessOptionsParser() {
223223
kAllowedInEnvironment);
224224

225225
AddOption("--security-reverts", "", &PerProcessOptions::security_reverts);
226+
AddOption("--completion-bash",
227+
"print source-able bash completion script",
228+
&PerProcessOptions::print_bash_completion);
226229
AddOption("--help",
227230
"print node command line options",
228231
&PerProcessOptions::print_help);

src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class PerProcessOptions {
112112
bool zero_fill_all_buffers = false;
113113

114114
std::vector<std::string> security_reverts;
115+
bool print_bash_completion = false;
115116
bool print_help = false;
116117
bool print_v8_help = false;
117118
bool print_version = false;

0 commit comments

Comments
 (0)