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

tools: add bash completion for node #20713

Closed
wants to merge 1 commit into from

Conversation

danbev
Copy link
Contributor

@danbev danbev commented May 14, 2018

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]  # (press the 'tab' key)
--abort_on_uncaught_exception  --inspect-brk-node=            --require
--check                        --inspect-brk=                 --security-reverts
--completion-bash              --inspect-port                 --stack_trace_limit
--debug                        --inspect=                     --throw-deprecation
--debug-brk                    --interactive                  --title
--debug-brk=                   --loader                       --tls-cipher-list
--debug-port                   --max_old_space_size           --trace-deprecation
--debug=                       --napi-modules                 --trace-event-categories
--eval                         --no-deprecation               --trace-event-file-pattern
--experimental-modules         --no-force-async-hooks-checks  --trace-events-enabled
--experimental-repl-await      --no-warnings                  --trace-sync-io
--experimental-vm-modules      --openssl-config               --trace-warnings
--experimental-worker          --pending-deprecation          --track-heap-objects
--expose-internals             --perf_basic_prof              --use-bundled-ca
--expose_internals             --perf_prof                    --use-openssl-ca
--help                         --preserve-symlinks            --v8-options
--icu-data-dir                 --preserve-symlinks-main       --v8-pool-size
--inspect                      --print                        --version
--inspect-brk                  --prof-process                 --zero-fill-buffers
--inspect-brk-node             --redirect-warnings
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the tools Issues and PRs related to the tools directory. label May 14, 2018
@danbev
Copy link
Contributor Author

danbev commented May 14, 2018

@mscdex
Copy link
Contributor

mscdex commented May 14, 2018

Isn't that output missing some options, like --v8-options and --v8-pool-size=num?

@danbev
Copy link
Contributor Author

danbev commented May 14, 2018

Isn't that output missing some options, like --v8-options and --v8-pool-size=num?

Oh indeed, I missed that. I'll take a look. Thanks

@danbev
Copy link
Contributor Author

danbev commented May 14, 2018

With the latest update output looks like this:

$ ./node --
--                             --no-deprecation               --trace-deprecation
--abort-on-uncaught-exception  --no-force-async-hooks-checks  --trace-event-categories
--check                        --no-warnings                  --trace-event-file-pattern
--eval                         --openssl-config=file          --trace-events-enabled
--experimental-modules         --pending-deprecation          --trace-sync-io
--experimental-repl-await      --preserve-symlinks            --trace-warnings
--experimental-vm-modules      --preserve-symlinks-main       --track-heap-objects
--help                         --print                        --use-bundled-ca
--icu-data-dir=dir             --prof                         --use-openssl-ca
--inspect-brk[=[host:]port]    --prof-process                 --v8-options
--inspect-port=[host:]port     --redirect-warnings=file       --v8-pool-size=num
--inspect[=[host:]port]        --require                      --version
--interactive                  --throw-deprecation            --zero-fill-buffers
--napi-modules                 --tls-cipher-list=val

@addaleax
Copy link
Member

I know some UNIX CLI tools ship something like tool --completion baked into the executable, which provides a source-able bash script as the output. Would that make sense?

@danbev
Copy link
Contributor Author

danbev commented May 14, 2018

Would that make sense?

That sounds interesting for sure!

@BridgeAR
Copy link
Member

@danbev is this actually work in progress or ready to review?

@danbev
Copy link
Contributor Author

danbev commented May 30, 2018

@danbev is this actually work in progress or ready to review?

I'll mark it as in progress and follow up later this week. Thanks

@danbev danbev added the wip Issues and PRs that are still a work in progress. label May 30, 2018
@maclover7
Copy link
Contributor

ping @danbev

@danbev
Copy link
Contributor Author

danbev commented Aug 15, 2018

I know some UNIX CLI tools ship something like tool --completion baked into the executable, which provides a source-able bash script as the output.

Sorry about the delay on this. Just to clarify, your suggestion is that we provide an option to node which returns the source-able bash script? Something like:

$ node --completion

@danbev danbev removed the wip Issues and PRs that are still a work in progress. label Aug 16, 2018
@danbev
Copy link
Contributor Author

danbev commented Aug 20, 2018

@danbev danbev force-pushed the node_bash_completion branch 2 times, most recently from ee942ca to b20443b Compare August 24, 2018 07:07
@danbev
Copy link
Contributor Author

