Skip to content

Commit 39785c7

Browse files
sam-githubjasnell
authored andcommitted
inspector: document bad usage for --inspect-port
Document --inspect-port, and fix the reporting for when it is misused. The option requires an argument, but when the argument was omitted, the error message incorrectly reported --inspect-port as being bad, as if was not supported at all: % node --inspect-port node: bad option: --inspect-port % node --none-such node: bad option: --none-such It is now correctly reported as requiring an argument: % ./node --inspect-port ./node: --inspect-port requires an argument PR-URL: #12581 Reviewed-By: James M Snell <[email protected]>
1 parent d4e9e0f commit 39785c7

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

doc/api/cli.md

+12
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ added: v7.6.0
112112
-->
113113

114114
Activate inspector on host:port and break at start of user script.
115+
Default host:port is 127.0.0.1:9229.
116+
117+
118+
### `--inspect-port=[host:]port`
119+
<!-- YAML
120+
added: v7.6.0
121+
-->
122+
123+
Set the host:port to be used when the inspector is activated.
124+
Useful when activating the inspector by sending the `SIGUSR1` signal.
125+
126+
Default host is 127.0.0.1.
115127

116128

117129
### `--no-deprecation`

doc/node.1

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ instances for debugging and profiling. It uses the Chrome Debugging Protocol.
106106
.BR \-\-inspect-brk \fI[=[host:]port]\fR
107107
Activate inspector on host:port and break at start of user script.
108108

109+
.TP
110+
.BR \-\-inspect-port \fI=[host:]port\fR
111+
Set the host:port to be used when the inspector is activated.
112+
109113
.TP
110114
.BR \-\-no\-deprecation
111115
Silence deprecation warnings.

src/node.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -3612,6 +3612,8 @@ static void PrintHelp() {
36123612
" --inspect-brk[=[host:]port]\n"
36133613
" activate inspector on host:port\n"
36143614
" and break at start of user script\n"
3615+
" --inspect-port=[host:]port\n"
3616+
" set host:port for inspector\n"
36153617
#endif
36163618
" --no-deprecation silence deprecation warnings\n"
36173619
" --trace-deprecation show stack traces on deprecations\n"
@@ -3798,7 +3800,7 @@ static void ParseArgs(int* argc,
37983800

37993801
CheckIfAllowedInEnv(argv[0], is_env, arg);
38003802

3801-
if (debug_options.ParseOption(arg)) {
3803+
if (debug_options.ParseOption(argv[0], arg)) {
38023804
// Done, consumed by DebugOptions::ParseOption().
38033805
} else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
38043806
printf("%s\n", NODE_VERSION);

src/node_debug_options.cc

+14-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ DebugOptions::DebugOptions() :
6262
wait_connect_(false), http_enabled_(false),
6363
host_name_("127.0.0.1"), port_(-1) { }
6464

65-
bool DebugOptions::ParseOption(const std::string& option) {
65+
bool DebugOptions::ParseOption(const char* argv0, const std::string& option) {
6666
bool enable_inspector = false;
6767
bool has_argument = false;
6868
std::string option_name;
@@ -72,21 +72,28 @@ bool DebugOptions::ParseOption(const std::string& option) {
7272
if (pos == std::string::npos) {
7373
option_name = option;
7474
} else {
75-
has_argument = true;
7675
option_name = option.substr(0, pos);
7776
argument = option.substr(pos + 1);
77+
78+
if (argument.length() > 0)
79+
has_argument = true;
80+
else
81+
argument.clear();
7882
}
7983

8084
if (option_name == "--inspect") {
8185
enable_inspector = true;
8286
} else if (option_name == "--inspect-brk") {
8387
enable_inspector = true;
8488
wait_connect_ = true;
85-
} else if ((option_name != "--debug-port" &&
86-
option_name != "--inspect-port") ||
87-
!has_argument) {
88-
// only other valid possibility is --inspect-port,
89-
// which requires an argument
89+
} else if (option_name == "--debug-port" ||
90+
option_name == "--inspect-port") {
91+
if (!has_argument) {
92+
fprintf(stderr, "%s: %s requires an argument\n",
93+
argv0, option.c_str());
94+
exit(9);
95+
}
96+
} else {
9097
return false;
9198
}
9299

src/node_debug_options.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace node {
99
class DebugOptions {
1010
public:
1111
DebugOptions();
12-
bool ParseOption(const std::string& option);
12+
bool ParseOption(const char* argv0, const std::string& option);
1313
bool inspector_enabled() const {
1414
#if HAVE_INSPECTOR
1515
return inspector_enabled_;

test/parallel/test-cli-bad-options.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
require('../common');
3+
4+
// Tests that node exits consistently on bad option syntax.
5+
6+
const assert = require('assert');
7+
const spawn = require('child_process').spawnSync;
8+
9+
requiresArgument('--inspect-port');
10+
requiresArgument('--inspect-port=');
11+
requiresArgument('--debug-port');
12+
requiresArgument('--debug-port=');
13+
requiresArgument('--eval');
14+
15+
function requiresArgument(option) {
16+
const r = spawn(process.execPath, [option], {encoding: 'utf8'});
17+
18+
assert.strictEqual(r.status, 9);
19+
20+
const msg = r.stderr.split(/\r?\n/)[0];
21+
assert.strictEqual(msg, process.execPath + ': ' + option +
22+
' requires an argument');
23+
}

0 commit comments

Comments
 (0)