parseAllAsRoot() for list of parsed commands #797
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.
Description
It can be useful to get the list of parseable commands from the root
to the last subcommand in order to trace the path taken through the
command decision tree, and access parent command states from a
subcommand.
Here is an example of two paths through a decision tree encoded
as swift argument parser commands, subcommands and options.
In both leaf commands (decision2 and decision2A) they want to
know two things. First, is the fact that decision1 was chosen. It's
possible that subcommands can be reused at different points in a
decision tree. That choice can affect the behaviour of the
subcommand when it executes.
Second, the subcommands might further alter their behaviour
in the presence of flags/options, such as opt1 in this example.
Option groups are the usual method to propagate options down
to subcommands, but they have some limitations. The options
become present in the subcommands, and often visible with the
online help. This causes bloat in the subcommands if they need
to be aware of parent options. Also, they only apply to options
(or flags). There is no way to identify the parent commands that
were chosen.
Detailed Design
Provide a new parseAllAsRoot() function that operates much in the
same way as parseAsRoot(), except it returns the list of parsed
commands. With such a list, a client can either store the list in a
shared location, or connect the commands together with parent
and child relationships with a custom command protocol.
Documentation Plan
The documentation has been updated with a documentation string for the new
parseAllAsRoot() function on ParseableCommand.
Test Plan
The code paths are very similar to the existing parseAsRoot(), except that the
parsed commands list is preserved in this new function instead of being thrown
away. Existing tests will continue to verify the shared code paths. In order to
verify that the command list is formed as expected, an existing E2E test case
has been enhanced to verify the correctness of the whole list.
Source Impact
There should be no impact to existing client.
Checklist