-
Notifications
You must be signed in to change notification settings - Fork 40
CPLAT-16476 Allow args after separator to TestTool #364
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 'package:args/args.dart'; | ||
|
||
/// Returns the "rest" args from [argResults], but with the arg separator "--" | ||
/// restored to its original position if it was included. | ||
/// | ||
/// This is necessary because [ArgResults.rest] will _not_ include the separator | ||
/// unless it stopped parsing before it reached the separator. | ||
/// | ||
/// The use case for this is a [CompoundTool] that uses the [takeAllArgs] arg | ||
/// mapper, because the goal there is to forward on the original args minus the | ||
/// consumed options and flags. If the separator has also been removed, you may | ||
/// hit an error when trying to parse those args. | ||
/// | ||
/// var parser = ArgParser()..addFlag('verbose', abbr: 'v'); | ||
/// var results = parser.parse(['a', '-v', 'b', '--', '--unknown', 'c']); | ||
/// print(results.rest); | ||
/// // ['a', 'b', '--unknown', 'c'] | ||
/// print(restArgsWithSeparator(results)); | ||
/// // ['a', 'b', '--', '--unknown', 'c'] | ||
List<String> restArgsWithSeparator(ArgResults argResults) { | ||
// If no separator was used, return the rest args as is. | ||
if (!argResults.arguments.contains('--')) { | ||
return argResults.rest; | ||
} | ||
|
||
final args = argResults.arguments; | ||
final rest = argResults.rest; | ||
var restIndex = 0; | ||
for (var argsIndex = 0; argsIndex < args.length; argsIndex++) { | ||
// Iterate through the original args until we hit the first separator. | ||
if (args[argsIndex] == '--') break; | ||
// While doing so, move a cursor through the rest args list each time we | ||
// match up between the original list and the rest args list. This works | ||
// because the rest args list should be an ordered subset of the original | ||
// args list. | ||
if (args[argsIndex] == rest[restIndex]) { | ||
restIndex++; | ||
} | ||
} | ||
|
||
// At this point, [restIndex] should be pointing to the spot where the first | ||
// arg separator should be restored. | ||
return [...rest.sublist(0, restIndex), '--', ...rest.sublist(restIndex)]; | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'package:args/args.dart'; | ||
import 'package:dart_dev/src/utils/rest_args_with_separator.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('restArgsWithSeparator', () { | ||
ArgParser parser; | ||
|
||
setUp(() { | ||
parser = ArgParser() | ||
..addOption('output', abbr: 'o') | ||
..addFlag('verbose', abbr: 'v'); | ||
}); | ||
|
||
test('with no args', () { | ||
final results = parser.parse([]); | ||
expect(restArgsWithSeparator(results), <String>[]); | ||
}); | ||
|
||
test('restores the separator to the correct spot', () { | ||
final results = parser.parse([ | ||
'a', | ||
'-o', | ||
'out', | ||
'-v', | ||
'b', | ||
'--', | ||
'c', | ||
'-d', | ||
]); | ||
expect(restArgsWithSeparator(results), [ | ||
'a', | ||
'b', | ||
'--', | ||
'c', | ||
'-d', | ||
]); | ||
}); | ||
|
||
test('with multiple separators', () { | ||
final results = parser | ||
.parse(['a', '-o', 'out', '-v', 'b', '--', 'c', '-d', '--', 'e']); | ||
expect(restArgsWithSeparator(results), [ | ||
'a', | ||
'b', | ||
'--', | ||
'c', | ||
'-d', | ||
'--', | ||
'e', | ||
]); | ||
}); | ||
}); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm having trouble following this. It seems like the only result of this is setting rCursor. But isn't rCursor just
args.indexOf('--')
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be the position of the separator within the original arguments list, which isn't necessarily the same as the position at which the separator should be restored in the
rest
arguments list, since the latter excludes flags and options that were parsed by the ArgParser. So for example:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I get it. Ugh. It'd be better if ArgParser just didn't think it knew about '--' and included it.