danbev commented Aug 24, 2018

@danbev
Copy link
Contributor Author

danbev commented Aug 24, 2018

@addaleax Would you be able to take a look at this once more when you get a chance, and see if this is what you had in mind regarding your suggestion of using the --completion option?

src/node.cc Outdated
"{\n"
" local cur_word options\n"
" cur_word=\"${COMP_WORDS[COMP_CWORD]}\"\n"
" options=\"`$1 --help`\"\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

How robust is this? That is how much can we change the output of node --help before this breaks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps @addaleax suggestion about #22490 (which I've not looked into yet) might help making this more robust. I'll take a closer look next week.

@refack
Copy link
Contributor

refack commented Aug 24, 2018

I'm tending toward -0 on this, as is. Since the real "magic" is baked into the --help logic, this seems like a too specific change for a specific target (IIUC just bash), that could be replaced with a simple independant snippet:

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

src/node.cc Outdated
" return 0\n"
" fi\n"
"}\n"
"complete -F _node_complete node node_g\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

can't we replace node node_g with argv[0]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll take a look. Thanks

@addaleax
Copy link
Member

@danbev Just to make sure, have you seen #22490? I don’t know whether this would help make this more robust or not, but it seems like it might.

@danbev
Copy link
Contributor Author

danbev commented Aug 24, 2018

Just to make sure, have you seen #22490?

Thanks I was not aware of it. I'll take a look closer look at it next week. Thanks

@danbev
Copy link
Contributor Author

danbev commented Aug 24, 2018

Since the real "magic" is baked into the --help logic, this seems like a too specific change for a specific target (IIUC just bash), that could be replaced with a simple independant snippet:

This was what the original commit did, but was changed after the suggestion in #20713 (comment).

@danbev
Copy link
Contributor Author

danbev commented Sep 5, 2018

doc/api/cli.md Outdated
added: REPLACEME
-->

Output source-able bash completion script for node.
Copy link
Member

Choose a reason for hiding this comment

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

A more complete example of how to use this would be good here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add an example. Thanks

Copy link
Member

Choose a reason for hiding this comment

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

Maybe reword this to:

Print source-able bash completion script for Node.js.?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, I'll update. Sorry for the late reply by the way, I just saw this comment.

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]
@danbev
Copy link
Contributor Author

danbev commented Sep 20, 2018

@richardlau
Copy link
Member

linuxone failures were a job configuration issue that is now fixed: nodejs/build#1501

@danbev
Copy link
Contributor Author

danbev commented Sep 20, 2018

@richardlau Thanks!

@danbev
Copy link
Contributor Author

danbev commented Sep 21, 2018

Re-build of failing node-test-commit-linuxone was successful.

@danbev
Copy link
Contributor Author

danbev commented Sep 21, 2018

Landed in 56493bf.

@danbev danbev closed this Sep 21, 2018
@danbev danbev deleted the node_bash_completion branch September 21, 2018 05:26
danbev added a commit that referenced this pull request Sep 21, 2018
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]>
targos pushed a commit that referenced this pull request Sep 21, 2018
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]>
targos added a commit that referenced this pull request Oct 7, 2018
Notable changes:

* assert
  * The diff output is now a tiny bit improved by sorting object
    properties when inspecting the values that are compared with each
    other. #22788
* cli
  * The options parser now normalizes `_` to `-` in all multi-word
    command-line flags, e.g. `--no_warnings` has the same effect as
    `--no-warnings`. #23020
  * Added bash completion for the `node` binary. To generate a bash
    completion script, run `node --completion-bash`. The output can be
    saved to a file which can be sourced to enable completion.
    #20713
* crypto
  * Added support for PEM-level encryption.
    #23151
  * Added an API asymmetric key pair generation. The new methods
    `crypto.generateKeyPair` and `crypto.generateKeyPairSync` can be
    used to generate public and private key pairs. The API supports
    RSA, DSA and EC and a variety of key encodings (both PEM and DER).
    #22660
* fs
  * Added a `recursive` option to `fs.mkdir` and `fs.mkdirSync`. If
    this option is set to true, non-existing parent folders will be
    automatically created. #21875
* http2
  * Added a `'ping'` event to `Http2Session` that is emitted whenever a
    non-ack `PING` is received.
    #23009
  * Added support for the `ORIGIN` frame.
    #22956
