parse method
parse is a basic method to parse command line arguments
args
is list of arguments from app main function.
Currently it's parsing next argument types:
- default parameters without flags and names
For example:
some_dart_app param1 param2 param3 paramEtc
And now instance of argenius have a property ordered with values:
List<String> argenius.ordered=['param1', 'param2', 'param3', 'paramEtc']
- named argument with names wich started with double minus and name with value is spliting with space symbol ' '
For example:
some_dart_app --name1 value1 name2 value
And now instance of argenius have a property named with values:
Map<String, String> argenius.named={'name1': 'value1', 'name2': 'value2'}
- named argument with names wich started with double minus and name with value is spliting with equals symbol '='
For example:
some_dart_app --name1=value1 name2=value
And now instance of argenius have a property named with values:
Map<String, String> argenius.named={'name1': 'value1', 'name2': 'value2'}
Implementation
parse(List<String> args) {
arguments.addAll(args);
for (var i = 0; i < arguments.length; i++) {
String arg = arguments[i];
if (hasNameAndValue(arg)) {
final List<String> nameWithValue = getNameWithValue(arg);
if (nameWithValue.isNotEmpty) {
named[nameWithValue[0]] = nameWithValue[1];
}
} else if (isNameOfVariable(arg)) {
String? name = getName(arg);
if (name != null) {
String? value = arguments[i + 1];
named[name] = value;
i++;
}
} else {
ordered.add(arg);
}
}
}