Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: port --bash-completion to C++ #25901

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 0 additions & 29 deletions lib/internal/main/print_bash_completion.js

This file was deleted.

1 change: 0 additions & 1 deletion node.gyp
Expand Up @@ -138,7 +138,6 @@
'lib/internal/main/eval_string.js',
'lib/internal/main/eval_stdin.js',
'lib/internal/main/inspect.js',
'lib/internal/main/print_bash_completion.js',
'lib/internal/main/print_help.js',
'lib/internal/main/prof_process.js',
'lib/internal/main/repl.js',
Expand Down
15 changes: 12 additions & 3 deletions src/node.cc
Expand Up @@ -396,9 +396,6 @@ MaybeLocal<Value> StartMainThreadExecution(Environment* env) {
return StartExecution(env, "internal/main/print_help");
}

if (per_process::cli_options->print_bash_completion) {
return StartExecution(env, "internal/main/print_bash_completion");
}

if (env->options()->prof_process) {
return StartExecution(env, "internal/main/prof_process");
Expand Down Expand Up @@ -771,6 +768,12 @@ void Init(int* argc,
exit(0);
}

if (per_process::cli_options->print_bash_completion) {
std::string completion = options_parser::GetBashCompletion();
printf("%s\n", completion.c_str());
exit(0);
}

if (per_process::cli_options->print_v8_help) {
V8::SetFlagsFromString("--help", 6); // Doesn't return.
UNREACHABLE();
Expand Down Expand Up @@ -829,6 +832,12 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
return result;
}

if (per_process::cli_options->print_bash_completion) {
std::string completion = options_parser::GetBashCompletion();
printf("%s\n", completion.c_str());
exit(0);
}

if (per_process::cli_options->print_v8_help) {
V8::SetFlagsFromString("--help", 6); // Doesn't return.
UNREACHABLE();
Expand Down
39 changes: 39 additions & 0 deletions src/node_options.cc
Expand Up @@ -4,6 +4,8 @@
#include "env-inl.h"
#include "node_binding.h"

#include <errno.h>
#include <sstream>
#include <cstdlib> // strtoul, errno

using v8::Boolean;
Expand Down Expand Up @@ -678,6 +680,43 @@ HostPort SplitHostPort(const std::string& arg,
ParseAndValidatePort(arg.substr(colon + 1), errors) };
}

std::string GetBashCompletion() {
Mutex::ScopedLock lock(per_process::cli_options_mutex);
const auto& parser = _ppop_instance;

std::ostringstream out;

out << "_node_complete() {\n"
" local cur_word options\n"
" cur_word=\"${COMP_WORDS[COMP_CWORD]}\"\n"
" if [[ \"${cur_word}\" == -* ]] ; then\n"
" COMPREPLY=( $(compgen -W '";

for (const auto& item : parser.options_) {
if (item.first[0] != '[') {
out << item.first << " ";
}
}
for (const auto& item : parser.aliases_) {
if (item.first[0] != '[') {
out << item.first << " ";
}
}
if (parser.aliases_.size() > 0) {
out.seekp(-1, out.cur); // Strip the trailing space
}

out << "' -- \"${cur_word}\") )\n"
" return 0\n"
" else\n"
" COMPREPLY=( $(compgen -f \"${cur_word}\") )\n"
" return 0\n"
" fi\n"
"}\n"
"complete -F _node_complete node node_g";
return out.str();
}

// Return a map containing all the options and their metadata as well
// as the aliases
void GetOptions(const FunctionCallbackInfo<Value>& args) {
Expand Down
2 changes: 2 additions & 0 deletions src/node_options.h
Expand Up @@ -224,6 +224,7 @@ namespace options_parser {
HostPort SplitHostPort(const std::string& arg,
std::vector<std::string>* errors);
void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
std::string GetBashCompletion();

enum OptionEnvvarSettings {
kAllowedInEnvironment,
Expand Down Expand Up @@ -412,6 +413,7 @@ class OptionsParser {
friend class OptionsParser;

friend void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
friend std::string GetBashCompletion();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be a member of OptionsParser, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can, but so is GetOptions() so I figured it's better to keep them consistent

};

using StringVector = std::vector<std::string>;
Expand Down
22 changes: 16 additions & 6 deletions test/parallel/test-bash-completion.js
Expand Up @@ -2,22 +2,32 @@
require('../common');
const assert = require('assert');
const child_process = require('child_process');
const { inspect } = require('util');

const p = child_process.spawnSync(
process.execPath, [ '--completion-bash' ]);
assert.ifError(p.error);
assert.ok(p.stdout.toString().includes(
`_node_complete() {

const output = p.stdout.toString().trim().replace(/\r/g, '');
console.log(output);

const prefix = `_node_complete() {
local cur_word options
cur_word="\${COMP_WORDS[COMP_CWORD]}"
if [[ "\${cur_word}" == -* ]] ; then
COMPREPLY=( $(compgen -W '`));
assert.ok(p.stdout.toString().includes(
`' -- "\${cur_word}") )
COMPREPLY=( $(compgen -W '`.replace(/\r/g, '');
const suffix = `' -- "\${cur_word}") )
return 0
else
COMPREPLY=( $(compgen -f "\${cur_word}") )
return 0
fi
}
complete -F _node_complete node node_g`));
complete -F _node_complete node node_g`.replace(/\r/g, '');

assert.ok(
output.includes(prefix),
`Expect\n\n ${inspect(output)}\n\nto include\n\n${inspect(prefix)}`);
assert.ok(
output.includes(suffix),
`Expect\n\n ${inspect(output)}\n\nto include\n\n${inspect(suffix)}`);