-
Notifications
You must be signed in to change notification settings - Fork 607
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
[ts-command-line] Add ScopedCommandLineAction class #3364
[ts-command-line] Add ScopedCommandLineAction class #3364
Conversation
…er/danade/ScopedCommandLine
Would a syntax like |
…eveloper to decide if the scope is required
Hmmm.... The nice part about the approach taken in the PR is that duplicate parameters across different scopes (which provide their own parameters) are handled naturally, since the args would simply be available in the Also, the design is such that you use the action to implement a single scope, with multiple different scoping parameters progressively narrowing the scope (ex. The way it is currently designed, the action allows the developer the flexibility to choose how the scope should be used and how scoped parameters should be applied within the selected scope. It's explicitly not choosing to make any statements on which arguments apply to which scope values, because IMO that should be decided by the developer. |
libraries/ts-command-line/src/test/CommandLineParameter.test.ts
Outdated
Show resolved
Hide resolved
libraries/ts-command-line/src/test/CommandLineParameter.test.ts
Outdated
Show resolved
Hide resolved
libraries/ts-command-line/src/test/CommandLineParameter.test.ts
Outdated
Show resolved
Hide resolved
libraries/ts-command-line/src/test/ScopedCommandLineAction.test.ts
Outdated
Show resolved
Hide resolved
libraries/ts-command-line/src/test/ScopedCommandLineAction.test.ts
Outdated
Show resolved
Hide resolved
libraries/ts-command-line/src/test/ScopedCommandLineAction.test.ts
Outdated
Show resolved
Hide resolved
libraries/ts-command-line/src/providers/ScopedCommandLineAction.ts
Outdated
Show resolved
Hide resolved
common/changes/@rushstack/ts-command-line/user-danade-ScopedCommandLine_2022-04-21-22-00.json
Outdated
Show resolved
Hide resolved
Co-authored-by: Ian Clanton-Thuon <[email protected]>
…er/danade/ScopedCommandLine
Use a symbol for the 'scoping' parameter group
Summary
Adds the
ScopedCommandLineAction
class, which allows for scoping on commands to influence the parsing of parameters used.Details
The
ScopedCommandLineAction
class was implemented using a 2-stage parsing technique. All scoped arguments must be provided as positional arguments specified after the pseudo-argument--
. This pseudo-argument is required when specifying scoped arguments, and forces all following arguments to be interpreted as positional. These positional arguments are dropped into theScopedCommandLineAction.remainder
property, where they are then fed to a secondary parser to validate against the defined scoped commands. This allows for argparse to validate and parse the underlying scoped arguments as if they were provided to a top-level action, and makes help documentation clear and understandable in this scenario.Underlying scoped arguments can be varied depending on the arguments provided to the owning action by the caller. This allows for two different scopes to have different arguments, eg:
<tool> <action> --scope A -- --scopedFlag1
works because--scopedFlag1
is defined only when--scope
is set toA
<tool> <action> --scope B -- --scopedFlag2
works because--scopedFlag2
is defined only when--scope
is set toB
<tool> <action> --scope A -- --scopedFlag2
does not work because--scopedFlag2
is not defined when--scope
is set toA
Additionally, scopes are not necessarily required on scoped commands (this is left up to the developer to allow or prevent), eg:
<tool> <action> --scope A
runs the action limited to scopeA
<tool> <action>
runs the action against all scopesAs mentioned,
--help
documentation is well-defined and easy to understand, eg.How it was tested
Added tests and snapshot tests to ensure that validation of parameters works as expected, and that the provided action
onExecute
method gets called with all required parameters populated.