* module
  * Added `module.createRequireFromPath(filename)`. This new method can
    be used to create a custom require function that will resolve
    modules relative to the filename path.
    #19360
* process
  * Added a `'multipleResolves'` process event that is emitted whenever
    a `Promise` is attempted to be resolved multiple times, e.g. if the
    `resolve` and `reject` functions are both called in a `Promise`
    executor. #22218
* **url**
  * Added `url.fileURLToPath(url)` and `url.pathToFileURL(path)`. These
    methods can be used to correctly convert between file: URLs and
    absolute paths. #22506
* **util**
  * Added the `sorted` option to `util.inspect()`. If set to `true`,
    all properties of an object and Set and Map entries will be sorted
    in the returned string. If set to a function, it is used as a
    compare function. #22788
  * The `util.instpect.custom` symbol is now defined in the global
    symbol registry as `Symbol.for('nodejs.util.inspect.custom')`.
    #20857
* **Windows**
  * The Windows msi installer now provides an option to automatically
    install the tools required to build native modules.
    #22645
* **Added new collaborators**:
  * digitalinfinity - Hitesh Kanwathirtha

PR-URL: #23313
targos added a commit that referenced this pull request Oct 10, 2018
Notable changes:

* assert
  * The diff output is now a tiny bit improved by sorting object
    properties when inspecting the values that are compared with each
    other. #22788
* cli
  * The options parser now normalizes `_` to `-` in all multi-word
    command-line flags, e.g. `--no_warnings` has the same effect as
    `--no-warnings`. #23020
  * Added bash completion for the `node` binary. To generate a bash
    completion script, run `node --completion-bash`. The output can be
    saved to a file which can be sourced to enable completion.
    #20713
* crypto
  * Added support for PEM-level encryption.
    #23151
  * Added an API asymmetric key pair generation. The new methods
    `crypto.generateKeyPair` and `crypto.generateKeyPairSync` can be
    used to generate public and private key pairs. The API supports
    RSA, DSA and EC and a variety of key encodings (both PEM and DER).
    #22660
* fs
  * Added a `recursive` option to `fs.mkdir` and `fs.mkdirSync`. If
    this option is set to true, non-existing parent folders will be
    automatically created. #21875
* http2
  * Added a `'ping'` event to `Http2Session` that is emitted whenever a
    non-ack `PING` is received.
    #23009
  * Added support for the `ORIGIN` frame.
    #22956
  * Updated nghttp2 to 1.34.0. This adds RFC 8441 extended connect
    protocol support to allow use of WebSockets over HTTP/2.
    #23284
* module
  * Added `module.createRequireFromPath(filename)`. This new method can
    be used to create a custom require function that will resolve
    modules relative to the filename path.
    #19360
* process
  * Added a `'multipleResolves'` process event that is emitted whenever
    a `Promise` is attempted to be resolved multiple times, e.g. if the
    `resolve` and `reject` functions are both called in a `Promise`
    executor. #22218
* url
  * Added `url.fileURLToPath(url)` and `url.pathToFileURL(path)`. These
    methods can be used to correctly convert between file: URLs and
    absolute paths. #22506
* util
  * Added the `sorted` option to `util.inspect()`. If set to `true`,
    all properties of an object and Set and Map entries will be sorted
    in the returned string. If set to a function, it is used as a
    compare function. #22788
  * The `util.instpect.custom` symbol is now defined in the global
    symbol registry as `Symbol.for('nodejs.util.inspect.custom')`.
    #20857
  * Added support for `BigInt` numbers in `util.format()`.
    #22097
* V8 API
  * A number of V8 C++ APIs have been marked as deprecated since they
    have been removed in the upstream repository. Replacement APIs
    are added where necessary. #23159
* Windows
  * The Windows msi installer now provides an option to automatically
    install the tools required to build native modules.
    #22645
* Workers
  * Debugging support for Workers using the DevTools protocol has been
    implemented. #21364
  * The public `inspector` module is now enabled in Workers.
    #22769
* Added new collaborators:
  * digitalinfinity - Hitesh Kanwathirtha

PR-URL: #23313
targos added a commit that referenced this pull request Oct 10, 2018
Notable changes:

* assert
  * The diff output is now a tiny bit improved by sorting object
    properties when inspecting the values that are compared with each
    other. #22788
