| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library args.src.usage; | 5 library args.src.usage; |
| 6 | 6 |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 | 8 |
| 9 import '../args.dart'; | 9 import '../args.dart'; |
| 10 | 10 |
| 11 /// Takes an [ArgParser] and generates a string of usage (i.e. help) text for | 11 /// Takes an [ArgParser] and generates a string of usage (i.e. help) text for |
| 12 /// its defined options. | 12 /// its defined options. |
| 13 /// | 13 /// |
| 14 /// Internally, it works like a tabular printer. The output is divided into | 14 /// Internally, it works like a tabular printer. The output is divided into |
| 15 /// three horizontal columns, like so: | 15 /// three horizontal columns, like so: |
| 16 /// | 16 /// |
| 17 /// -h, --help Prints the usage information | 17 /// -h, --help Prints the usage information |
| 18 /// | | | | | 18 /// | | | | |
| 19 /// | 19 /// |
| 20 /// It builds the usage text up one column at a time and handles padding with | 20 /// It builds the usage text up one column at a time and handles padding with |
| 21 /// spaces and wrapping to the next line to keep the cells correctly lined up. | 21 /// spaces and wrapping to the next line to keep the cells correctly lined up. |
| 22 class Usage { | 22 class Usage { |
| 23 static const NUM_COLUMNS = 3; // Abbreviation, long name, help. | 23 static const NUM_COLUMNS = 3; // Abbreviation, long name, help. |
| 24 | 24 |
| 25 /// The parser this is generating usage for. | 25 /// A list of the [Option]s intermingled with [String] separators. |
| 26 final ArgParser args; | 26 final List optionsAndSeparators; |
| 27 | 27 |
| 28 /// The working buffer for the generated usage text. | 28 /// The working buffer for the generated usage text. |
| 29 StringBuffer buffer; | 29 StringBuffer buffer; |
| 30 | 30 |
| 31 /// The column that the "cursor" is currently on. | 31 /// The column that the "cursor" is currently on. |
| 32 /// | 32 /// |
| 33 /// If the next call to [write()] is not for this column, it will correctly | 33 /// If the next call to [write()] is not for this column, it will correctly |
| 34 /// handle advancing to the next column (and possibly the next row). | 34 /// handle advancing to the next column (and possibly the next row). |
| 35 int currentColumn = 0; | 35 int currentColumn = 0; |
| 36 | 36 |
| 37 /// The width in characters of each column. | 37 /// The width in characters of each column. |
| 38 List<int> columnWidths; | 38 List<int> columnWidths; |
| 39 | 39 |
| 40 /// The number of sequential lines of text that have been written to the last | 40 /// The number of sequential lines of text that have been written to the last |
| 41 /// column (which shows help info). | 41 /// column (which shows help info). |
| 42 /// | 42 /// |
| 43 /// We track this so that help text that spans multiple lines can be padded | 43 /// We track this so that help text that spans multiple lines can be padded |
| 44 /// with a blank line after it for separation. Meanwhile, sequential options | 44 /// with a blank line after it for separation. Meanwhile, sequential options |
| 45 /// with single-line help will be compacted next to each other. | 45 /// with single-line help will be compacted next to each other. |
| 46 int numHelpLines = 0; | 46 int numHelpLines = 0; |
| 47 | 47 |
| 48 /// How many newlines need to be rendered before the next bit of text can be | 48 /// How many newlines need to be rendered before the next bit of text can be |
| 49 /// written. | 49 /// written. |
| 50 /// | 50 /// |
| 51 /// We do this lazily so that the last bit of usage doesn't have dangling | 51 /// We do this lazily so that the last bit of usage doesn't have dangling |
| 52 /// newlines. We only write newlines right *before* we write some real | 52 /// newlines. We only write newlines right *before* we write some real |
| 53 /// content. | 53 /// content. |
| 54 int newlinesNeeded = 0; | 54 int newlinesNeeded = 0; |
| 55 | 55 |
| 56 Usage(this.args); | 56 Usage(this.optionsAndSeparators); |
| 57 | 57 |
| 58 /// Generates a string displaying usage information for the defined options. | 58 /// Generates a string displaying usage information for the defined options. |
| 59 /// This is basically the help text shown on the command line. | 59 /// This is basically the help text shown on the command line. |
| 60 String generate() { | 60 String generate() { |
| 61 buffer = new StringBuffer(); | 61 buffer = new StringBuffer(); |
| 62 | 62 |
| 63 calculateColumnWidths(); | 63 calculateColumnWidths(); |
| 64 | 64 |
| 65 args.options.forEach((name, option) { | 65 for (var optionOrSeparator in optionsAndSeparators) { |
| 66 if (option.hide) return; | 66 if (optionOrSeparator is String) { |
| 67 // Ensure that there's always a blank line before a separator. |
| 68 if (buffer.isNotEmpty) buffer.write("\n\n"); |
| 69 buffer.write(optionOrSeparator); |
| 70 newlinesNeeded = 1; |
| 71 continue; |
| 72 } |
| 73 |
| 74 var option = optionOrSeparator as Option; |
| 75 if (option.hide) continue; |
| 67 | 76 |
| 68 write(0, getAbbreviation(option)); | 77 write(0, getAbbreviation(option)); |
| 69 write(1, getLongOption(option)); | 78 write(1, getLongOption(option)); |
| 70 | 79 |
| 71 if (option.help != null) write(2, option.help); | 80 if (option.help != null) write(2, option.help); |
| 72 | 81 |
| 73 if (option.allowedHelp != null) { | 82 if (option.allowedHelp != null) { |
| 74 var allowedNames = option.allowedHelp.keys.toList(growable: false); | 83 var allowedNames = option.allowedHelp.keys.toList(growable: false); |
| 75 allowedNames.sort(); | 84 allowedNames.sort(); |
| 76 newline(); | 85 newline(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 87 } else if (!option.isFlag) { | 96 } else if (!option.isFlag) { |
| 88 write(2, '(defaults to "${option.defaultValue}")'); | 97 write(2, '(defaults to "${option.defaultValue}")'); |
| 89 } | 98 } |
| 90 } | 99 } |
| 91 | 100 |
| 92 // If any given option displays more than one line of text on the right | 101 // If any given option displays more than one line of text on the right |
| 93 // column (i.e. help, default value, allowed options, etc.) then put a | 102 // column (i.e. help, default value, allowed options, etc.) then put a |
| 94 // blank line after it. This gives space where it's useful while still | 103 // blank line after it. This gives space where it's useful while still |
| 95 // keeping simple one-line options clumped together. | 104 // keeping simple one-line options clumped together. |
| 96 if (numHelpLines > 1) newline(); | 105 if (numHelpLines > 1) newline(); |
| 97 }); | 106 } |
| 98 | 107 |
| 99 return buffer.toString(); | 108 return buffer.toString(); |
| 100 } | 109 } |
| 101 | 110 |
| 102 String getAbbreviation(Option option) { | 111 String getAbbreviation(Option option) { |
| 103 if (option.abbreviation != null) { | 112 if (option.abbreviation != null) { |
| 104 return '-${option.abbreviation}, '; | 113 return '-${option.abbreviation}, '; |
| 105 } else { | 114 } else { |
| 106 return ''; | 115 return ''; |
| 107 } | 116 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 120 return result; | 129 return result; |
| 121 } | 130 } |
| 122 | 131 |
| 123 String getAllowedTitle(String allowed) { | 132 String getAllowedTitle(String allowed) { |
| 124 return ' [$allowed]'; | 133 return ' [$allowed]'; |
| 125 } | 134 } |
| 126 | 135 |
| 127 void calculateColumnWidths() { | 136 void calculateColumnWidths() { |
| 128 int abbr = 0; | 137 int abbr = 0; |
| 129 int title = 0; | 138 int title = 0; |
| 130 args.options.forEach((name, option) { | 139 for (var option in optionsAndSeparators) { |
| 131 if (option.hide) return; | 140 if (option is! Option) continue; |
| 141 if (option.hide) continue; |
| 132 | 142 |
| 133 // Make room in the first column if there are abbreviations. | 143 // Make room in the first column if there are abbreviations. |
| 134 abbr = max(abbr, getAbbreviation(option).length); | 144 abbr = max(abbr, getAbbreviation(option).length); |
| 135 | 145 |
| 136 // Make room for the option. | 146 // Make room for the option. |
| 137 title = max(title, getLongOption(option).length); | 147 title = max(title, getLongOption(option).length); |
| 138 | 148 |
| 139 // Make room for the allowed help. | 149 // Make room for the allowed help. |
| 140 if (option.allowedHelp != null) { | 150 if (option.allowedHelp != null) { |
| 141 for (var allowed in option.allowedHelp.keys) { | 151 for (var allowed in option.allowedHelp.keys) { |
| 142 title = max(title, getAllowedTitle(allowed).length); | 152 title = max(title, getAllowedTitle(allowed).length); |
| 143 } | 153 } |
| 144 } | 154 } |
| 145 }); | 155 } |
| 146 | 156 |
| 147 // Leave a gutter between the columns. | 157 // Leave a gutter between the columns. |
| 148 title += 4; | 158 title += 4; |
| 149 columnWidths = [abbr, title]; | 159 columnWidths = [abbr, title]; |
| 150 } | 160 } |
| 151 | 161 |
| 152 void newline() { | 162 void newline() { |
| 153 newlinesNeeded++; | 163 newlinesNeeded++; |
| 154 currentColumn = 0; | 164 currentColumn = 0; |
| 155 numHelpLines = 0; | 165 numHelpLines = 0; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 String padRight(String source, int length) { | 244 String padRight(String source, int length) { |
| 235 final result = new StringBuffer(); | 245 final result = new StringBuffer(); |
| 236 result.write(source); | 246 result.write(source); |
| 237 | 247 |
| 238 while (result.length < length) { | 248 while (result.length < length) { |
| 239 result.write(' '); | 249 result.write(' '); |
| 240 } | 250 } |
| 241 | 251 |
| 242 return result.toString(); | 252 return result.toString(); |
| 243 } | 253 } |
| OLD | NEW |