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: https://github.com/nodejs/node/pull/20713
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
This commit is contained in:
Daniel Bevenius 2018-05-14 13:09:27 +02:00
parent 408e8ce22c
commit 56493bf1eb
7 changed files with 51 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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
};

View File

@ -111,11 +111,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();
}

View File

@ -84,6 +84,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',

View File

@ -221,6 +221,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);

View File

@ -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;