Chromium Code Reviews| 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 16 matching lines...) Expand all Loading... | |
| 27 final List<String> paths = []; | 27 final List<String> paths = []; |
| 28 | 28 |
| 29 | 29 |
| 30 const HELP_FLAG = 'help'; | 30 const HELP_FLAG = 'help'; |
| 31 const KIND_FLAG = 'kind'; | 31 const KIND_FLAG = 'kind'; |
| 32 const MACHINE_FLAG = 'machine'; | 32 const MACHINE_FLAG = 'machine'; |
| 33 const WRITE_FLAG = 'write'; | 33 const WRITE_FLAG = 'write'; |
| 34 const SELECTION_FLAG = 'selection'; | 34 const SELECTION_FLAG = 'selection'; |
| 35 const TRANSFORM_FLAG = 'transform'; | 35 const TRANSFORM_FLAG = 'transform'; |
| 36 const MAX_LINE_FLAG = 'max_line_length'; | 36 const MAX_LINE_FLAG = 'max_line_length'; |
| 37 const INDENT_FLAG = 'indent'; | |
| 37 | 38 |
| 38 | 39 |
| 39 const FOLLOW_LINKS = false; | 40 const FOLLOW_LINKS = false; |
| 40 | 41 |
| 41 | 42 |
| 42 main(args) { | 43 main(args) { |
| 43 var options = argParser.parse(args); | 44 var options = argParser.parse(args); |
| 44 if (options['help']) { | 45 if (options['help']) { |
| 45 _printUsage(); | 46 _printUsage(); |
| 46 return; | 47 return; |
| 47 } | 48 } |
| 48 | 49 |
| 49 _readOptions(options); | 50 _readOptions(options); |
| 50 | 51 |
| 51 if (options.rest.isEmpty) { | 52 if (options.rest.isEmpty) { |
| 52 _formatStdin(kind); | 53 _formatStdin(kind); |
| 53 } else { | 54 } else { |
| 54 paths.addAll(options.rest); | 55 paths.addAll(options.rest); |
| 55 _formatPaths(paths); | 56 _formatPaths(paths); |
| 56 } | 57 } |
| 57 } | 58 } |
| 58 | 59 |
| 59 _readOptions(options) { | 60 _readOptions(options) { |
| 60 kind = _parseKind(options[KIND_FLAG]); | 61 kind = _parseKind(options[KIND_FLAG]); |
| 61 machineFormat = options[MACHINE_FLAG]; | 62 machineFormat = options[MACHINE_FLAG]; |
| 62 overwriteFileContents = options[WRITE_FLAG]; | 63 overwriteFileContents = options[WRITE_FLAG]; |
| 63 selection = _parseSelection(options[SELECTION_FLAG]); | 64 selection = _parseSelection(options[SELECTION_FLAG]); |
| 64 formatterSettings = | 65 formatterSettings = new FormatterOptions( |
| 65 new FormatterOptions(codeTransforms: options[TRANSFORM_FLAG], | 66 codeTransforms: options[TRANSFORM_FLAG], |
| 66 pageWidth: _parseLineLength(options[MAX_LINE_FLAG])); | 67 tabsForIndent: _parseTabsForIndent(options[INDENT_FLAG]), |
| 68 spacesPerIndent: _parseSpacesPerIndent(options[INDENT_FLAG]), | |
| 69 pageWidth: _parseLineLength(options[MAX_LINE_FLAG])); | |
| 67 } | 70 } |
| 68 | 71 |
| 72 /// Translate the indent option into spaces per indent. | |
| 73 int _parseSpacesPerIndent(String indentOption) { | |
| 74 if (indentOption == 'tab') { | |
| 75 return 1; | |
| 76 } | |
| 77 int spacesPerIndent = _toInt(indentOption); | |
| 78 if (spacesPerIndent == null) { | |
| 79 throw new FormatterException('Indentation is specified as an Integer or ' | |
| 80 'the value "tab".'); | |
| 81 } | |
| 82 return spacesPerIndent; | |
| 83 } | |
| 84 | |
| 85 /// Translate the indent option into tabs for indent. | |
| 86 bool _parseTabsForIndent(String indentOption) => indentOption == 'tab'; | |
| 87 | |
| 69 CodeKind _parseKind(kindOption) { | 88 CodeKind _parseKind(kindOption) { |
| 70 switch(kindOption) { | 89 switch(kindOption) { |
| 71 case 'stmt' : | 90 case 'stmt' : |
| 72 return CodeKind.STATEMENT; | 91 return CodeKind.STATEMENT; |
| 73 default: | 92 default: |
| 74 return CodeKind.COMPILATION_UNIT; | 93 return CodeKind.COMPILATION_UNIT; |
| 75 } | 94 } |
| 76 } | 95 } |
| 77 | 96 |
| 78 int _parseLineLength(String lengthOption) { | 97 int _parseLineLength(String lengthOption) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 // NOTE: these flags are placeholders only! | 188 // NOTE: these flags are placeholders only! |
| 170 var parser = new ArgParser(); | 189 var parser = new ArgParser(); |
| 171 parser.addFlag(WRITE_FLAG, abbr: 'w', negatable: false, | 190 parser.addFlag(WRITE_FLAG, abbr: 'w', negatable: false, |
| 172 help: 'Write reformatted sources to files (overwriting contents). ' | 191 help: 'Write reformatted sources to files (overwriting contents). ' |
| 173 'Do not print reformatted sources to standard output.'); | 192 'Do not print reformatted sources to standard output.'); |
| 174 parser.addFlag(TRANSFORM_FLAG, abbr: 't', negatable: false, | 193 parser.addFlag(TRANSFORM_FLAG, abbr: 't', negatable: false, |
| 175 help: 'Perform code transformations.'); | 194 help: 'Perform code transformations.'); |
| 176 parser.addOption(MAX_LINE_FLAG, abbr: 'l', defaultsTo: '80', | 195 parser.addOption(MAX_LINE_FLAG, abbr: 'l', defaultsTo: '80', |
| 177 help: 'Wrap lines longer than this length. ' | 196 help: 'Wrap lines longer than this length. ' |
| 178 'To never wrap, specify "Infinity" or "Inf" for short.'); | 197 'To never wrap, specify "Infinity" or "Inf" for short.'); |
| 198 parser.addOption(INDENT_FLAG, abbr: 'i', defaultsTo: '2', | |
| 199 help: 'Specify number of spaces per indentation. ' | |
| 200 'To indent using tabs, specify "--$INDENT_FLAG tab".'); | |
|
pquitslund
2014/03/07 19:21:45
As discussed, let's make this option hidden.
Also
danrubel
2014/03/11 14:22:36
Done.
| |
| 179 parser.addOption(KIND_FLAG, abbr: 'k', defaultsTo: 'cu', | 201 parser.addOption(KIND_FLAG, abbr: 'k', defaultsTo: 'cu', |
| 180 help: 'Specify source snippet kind ("stmt" or "cu") ' | 202 help: 'Specify source snippet kind ("stmt" or "cu") ' |
| 181 '--- [PROVISIONAL API].', hide: true); | 203 '--- [PROVISIONAL API].', hide: true); |
| 182 parser.addOption(SELECTION_FLAG, abbr: 's', | 204 parser.addOption(SELECTION_FLAG, abbr: 's', |
| 183 help: 'Specify selection information as an offset,length pair ' | 205 help: 'Specify selection information as an offset,length pair ' |
| 184 '(e.g., -s "0,4").', hide: true); | 206 '(e.g., -s "0,4").', hide: true); |
| 185 parser.addFlag(MACHINE_FLAG, abbr: 'm', negatable: false, | 207 parser.addFlag(MACHINE_FLAG, abbr: 'm', negatable: false, |
| 186 help: 'Produce output in a format suitable for parsing.'); | 208 help: 'Produce output in a format suitable for parsing.'); |
| 187 parser.addFlag(HELP_FLAG, abbr: 'h', negatable: false, | 209 parser.addFlag(HELP_FLAG, abbr: 'h', negatable: false, |
| 188 help: 'Print this usage information.'); | 210 help: 'Print this usage information.'); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 'offset': formatResult.selection.offset, | 250 'offset': formatResult.selection.offset, |
| 229 'length': formatResult.selection.length | 251 'length': formatResult.selection.length |
| 230 } | 252 } |
| 231 }); | 253 }); |
| 232 | 254 |
| 233 /// Log the given [msg]. | 255 /// Log the given [msg]. |
| 234 _log(String msg) { | 256 _log(String msg) { |
| 235 //TODO(pquitslund): add proper log support | 257 //TODO(pquitslund): add proper log support |
| 236 print(msg); | 258 print(msg); |
| 237 } | 259 } |
| OLD | NEW |