-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add save/restore state to allow multiple calls to parse #2299
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
Merged
shadowspawn
merged 3 commits into
tj:release/13.x
from
shadowspawn:feature/multiple-parse
Dec 27, 2024
Merged
Add save/restore state to allow multiple calls to parse #2299
shadowspawn
merged 3 commits into
tj:release/13.x
from
shadowspawn:feature/multiple-parse
Dec 27, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Comments welcome on routine naming or more interesting cases to add to unit tests. |
This was referenced Dec 21, 2024
abetomo
approved these changes
Dec 25, 2024
Released in Commander v13.0.0 |
jelmore1674
pushed a commit
to jelmore1674/build-changelog
that referenced
this pull request
Mar 19, 2025
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [commander](https://github.com/tj/commander.js) | devDependencies | major | [`12.1.0` -> `13.0.0`](https://renovatebot.com/diffs/npm/commander/12.1.0/13.0.0) | --- ### Release Notes <details> <summary>tj/commander.js (commander)</summary> ### [`v13.0.0`](https://github.com/tj/commander.js/blob/HEAD/CHANGELOG.md#1300-2024-12-30) [Compare Source](tj/commander.js@v12.1.0...v13.0.0) ##### Added - support multiple calls to `.parse()` with default settings (\[[#​2299](tj/commander.js#2299)]) - add `.saveStateBeforeParse()` and `.restoreStateBeforeParse()` for use by subclasses (\[[#​2299](tj/commander.js#2299)]) - style routines like `styleTitle()` to add color to help using `.configureHelp()` or Help subclass (\[[#​2251](tj/commander.js#2251)]) - color related support in `.configureOutput()` for `getOutHasColors()`, `getErrHasColors()`, and `stripColor()` (\[[#​2251](tj/commander.js#2251)]) - Help property for `minWidthToWrap` (\[[#​2251](tj/commander.js#2251)]) - Help methods for `displayWidth()`, `boxWrap()`, `preformatted()` et al (\[[#​2251](tj/commander.js#2251)]) ##### Changed - *Breaking*: excess command-arguments cause an error by default, see migration tips (\[[#​2223](tj/commander.js#2223)]) - *Breaking*: throw during Option construction for unsupported option flags, like multiple characters after single `-` (\[[#​2270](tj/commander.js#2270)]) - *Breaking*: throw on multiple calls to `.parse()` if `storeOptionsAsProperties: true` (\[[#​2299](tj/commander.js#2299)]) - TypeScript: include implicit `this` in parameters for action handler callback (\[[#​2197](tj/commander.js#2197)]) ##### Deleted - *Breaking*: `Help.wrap()` refactored into `formatItem()` and `boxWrap()` (\[[#​2251](tj/commander.js#2251)]) ##### Migration Tips **Excess command-arguments** It is now an error for the user to specify more command-arguments than are expected. (`allowExcessArguments` is now false by default.) Old code: ```js program.option('-p, --port <number>', 'port number'); program.action((options) => { console.log(program.args); }); ``` Now shows an error: ```console $ node example.js a b c error: too many arguments. Expected 0 arguments but got 3. ``` You can declare the expected arguments. The help will then be more accurate too. Note that declaring new arguments will change what is passed to the action handler. ```js program.option('-p, --port <number>', 'port number'); program.argument('[args...]', 'remote command and arguments'); // expecting zero or more arguments program.action((args, options) => { console.log(args); }); ``` Or you could suppress the error, useful for minimising changes in legacy code. ```js program.option('-p, --port', 'port number'); program.allowExcessArguments(); program.action((options) => { console.log(program.args); }); ``` </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS44NC4wIiwidXBkYXRlZEluVmVyIjoiMzkuODQuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==--> Reviewed-on: https://git.justinelmore.dev/jelmore1674/build-changelog/pulls/38 Reviewed-by: Justin Elmore <[email protected]> Co-authored-by: Renovate Bot <[email protected]> Co-committed-by: Renovate Bot <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request
Problem
People may not realise parse should only be called once, and discover by trial and error.
See: #438 #1565 #1819 #1829 #1916 #2036 #2246 #2247 #2263
Solution
I thought it was going to be hard and messy to save/restore state and not worth it, but turned out fairly straightforward! Fingers crossed.
When
storeOptionsAsProperties: false
allow multiple calls to parse by storing and resetting the state. This is done in a lazy way, so a wide or deep hierarchy of subcommands does not need to save state on whole tree.When
storeOptionsAsProperties: true
then throw on the second call to parse.Not planning to support multiple calls to
.parseOptions()
as little used for direct calls, but could do that by moving the implementation into a private routine and call._prepareForParse()
in public routine.ChangeLog
.parse()
with default settings.parse()
ifstoreOptionsAsProperties: true
.saveStateBeforeParse()
and.restoreStateBeforeParse()
for use by subclasses