Add save/restore state to allow multiple calls to parse #2299
+295
−2
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