| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 | 2 |
| 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 4 // for details. All rights reserved. Use of this source code is governed by a | 4 // for details. All rights reserved. Use of this source code is governed by a |
| 5 // BSD-style license that can be found in the LICENSE file. | 5 // BSD-style license that can be found in the LICENSE file. |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:args/args.dart'; | 10 import 'package:args/args.dart'; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 _readOptions(options) { | 59 _readOptions(options) { |
| 60 kind = _parseKind(options[KIND_FLAG]); | 60 kind = _parseKind(options[KIND_FLAG]); |
| 61 machineFormat = options[MACHINE_FLAG]; | 61 machineFormat = options[MACHINE_FLAG]; |
| 62 overwriteFileContents = options[WRITE_FLAG]; | 62 overwriteFileContents = options[WRITE_FLAG]; |
| 63 selection = _parseSelection(options[SELECTION_FLAG]); | 63 selection = _parseSelection(options[SELECTION_FLAG]); |
| 64 formatterSettings = | 64 formatterSettings = |
| 65 new FormatterOptions(codeTransforms: options[TRANSFORM_FLAG], | 65 new FormatterOptions(codeTransforms: options[TRANSFORM_FLAG], |
| 66 pageWidth: _toInt(options[MAX_LINE_FLAG])); | 66 pageWidth: _parseLineLength(options[MAX_LINE_FLAG])); |
| 67 } | 67 } |
| 68 | 68 |
| 69 CodeKind _parseKind(kindOption) { | 69 CodeKind _parseKind(kindOption) { |
| 70 switch(kindOption) { | 70 switch(kindOption) { |
| 71 case 'stmt' : | 71 case 'stmt' : |
| 72 return CodeKind.STATEMENT; | 72 return CodeKind.STATEMENT; |
| 73 default: | 73 default: |
| 74 return CodeKind.COMPILATION_UNIT; | 74 return CodeKind.COMPILATION_UNIT; |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 int _parseLineLength(String lengthOption) { |
| 79 var length = _toInt(lengthOption); |
| 80 if (length == null) { |
| 81 var val = lengthOption.toUpperCase(); |
| 82 if (val == 'INF' || val == 'INFINITY') { |
| 83 length = -1; |
| 84 } else { |
| 85 throw new FormatterException('Line length is specified as an Integer or ' |
| 86 'the value "Inf".'); |
| 87 } |
| 88 } |
| 89 return length; |
| 90 } |
| 91 |
| 92 |
| 78 Selection _parseSelection(selectionOption) { | 93 Selection _parseSelection(selectionOption) { |
| 79 if (selectionOption != null) { | 94 if (selectionOption != null) { |
| 80 var units = selectionOption.split(','); | 95 var units = selectionOption.split(','); |
| 81 if (units.length == 2) { | 96 if (units.length == 2) { |
| 82 var offset = _toInt(units[0]); | 97 var offset = _toInt(units[0]); |
| 83 var length = _toInt(units[1]); | 98 var length = _toInt(units[1]); |
| 84 if (offset != null && length != null) { | 99 if (offset != null && length != null) { |
| 85 return new Selection(offset, length); | 100 return new Selection(offset, length); |
| 86 } | 101 } |
| 87 } | 102 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 /// Initialize the arg parser instance. | 167 /// Initialize the arg parser instance. |
| 153 ArgParser _initArgParser() { | 168 ArgParser _initArgParser() { |
| 154 // NOTE: these flags are placeholders only! | 169 // NOTE: these flags are placeholders only! |
| 155 var parser = new ArgParser(); | 170 var parser = new ArgParser(); |
| 156 parser.addFlag(WRITE_FLAG, abbr: 'w', negatable: false, | 171 parser.addFlag(WRITE_FLAG, abbr: 'w', negatable: false, |
| 157 help: 'Write reformatted sources to files (overwriting contents). ' | 172 help: 'Write reformatted sources to files (overwriting contents). ' |
| 158 'Do not print reformatted sources to standard output.'); | 173 'Do not print reformatted sources to standard output.'); |
| 159 parser.addFlag(TRANSFORM_FLAG, abbr: 't', negatable: false, | 174 parser.addFlag(TRANSFORM_FLAG, abbr: 't', negatable: false, |
| 160 help: 'Perform code transformations.'); | 175 help: 'Perform code transformations.'); |
| 161 parser.addOption(MAX_LINE_FLAG, abbr: 'l', defaultsTo: '80', | 176 parser.addOption(MAX_LINE_FLAG, abbr: 'l', defaultsTo: '80', |
| 162 help: 'Wrap lines longer than this length.'); | 177 help: 'Wrap lines longer than this length. ' |
| 178 'To never wrap, specify "Infinity" or "Inf" for short.'); |
| 163 parser.addOption(KIND_FLAG, abbr: 'k', defaultsTo: 'cu', | 179 parser.addOption(KIND_FLAG, abbr: 'k', defaultsTo: 'cu', |
| 164 help: 'Specify source snippet kind ("stmt" or "cu")' | 180 help: 'Specify source snippet kind ("stmt" or "cu") ' |
| 165 ' --- [PROVISIONAL API].', hide: true); | 181 '--- [PROVISIONAL API].', hide: true); |
| 166 parser.addOption(SELECTION_FLAG, abbr: 's', | 182 parser.addOption(SELECTION_FLAG, abbr: 's', |
| 167 help: 'Specify selection information as an offset,length pair ' | 183 help: 'Specify selection information as an offset,length pair ' |
| 168 '(e.g., -s "0,4").', hide: true); | 184 '(e.g., -s "0,4").', hide: true); |
| 169 parser.addFlag(MACHINE_FLAG, abbr: 'm', negatable: false, | 185 parser.addFlag(MACHINE_FLAG, abbr: 'm', negatable: false, |
| 170 help: 'Produce output in a format suitable for parsing.'); | 186 help: 'Produce output in a format suitable for parsing.'); |
| 171 parser.addFlag(HELP_FLAG, abbr: 'h', negatable: false, | 187 parser.addFlag(HELP_FLAG, abbr: 'h', negatable: false, |
| 172 help: 'Print this usage information.'); | 188 help: 'Print this usage information.'); |
| 173 return parser; | 189 return parser; |
| 174 } | 190 } |
| 175 | 191 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 'offset': formatResult.selection.offset, | 228 'offset': formatResult.selection.offset, |
| 213 'length': formatResult.selection.length | 229 'length': formatResult.selection.length |
| 214 } | 230 } |
| 215 }); | 231 }); |
| 216 | 232 |
| 217 /// Log the given [msg]. | 233 /// Log the given [msg]. |
| 218 _log(String msg) { | 234 _log(String msg) { |
| 219 //TODO(pquitslund): add proper log support | 235 //TODO(pquitslund): add proper log support |
| 220 print(msg); | 236 print(msg); |
| 221 } | 237 } |
| OLD | NEW |