* cli
  * The options parser now normalizes `_` to `-` in all multi-word
    command-line flags, e.g. `--no_warnings` has the same effect as
    `--no-warnings`. #23020
  * Added bash completion for the `node` binary. To generate a bash
    completion script, run `node --completion-bash`. The output can be
    saved to a file which can be sourced to enable completion.
    #20713
* crypto
  * Added support for PEM-level encryption.
    #23151
  * Added an API asymmetric key pair generation. The new methods
    `crypto.generateKeyPair` and `crypto.generateKeyPairSync` can be
    used to generate public and private key pairs. The API supports
    RSA, DSA and EC and a variety of key encodings (both PEM and DER).
    #22660
* fs
  * Added a `recursive` option to `fs.mkdir` and `fs.mkdirSync`. If
    this option is set to true, non-existing parent folders will be
    automatically created. #21875
* http2
  * Added a `'ping'` event to `Http2Session` that is emitted whenever a
    non-ack `PING` is received.
    #23009
  * Added support for the `ORIGIN` frame.
    #22956
  * Updated nghttp2 to 1.34.0. This adds RFC 8441 extended connect
    protocol support to allow use of WebSockets over HTTP/2.
    #23284
* module
  * Added `module.createRequireFromPath(filename)`. This new method can
    be used to create a custom require function that will resolve
    modules relative to the filename path.
    #19360
* process
  * Added a `'multipleResolves'` process event that is emitted whenever
    a `Promise` is attempted to be resolved multiple times, e.g. if the
    `resolve` and `reject` functions are both called in a `Promise`
    executor. #22218
* url
  * Added `url.fileURLToPath(url)` and `url.pathToFileURL(path)`. These
    methods can be used to correctly convert between file: URLs and
    absolute paths. #22506
* util
  * Added the `sorted` option to `util.inspect()`. If set to `true`,
    all properties of an object and Set and Map entries will be sorted
    in the returned string. If set to a function, it is used as a
    compare function. #22788
  * The `util.instpect.custom` symbol is now defined in the global
    symbol registry as `Symbol.for('nodejs.util.inspect.custom')`.
    #20857
  * Added support for `BigInt` numbers in `util.format()`.
    #22097
* V8 API
  * A number of V8 C++ APIs have been marked as deprecated since they
    have been removed in the upstream repository. Replacement APIs
    are added where necessary. #23159
* Windows
  * The Windows msi installer now provides an option to automatically
    install the tools required to build native modules.
    #22645
* Workers
  * Debugging support for Workers using the DevTools protocol has been
    implemented. #21364
  * The public `inspector` module is now enabled in Workers.
    #22769
* Added new collaborators:
  * digitalinfinity - Hitesh Kanwathirtha

PR-URL: #23313
targos added a commit that referenced this pull request Oct 10, 2018
Notable changes:

* assert
  * The diff output is now a tiny bit improved by sorting object
    properties when inspecting the values that are compared with each
    other. #22788
* cli
  * The options parser now normalizes `_` to `-` in all multi-word
    command-line flags, e.g. `--no_warnings` has the same effect as
    `--no-warnings`. #23020
  * Added bash completion for the `node` binary. To generate a bash
    completion script, run `node --completion-bash`. The output can be
    saved to a file which can be sourced to enable completion.
    #20713
* crypto
  * Added support for PEM-level encryption.
    #23151
  * Added an API asymmetric key pair generation. The new methods
    `crypto.generateKeyPair` and `crypto.generateKeyPairSync` can be
    used to generate public and private key pairs. The API supports
    RSA, DSA and EC and a variety of key encodings (both PEM and DER).
    #22660
* fs
  * Added a `recursive` option to `fs.mkdir` and `fs.mkdirSync`. If
    this option is set to true, non-existing parent folders will be
    automatically created. #21875
* http2
  * Added a `'ping'` event to `Http2Session` that is emitted whenever a
    non-ack `PING` is received.
    #23009
  * Added support for the `ORIGIN` frame.
    #22956
  * Updated nghttp2 to 1.34.0. This adds RFC 8441 extended connect
    protocol support to allow use of WebSockets over HTTP/2.
    #23284
* module
  * Added `module.createRequireFromPath(filename)`. This new method can
    be used to create a custom require function that will resolve
    modules relative to the filename path.
    #19360
