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 /** | 11 /// Takes an [ArgParser] and generates a string of usage (i.e. help) text for |
12 * Takes an [ArgParser] and generates a string of usage (i.e. help) text for its | 12 /// its defined options. Internally, it works like a tabular printer. The |
13 * defined options. Internally, it works like a tabular printer. The output is | 13 /// output is divided into three horizontal columns, like so: |
14 * divided into three horizontal columns, like so: | 14 /// |
15 * | 15 /// -h, --help Prints the usage information |
16 * -h, --help Prints the usage information | 16 /// | | | | |
17 * | | | | | 17 /// |
18 * | 18 /// It builds the usage text up one column at a time and handles padding with |
19 * It builds the usage text up one column at a time and handles padding with | 19 /// spaces and wrapping to the next line to keep the cells correctly lined up. |
20 * spaces and wrapping to the next line to keep the cells correctly lined up. | |
21 */ | |
22 class Usage { | 20 class Usage { |
23 static const NUM_COLUMNS = 3; // Abbreviation, long name, help. | 21 static const NUM_COLUMNS = 3; // Abbreviation, long name, help. |
24 | 22 |
25 /** The parser this is generating usage for. */ | 23 /// The parser this is generating usage for. |
26 final ArgParser args; | 24 final ArgParser args; |
27 | 25 |
28 /** The working buffer for the generated usage text. */ | 26 /// The working buffer for the generated usage text. |
29 StringBuffer buffer; | 27 StringBuffer buffer; |
30 | 28 |
31 /** | 29 /// The column that the "cursor" is currently on. If the next call to |
32 * The column that the "cursor" is currently on. If the next call to | 30 /// [write()] is not for this column, it will correctly handle advancing to |
33 * [write()] is not for this column, it will correctly handle advancing to | 31 /// the next column (and possibly the next row). |
34 * the next column (and possibly the next row). | |
35 */ | |
36 int currentColumn = 0; | 32 int currentColumn = 0; |
37 | 33 |
38 /** The width in characters of each column. */ | 34 /// The width in characters of each column. |
39 List<int> columnWidths; | 35 List<int> columnWidths; |
40 | 36 |
41 /** | 37 /// The number of sequential lines of text that have been written to the last |
42 * The number of sequential lines of text that have been written to the last | 38 /// column (which shows help info). We track this so that help text that spans |
43 * column (which shows help info). We track this so that help text that spans | 39 /// multiple lines can be padded with a blank line after it for separation. |
44 * multiple lines can be padded with a blank line after it for separation. | 40 /// Meanwhile, sequential options with single-line help will be compacted next |
45 * Meanwhile, sequential options with single-line help will be compacted next | 41 /// to each other. |
46 * to each other. | |
47 */ | |
48 int numHelpLines = 0; | 42 int numHelpLines = 0; |
49 | 43 |
50 /** | 44 /// How many newlines need to be rendered before the next bit of text can be |
51 * How many newlines need to be rendered before the next bit of text can be | 45 /// written. We do this lazily so that the last bit of usage doesn't have |
52 * written. We do this lazily so that the last bit of usage doesn't have | 46 /// dangling newlines. We only write newlines right *before* we write some |
53 * dangling newlines. We only write newlines right *before* we write some | 47 /// real content. |
54 * real content. | |
55 */ | |
56 int newlinesNeeded = 0; | 48 int newlinesNeeded = 0; |
57 | 49 |
58 Usage(this.args); | 50 Usage(this.args); |
59 | 51 |
60 /** | 52 /// Generates a string displaying usage information for the defined options. |
61 * Generates a string displaying usage information for the defined options. | 53 /// This is basically the help text shown on the command line. |
62 * This is basically the help text shown on the command line. | |
63 */ | |
64 String generate() { | 54 String generate() { |
65 buffer = new StringBuffer(); | 55 buffer = new StringBuffer(); |
66 | 56 |
67 calculateColumnWidths(); | 57 calculateColumnWidths(); |
68 | 58 |
69 args.options.forEach((name, option) { | 59 args.options.forEach((name, option) { |
70 if (option.hide) return; | 60 if (option.hide) return; |
71 | 61 |
72 write(0, getAbbreviation(option)); | 62 write(0, getAbbreviation(option)); |
73 write(1, getLongOption(option)); | 63 write(1, getLongOption(option)); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 if (allowed == option.defaultValue) { | 212 if (allowed == option.defaultValue) { |
223 allowedBuffer.write(' (default)'); | 213 allowedBuffer.write(' (default)'); |
224 } | 214 } |
225 first = false; | 215 first = false; |
226 } | 216 } |
227 allowedBuffer.write(']'); | 217 allowedBuffer.write(']'); |
228 return allowedBuffer.toString(); | 218 return allowedBuffer.toString(); |
229 } | 219 } |
230 } | 220 } |
231 | 221 |
232 /** Pads [source] to [length] by adding spaces at the end. */ | 222 /// Pads [source] to [length] by adding spaces at the end. |
233 String padRight(String source, int length) { | 223 String padRight(String source, int length) { |
234 final result = new StringBuffer(); | 224 final result = new StringBuffer(); |
235 result.write(source); | 225 result.write(source); |
236 | 226 |
237 while (result.length < length) { | 227 while (result.length < length) { |
238 result.write(' '); | 228 result.write(' '); |
239 } | 229 } |
240 | 230 |
241 return result.toString(); | 231 return result.toString(); |
242 } | 232 } |
OLD | NEW |