args
args copied to clipboard
ArgResults should either implement the Map interface or make the options Map accessible
Originally opened as dart-lang/sdk#12771
This issue was originally filed by [email protected]
Right now, the options Map is private. ArgResults defines the [] operator to allow Map-like read-only access.
This works fine. However, you end up passing the ArgResults object around, because pulling the values out of that ArgsResult object is quite a bit of work. This is inconvenient when it comes to testing, because creating an ArgResults object is more work than creating a Map.
Also, this kind of dependency is really weird. Whatever that function does, it probably has very little to do with argument parsing. It shouldn't take an ArgsResult object as argument.
<img src="https://avatars.githubusercontent.com/u/5449880?v=3" align="left" width="48" height="48"hspace="10"> Comment by iposva-google
Added Area-Pkg, Library-Args, Triaged labels.
<img src="https://avatars.githubusercontent.com/u/3276024?v=3" align="left" width="48" height="48"hspace="10"> Comment by anders-sandholm
Removed Library-Args label. Added Pkg-Args label.
<img src="https://avatars.githubusercontent.com/u/46275?v=3" align="left" width="48" height="48"hspace="10"> Comment by munificent
I think it would feel weird if ArgResults itself implemented Map. Things like results.values seems odd to me. What might be nice is ArgResults.options was an actual map. Unfortunately, that collides with the current type of .options.
I think what I'd probably like is to change that to a Map. You could then use .options.keys to get the option names. That's a breaking change, though, so I'm going to defer that a bit.
Set owner to @munificent. Removed Type-Defect, Priority-Unassigned labels. Added Type-Enhancement, Priority-Medium labels.
Suggesting an alternative: expose _parsed
to the public API as a readonly map.
Why:
I would like to accept a map from ArgParser OR a Map<String, dynamic>, the only way I can think to do that while still being type safe is like so:
Would also appreciate suggested alternatives to my code.
factory PartialParams.fromArgMap(Map<String, dynamic> args) {
return PartialParams(
arg1: args['1'],
arg2: args['2'],
arg3: args['3'],
// etc...
);
}
factory PartialParams.fromConfig(String fileName) {
var args = _parseConfigFile(fileName);
return PartialParams.fromArgMap(args);
}
factory PartialParams.fromArgs(List<String> args) {
var parsedArgs = parser.parse(args); /// parser is my [ArgParser]
/// THIS IS A WORKAROUND IN ORDER TO BE TYPE SAFE IN PartialParams.fromArgMap
Map<String, dynamic> parsedArgsMap = Map.fromIterable(
parsedArgs.options.map((e) => MapEntry(e, parsedArgs[e])),
);
/// END OF WORKAROUND
return PartialParams.fromArgMap(parsedArgsMap);
}