OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
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.md file. | |
4 | |
5 library fletchc.verbs.help_verb; | |
6 | |
7 import 'infrastructure.dart'; | |
8 | |
9 import 'actions.dart' show | |
10 commonActions, | |
11 uncommonActions; | |
12 | |
13 import 'documentation.dart' show | |
14 helpDocumentation, | |
15 synopsis; | |
16 | |
17 const Action helpAction = | |
18 const Action( | |
19 help, helpDocumentation, | |
20 supportedTargets: const [ TargetKind.ALL ], allowsTrailing: true); | |
21 | |
22 Future<int> help(AnalyzedSentence sentence, _) async { | |
23 int exitCode = 0; | |
24 bool showAllActions = sentence.target != null; | |
25 if (sentence.trailing != null) { | |
26 exitCode = 1; | |
27 } | |
28 if (sentence.verb.name != "help") { | |
29 exitCode = 1; | |
30 } | |
31 print(generateHelpText(showAllActions)); | |
32 return exitCode; | |
33 } | |
34 | |
35 String generateHelpText(bool showAllActions) { | |
36 List<String> helpStrings = <String>[synopsis]; | |
37 addAction(String name, Action action) { | |
38 helpStrings.add(""); | |
39 List<String> lines = action.documentation.trimRight().split("\n"); | |
40 for (int i = 0; i < lines.length; i++) { | |
41 String line = lines[i]; | |
42 if (line.length > 80) { | |
43 throw new StateError( | |
44 "Line ${i+1} of Action '$name' is too long and may not be " | |
45 "visible in a normal terminal window: $line\n" | |
46 "Please trim to 80 characters or fewer."); | |
47 } | |
48 helpStrings.add(lines[i]); | |
49 } | |
50 } | |
51 List<String> names = <String>[]..addAll(commonActions.keys); | |
52 if (showAllActions) { | |
53 names.addAll(uncommonActions.keys); | |
54 } | |
55 if (showAllActions) { | |
56 names.sort(); | |
57 } | |
58 for (String name in names) { | |
59 Action action = commonActions[name]; | |
60 if (action == null) { | |
61 action = uncommonActions[name]; | |
62 } | |
63 addAction(name, action); | |
64 } | |
65 | |
66 if (!showAllActions && helpStrings.length > 20) { | |
67 throw new StateError( | |
68 "More than 20 lines in the combined documentation of [commonActions]. " | |
69 "The documentation may scroll out of view:\n${helpStrings.join('\n')}." | |
70 "Can you shorten the documentation?"); | |
71 } | |
72 return helpStrings.join("\n"); | |
73 } | |
OLD | NEW |