* process
  * Added a `'multipleResolves'` process event that is emitted whenever
    a `Promise` is attempted to be resolved multiple times, e.g. if the
    `resolve` and `reject` functions are both called in a `Promise`
    executor. #22218
* url
  * Added `url.fileURLToPath(url)` and `url.pathToFileURL(path)`. These
    methods can be used to correctly convert between file: URLs and
    absolute paths. #22506
* util
  * Added the `sorted` option to `util.inspect()`. If set to `true`,
    all properties of an object and Set and Map entries will be sorted
    in the returned string. If set to a function, it is used as a
    compare function. #22788
  * The `util.instpect.custom` symbol is now defined in the global
    symbol registry as `Symbol.for('nodejs.util.inspect.custom')`.
    #20857
  * Added support for `BigInt` numbers in `util.format()`.
    #22097
* V8 API
  * A number of V8 C++ APIs have been marked as deprecated since they
    have been removed in the upstream repository. Replacement APIs
    are added where necessary. #23159
* Windows
  * The Windows msi installer now provides an option to automatically
    install the tools required to build native modules.
    #22645
* Workers
  * Debugging support for Workers using the DevTools protocol has been
    implemented. #21364
  * The public `inspector` module is now enabled in Workers.
    #22769
* Added new collaborators:
  * digitalinfinity - Hitesh Kanwathirtha

PR-URL: #23313
jasnell pushed a commit that referenced this pull request Oct 17, 2018
Notable changes:

* assert
  * The diff output is now a tiny bit improved by sorting object
    properties when inspecting the values that are compared with each
    other. #22788
* cli
  * The options parser now normalizes `_` to `-` in all multi-word
    command-line flags, e.g. `--no_warnings` has the same effect as
    `--no-warnings`. #23020
  * Added bash completion for the `node` binary. To generate a bash
    completion script, run `node --completion-bash`. The output can be
    saved to a file which can be sourced to enable completion.
    #20713
* crypto
  * Added support for PEM-level encryption.
    #23151
  * Added an API asymmetric key pair generation. The new methods
    `crypto.generateKeyPair` and `crypto.generateKeyPairSync` can be
    used to generate public and private key pairs. The API supports
    RSA, DSA and EC and a variety of key encodings (both PEM and DER).
    #22660
* fs
  * Added a `recursive` option to `fs.mkdir` and `fs.mkdirSync`. If
    this option is set to true, non-existing parent folders will be
    automatically created. #21875
* http2
  * Added a `'ping'` event to `Http2Session` that is emitted whenever a
    non-ack `PING` is received.
    #23009
  * Added support for the `ORIGIN` frame.
    #22956
  * Updated nghttp2 to 1.34.0. This adds RFC 8441 extended connect
    protocol support to allow use of WebSockets over HTTP/2.
    #23284
* module
  * Added `module.createRequireFromPath(filename)`. This new method can
    be used to create a custom require function that will resolve
    modules relative to the filename path.
    #19360
* process
  * Added a `'multipleResolves'` process event that is emitted whenever
    a `Promise` is attempted to be resolved multiple times, e.g. if the
    `resolve` and `reject` functions are both called in a `Promise`
    executor. #22218
* url
  * Added `url.fileURLToPath(url)` and `url.pathToFileURL(path)`. These
    methods can be used to correctly convert between file: URLs and
    absolute paths. #22506
* util
  * Added the `sorted` option to `util.inspect()`. If set to `true`,
    all properties of an object and Set and Map entries will be sorted
    in the returned string. If set to a function, it is used as a
    compare function. #22788
  * The `util.instpect.custom` symbol is now defined in the global
    symbol registry as `Symbol.for('nodejs.util.inspect.custom')`.
    #20857
  * Added support for `BigInt` numbers in `util.format()`.
    #22097
* V8 API
  * A number of V8 C++ APIs have been marked as deprecated since they
    have been removed in the upstream repository. Replacement APIs
    are added where necessary. #23159
* Windows
  * The Windows msi installer now provides an option to automatically
    install the tools required to build native modules.
    #22645
* Workers
  * Debugging support for Workers using the DevTools protocol has been
    implemented. #21364
  * The public `inspector` module is now enabled in Workers.
    #22769
* Added new collaborators:
  * digitalinfinity - Hitesh Kanwathirtha

PR-URL: #23313
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. semver-minor PRs that contain new features and should be released in the next minor version. tools Issues and PRs related to the tools directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants