3030
3131import java .util .HashMap ;
3232import java .util .Map ;
33+ import java .util .TreeMap ;
3334
3435import org .omg .sysml .plantuml .SysML2PlantUMLStyle ;
3536
37+ import com .google .common .base .Preconditions ;
38+
3639public class SysMLInteractiveHelp {
3740
38- private static final String GENERAL_HELP_STRING =
41+
42+ private static final String GENERAL_HELP_STRING_PREFIX =
3943 "The following SysML v2 magic commands are available.\n "
4044 + "For help on a specific command, use \" %help <COMMAND>\" or \" %<cmd> -h\" .\n \n "
41- + "%eval\t \t Evaluate a given expression.\n "
42- + "%export\t \t Save a file of the JSON representation of the abstract syntax tree rooted in the named element.\n "
43- + "%help\t \t Get a list of available commands or help on a specific command\n "
44- + "%list\t \t List loaded library packages or the results of a given query\n "
45- + "%load\t Loads a model from the repository and adds it to the Xtext index\n "
46- + "%show\t \t Print the abstract syntax tree rooted in a named element\n "
47- + "%projects\t List projects in the repository\n "
48- + "%publish\t Publish to the repository the modele elements rooted in a named element\n "
49- + "%view\t \t Render the view specified by the named view usage\n "
50- + "%viz\t \t Visualize the name model elements\n "
5145 ;
5246
47+ private static final String HELP_HELP_SHORT_STRING = "%help\t \t Get a list of available commands or help on a specific command" ;
5348 private static final String HELP_HELP_STRING =
5449 "Usage: %help [<COMMAND>]\n \n "
5550 + "Print help information on the named SysML v2 magic <COMMAND>.\n "
5651 + "If no <COMMAND> is given, then list the available commands.\n " ;
5752
53+ private static final String EVAL_HELP_SHORT_STRING = "%eval\t \t Evaluate a given expression." ;
5854 private static final String EVAL_HELP_STRING =
5955 "Usage: %eval [--target=<NAME>] <EXPR>\n \n "
6056 + "Print the results of evaluating <EXPR> on the target given by <NAME>, which must be fully qualified.\n "
6157 + "If a target is not given, then evaluate <EXPR> in global scope.\n " ;
6258
59+ private static final String LIST_HELP_SHORT_STRING = "%list\t \t List loaded library packages or the results of a given query" ;
6360 private static final String LIST_HELP_STRING =
6461 "Usage: %list [<QUERY>]\n \n "
6562 + "If <QUERY> is not given, then list all loaded library packages.\n "
@@ -71,17 +68,24 @@ public class SysMLInteractiveHelp {
7168 + " <NAME>::**\t \t all members of the namespace <NAME> and, recursively, members of owned namespaces.\n "
7269 + "The last two forms may be optionally followed by a filter expression in square brackets.\n " ;
7370
71+ private static final String SHOW_HELP_SHORT_STRING =
72+ "%show\t \t Print the abstract syntax tree rooted in a named element" ;
7473 private static final String SHOW_HELP_STRING =
7574 "Usage: %show [--style=<STYLE>] <NAME>\n \n "
7675 + "Print the abstract syntax tree rooted in <NAME>. <NAME> must be fully qualified.\n \n "
7776 + "<STYLE> is also case insensitive. The possible style names are:\n "
7877 + " TREE\t \t Hierarchically indented representation with only identifying information\n "
7978 + " JSON\t \t Complete JSON representation of the tree\n " ;
8079
80+ private static final String PUBLISH_HELP_SHORT_STRING =
81+ "%publish\t Publish to the repository the modele elements rooted in a named element" ;
8182 private static final String PUBLISH_HELP_STRING =
8283 "Usage: %publish <NAME>\n \n "
8384 + "Publish the model elements rooted in <NAME> to the repository. <NAME> must be fully qualified.\n " ;
8485
86+
87+ private static final String VIZ_HELP_SHORT_STRING =
88+ "%viz\t \t Visualize the name model elements" ;
8589 private static final String VIZ_HELP_STRING =
8690 "Usage: %viz [--view=<VIEW>] [--style=<STYLE>...] <NAME> [<NAME>...]\n \n "
8791 + "Visualize model elements of <NAME>(s). <NAME>s must be fully qualified.\n \n "
@@ -99,6 +103,8 @@ public class SysMLInteractiveHelp {
99103 + "\t %viz --view Tree --style LR --style ortholine Pkg1::PartDef Pkg1::Pkg2::partUsage\n "
100104 + "should visualize Pkg1::PartDef and Pkg1::Pkg2::partUsage with a tree view ordered in the left-to-right direction with orthogonal lines.\n " ;
101105
106+ private static final String VIEW_HELP_SHORT_STRING =
107+ "%view\t \t Render the view specified by the named view usage" ;
102108 private static final String VIEW_HELP_STRING =
103109 "Usage: %view [--render=<RENDERING>] [--style=<STYLE>...] <NAME>\n \n "
104110 + "Render the view specified by the view usage <NAME>. <NAME> must be fully qualified.\n "
@@ -114,23 +120,38 @@ public class SysMLInteractiveHelp {
114120 + SysML2PlantUMLStyle .getStyleHelp ();
115121
116122
123+ private static final String EXPORT_HELP_SHORT_STRING =
124+ "%export\t \t Save a file of the JSON representation of the abstract syntax tree rooted in the named element." ;
117125 private static final String EXPORT_HELP_STRING =
118126 "Usage: %export <NAME>\n \n "
119127 + "Save a file containing the complete JSON representation of the abstract syntax tree rooted in <NAME>.\n "
120128 + "<NAME> must be fully qualified.\n " ;
121129
130+ private static final String LOAD_HELP_SHORT_STRING = "%load\t \t Loads a model from the repository and adds it to the Xtext index" ;
122131 private static final String LOAD_HELP_STRING =
123132 "Usage: %load [--id=<PROJECT ID] [--name=<NAME>] [<NAME>]\n \n "
124- + "Downloads previously published models from the repository. <NAME> must be the full name of the project.\n "
133+ + "Downloads previously published models from the repository. <NAME> must be the full name of the project."
125134 + "Use --id=<PROJECT ID> to load projects by id.\n "
126135 + "Use %projects to view repository contents.\n " ;
127136
137+ private static final String PROJECTS_HELP_SHORT_STRING = "%load\t \t Loads a model from the repository and adds it to the Xtext index" ;
128138 private static final String PROJECTS_HELP_STRING =
129139 "Usage: %projects\n \n "
130140 + "Returns the name and identifier from the repository for all publications\n " ;
131141
142+ private static String generalHelpCached ;
143+
132144 public static String getGeneralHelp () {
133- return GENERAL_HELP_STRING ;
145+ if (generalHelpCached == null ) {
146+ StringBuilder sb = new StringBuilder ();
147+ sb .append (GENERAL_HELP_STRING_PREFIX );
148+ for (var entry : commandShortHelpMap .entrySet ()) {
149+ sb .append (entry .getValue ());
150+ sb .append (System .lineSeparator ());
151+ }
152+ generalHelpCached = sb .toString ();
153+ }
154+ return generalHelpCached ;
134155 }
135156
136157 public static String getHelpHelp () {
@@ -173,22 +194,37 @@ public static String getLoadHelp() {
173194 return LOAD_HELP_STRING ;
174195 }
175196
176- private static Map <String , String > commandHelpMap = createCommandHelpMap ();
197+ private static Map <String , String > commandShortHelpMap = new TreeMap <>();
198+ private static Map <String , String > commandHelpMap = new HashMap <>();
199+ static {
200+ registerHelpString ("%help" , HELP_HELP_SHORT_STRING , HELP_HELP_STRING );
201+ registerHelpString ("%eval" , EVAL_HELP_SHORT_STRING , EVAL_HELP_STRING );
202+ registerHelpString ("%list" , LIST_HELP_SHORT_STRING , LIST_HELP_STRING );
203+ registerHelpString ("%show" , SHOW_HELP_SHORT_STRING , SHOW_HELP_STRING );
204+ registerHelpString ("%publish" , PUBLISH_HELP_SHORT_STRING , PUBLISH_HELP_STRING );
205+ registerHelpString ("%viz" , VIZ_HELP_SHORT_STRING , VIZ_HELP_STRING );
206+ registerHelpString ("%view" , VIEW_HELP_SHORT_STRING , VIEW_HELP_STRING );
207+ registerHelpString ("%export" , EXPORT_HELP_SHORT_STRING , EXPORT_HELP_STRING );
208+ registerHelpString ("%load" , LOAD_HELP_SHORT_STRING , LOAD_HELP_STRING );
209+ registerHelpString ("%projects" , PROJECTS_HELP_SHORT_STRING , PROJECTS_HELP_STRING );
210+ }
177211
178- private static Map <String , String > createCommandHelpMap () {
179- Map <String , String > map = new HashMap <>();
180- map .put ("%help" , HELP_HELP_STRING );
181- map .put ("%eval" , EVAL_HELP_STRING );
182- map .put ("%list" , LIST_HELP_STRING );
183- map .put ("%show" , SHOW_HELP_STRING );
184- map .put ("%publish" , PUBLISH_HELP_STRING );
185- map .put ("%viz" , VIZ_HELP_STRING );
186- map .put ("%view" , VIEW_HELP_STRING );
187- map .put ("%export" , EXPORT_HELP_STRING );
188- map .put ("%load" , LOAD_HELP_STRING );
189- map .put ("%projects" , PROJECTS_HELP_STRING );
190-
191- return map ;
212+ /**
213+ * Adds command helps to the centralized store.
214+ * @param command The name of the magic command, including the '%' prefix
215+ * @param shortDescription
216+ * A one-line description of a command, including the command name with the '%' prefix
217+ * one or two tabs to make the output of the %help command nicely formatted.
218+ * @param detailedHelp
219+ * A multi-line help for a given command, expected to also detail the parameter formatting
220+ *
221+ * @throws IllegalStateException when help for a command is being re-registered
222+ */
223+ public static void registerHelpString (String command , String shortDescription , String detailedHelp ) {
224+ Preconditions .checkState (!commandHelpMap .containsKey (command ), "Help is already registered for command %s" , command );
225+ commandShortHelpMap .put (command , shortDescription );
226+ commandHelpMap .put (command , detailedHelp );
227+ generalHelpCached = null ;
192228 }
193229
194230 public static String getHelpString (String command ) {
0 commit comments