Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Unified Diff: pkg/args/lib/args.dart

Issue 12472019: pkg/args Option should be more strict about names and abbreviations (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: final nits Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/args/test/args_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/args/lib/args.dart
diff --git a/pkg/args/lib/args.dart b/pkg/args/lib/args.dart
index 304827b41ed4650f7d1b506c7f9a5240ac7d7ac0..48cef1642ece730acba2bbc42027c05a910255b0 100644
--- a/pkg/args/lib/args.dart
+++ b/pkg/args/lib/args.dart
@@ -285,11 +285,6 @@ class ArgParser {
// Make sure the abbreviation isn't too long or in use.
if (abbr != null) {
- if (abbr.length > 1) {
- throw new ArgumentError(
- 'Abbreviation "$abbr" is longer than one character.');
- }
-
var existing = findByAbbreviation(abbr);
if (existing != null) {
throw new ArgumentError(
@@ -342,7 +337,7 @@ class ArgParser {
class Option {
final String name;
final String abbreviation;
- final List allowed;
+ final List<String> allowed;
final defaultValue;
final Function callback;
final String help;
@@ -353,7 +348,33 @@ class Option {
Option(this.name, this.abbreviation, this.help, this.allowed,
this.allowedHelp, this.defaultValue, this.callback, {this.isFlag,
- this.negatable, this.allowMultiple: false});
+ this.negatable, this.allowMultiple: false}) {
+
+ if (name.isEmpty) {
+ throw new ArgumentError('Name cannot be empty.');
+ } else if (name.startsWith('-')) {
+ throw new ArgumentError('Name $name cannot start with "-".');
+ }
+
+ // Ensure name does not contain any invalid characters.
+ if (_invalidChars.hasMatch(name)) {
+ throw new ArgumentError('Name "$name" contains invalid characters.');
+ }
+
+ if (abbreviation != null) {
+ if (abbreviation.length != 1) {
+ throw new ArgumentError('Abbreviation must be null or have length 1.');
+ } else if(abbreviation == '-') {
+ throw new ArgumentError('Abbreviation cannot be "-".');
+ }
+
+ if (_invalidChars.hasMatch(abbreviation)) {
+ throw new ArgumentError('Abbreviation is an invalid character.');
+ }
+ }
+ }
+
+ static final _invalidChars = new RegExp(r'''[ \t\r\n"'\\/]''');
}
/**
« no previous file with comments | « no previous file | pkg/args/test/args_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698