2121 *
2222 * Contributors:
2323 * Ed Seidewitz, MDS
24- * Ivan Gomes
25- * Hisashi Miyashita
24+ * Zoltan Ujhelyi, MDS
25+ * Ivan Gomes, Twingineer
26+ * Hisashi Miyashita, Mgnite
2627 *
2728 *******************************************************************************/
2829
2930package org .omg .sysml .interactive ;
3031
3132import java .util .HashMap ;
3233import java .util .Map ;
34+ import java .util .TreeMap ;
3335
3436import org .omg .sysml .plantuml .SysML2PlantUMLStyle ;
3537
38+ import com .google .common .base .Preconditions ;
39+
3640public class SysMLInteractiveHelp {
3741
38- private static final String GENERAL_HELP_STRING =
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 \t Load a model from the repository\n "
46- + "%repo\t Set the api base path for the repository\n "
47- + "%show\t \t Print the abstract syntax tree rooted in a named element\n "
48- + "%projects\t List projects in the repository\n "
49- + "%publish\t Publish to the repository the modele elements rooted in a named element\n "
50- + "%view\t \t Render the view specified by the named view usage\n "
51- + "%viz\t \t Visualize the name model elements\n "
5245 ;
5346
47+ private static final String HELP_HELP_SHORT_STRING = "%help\t \t Get a list of available commands or help on a specific command" ;
5448 private static final String HELP_HELP_STRING =
5549 "Usage: %help [<COMMAND>]\n \n "
5650 + "Print help information on the named SysML v2 magic <COMMAND>.\n "
5751 + "If no <COMMAND> is given, then list the available commands.\n " ;
5852
53+ private static final String EVAL_HELP_SHORT_STRING = "%eval\t \t Evaluate a given expression." ;
5954 private static final String EVAL_HELP_STRING =
6055 "Usage: %eval [--target=<NAME>] <EXPR>\n \n "
6156 + "Print the results of evaluating <EXPR> on the target given by <NAME>, which must be fully qualified.\n "
6257 + "If a target is not given, then evaluate <EXPR> in global scope.\n " ;
6358
59+ private static final String LIST_HELP_SHORT_STRING = "%list\t \t List loaded library packages or the results of a given query" ;
6460 private static final String LIST_HELP_STRING =
6561 "Usage: %list [<QUERY>]\n \n "
6662 + "If <QUERY> is not given, then list all loaded library packages.\n "
@@ -72,13 +68,17 @@ public class SysMLInteractiveHelp {
7268 + " <NAME>::**\t \t all members of the namespace <NAME> and, recursively, members of owned namespaces.\n "
7369 + "The last two forms may be optionally followed by a filter expression in square brackets.\n " ;
7470
71+ private static final String SHOW_HELP_SHORT_STRING =
72+ "%show\t \t Print the abstract syntax tree rooted in a named element" ;
7573 private static final String SHOW_HELP_STRING =
7674 "Usage: %show [--style=<STYLE>] <NAME>\n \n "
7775 + "Print the abstract syntax tree rooted in <NAME>. <NAME> must be fully qualified.\n \n "
7876 + "<STYLE> is also case insensitive. The possible style names are:\n "
7977 + " TREE\t \t Hierarchically indented representation with only identifying information\n "
8078 + " JSON\t \t Complete JSON representation of the tree\n " ;
8179
80+ private static final String PUBLISH_HELP_SHORT_STRING =
81+ "%publish\t Publish to the repository the model elements rooted in a named element" ;
8282 private static final String PUBLISH_HELP_STRING =
8383 "Usage: %publish [-d] [--project=<PROJECT NAME>] [--branch=<BRANCH NAME>] <NAME>\n \n "
8484 + "Publish the model elements rooted in <NAME> to the repository. <NAME> must be fully qualified.\n "
@@ -90,6 +90,9 @@ public class SysMLInteractiveHelp {
9090 + "If <BRANCH NAME> is given, then the model is written to this branch of the project.\n "
9191 + "If <BRANCH NAME> is not given, the default branch is used.\n " ;
9292
93+
94+ private static final String VIZ_HELP_SHORT_STRING =
95+ "%viz\t \t Visualize the named model elements" ;
9396 private static final String VIZ_HELP_STRING =
9497 "Usage: %viz [--view=<VIEW>] [--style=<STYLE>...] <NAME> [<NAME>...]\n \n "
9598 + "Visualize model elements of <NAME>(s). <NAME>s must be fully qualified.\n \n "
@@ -107,6 +110,8 @@ public class SysMLInteractiveHelp {
107110 + "\t %viz --view Tree --style LR --style ortholine Pkg1::PartDef Pkg1::Pkg2::partUsage\n "
108111 + "should visualize Pkg1::PartDef and Pkg1::Pkg2::partUsage with a tree view ordered in the left-to-right direction with orthogonal lines.\n " ;
109112
113+ private static final String VIEW_HELP_SHORT_STRING =
114+ "%view\t \t Render the view specified by the named view usage" ;
110115 private static final String VIEW_HELP_STRING =
111116 "Usage: %view [--render=<RENDERING>] [--style=<STYLE>...] <NAME>\n \n "
112117 + "Render the view specified by the view usage <NAME>. <NAME> must be fully qualified.\n "
@@ -122,34 +127,51 @@ public class SysMLInteractiveHelp {
122127 + SysML2PlantUMLStyle .getStyleHelp ();
123128
124129
130+ private static final String EXPORT_HELP_SHORT_STRING =
131+ "%export\t \t Save a file of the JSON representation of the abstract syntax tree rooted in the named element." ;
125132 private static final String EXPORT_HELP_STRING =
126133 "Usage: %export <NAME>\n \n "
127134 + "Save a file containing the complete JSON representation of the abstract syntax tree rooted in <NAME>.\n "
128135 + "<NAME> must be fully qualified.\n " ;
129136
137+ private static final String LOAD_HELP_SHORT_STRING = "%load\t \t Load models from the repository" ;
130138 private static final String LOAD_HELP_STRING =
131- "Usage: %load [--id=<PROJECT ID] [--name=<NAME>] [--branch=<BRANCH_NAME>] [<NAME>]\n \n "
132- + "Download previously published models from a project in the repository. <NAME> is the full name of the project. \n "
133- + "Named elements of the downloaded models may then be referenced models in the notebook."
134- + "(Use %projects to view repository contents.) \n "
139+ "Usage: %load [--id=<PROJECT ID> ] [--name=<NAME>] [--branch=<BRANCH_NAME>] [<NAME>]\n \n "
140+ + "Download previously published models from a project in the repository. (Use %projects to view repository contents.) \n "
141+ + "Named elements of the downloaded models may then be referenced by models in the notebook.\n "
142+ + "<NAME> is the full name of the project. \n "
135143 + "If <PROJECT ID> is given, then the project with that UUID is loaded. In this case, the <NAME> must not be given.\n "
136- + "If <BRANCH NAME> is given, then the model is loaded from this branch of the project.\n "
137- + "If <BRANCH NAME> is not given, the default branch is used.\n " ;
144+ + "If <BRANCH NAME> or <BRANCH ID> is given, then the model is loaded from this branch of the project.\n "
145+ + "If no <BRANCH NAME> or <BRANCH ID> is given, the default branch is used.\n "
146+ + "If <BRANCH ID> is given, then a <BRANCH NAME> must not be given.\n " ;
138147
148+ private static final String PROJECTS_HELP_SHORT_STRING = "%projects\t List projects in the repository" ;
139149 private static final String PROJECTS_HELP_STRING =
140150 "Usage: %projects\n \n "
141151 + "Print the name and identifier of all projects in the repository.\n " ;
142152
143- private static final String API_BASE_PATH_HELP_STRING =
153+ private static final String REPO_HELP_SHORT_STRING ="%repo\t \t Set the API base path for the repository" ;
154+ private static final String REPO_HELP_STRING =
144155 "Usage: %repo [<BASE PATH>]\n \n "
145- + "If <BASE PATH> is not given, print the current repository base path.\r \n "
146- + "If <BASE PATH> is given, set the repository base path.\r \n "
147- + "\r \n "
148- + "<BASE PATH> is a URL giving the API base path for the repository access by the %projects, %publish and %load commands. \r \n "
149- + "For example: https://my.domain.com/sysml_repo" ;
150-
156+ + "Set the API base path for the repository accessed by the %projects, %publish and %load commands.\n "
157+ + "<BASE PATH> is a URL (possibly including port number), such as: https://my.domain.com/sysml_repo:9000.\n "
158+ + "If <BASE PATH> is not given, the current repository base path is printed.\n "
159+ + "If <BASE PATH> is given, the repository base path is set to this.\n "
160+ ;
161+
162+ private static String generalHelpCached ;
163+
151164 public static String getGeneralHelp () {
152- return GENERAL_HELP_STRING ;
165+ if (generalHelpCached == null ) {
166+ StringBuilder sb = new StringBuilder ();
167+ sb .append (GENERAL_HELP_STRING_PREFIX );
168+ for (var entry : commandShortHelpMap .entrySet ()) {
169+ sb .append (entry .getValue ());
170+ sb .append (System .lineSeparator ());
171+ }
172+ generalHelpCached = sb .toString ();
173+ }
174+ return generalHelpCached ;
153175 }
154176
155177 public static String getHelpHelp () {
@@ -192,27 +214,42 @@ public static String getLoadHelp() {
192214 return LOAD_HELP_STRING ;
193215 }
194216
195- public static String getApiBasePathHelp () {
196- return API_BASE_PATH_HELP_STRING ;
217+ public static String getRepoPathHelp () {
218+ return REPO_HELP_STRING ;
197219 }
198220
199- private static Map <String , String > commandHelpMap = createCommandHelpMap ();
200-
201- private static Map <String , String > createCommandHelpMap () {
202- Map <String , String > map = new HashMap <>();
203- map .put ("%help" , HELP_HELP_STRING );
204- map .put ("%eval" , EVAL_HELP_STRING );
205- map .put ("%list" , LIST_HELP_STRING );
206- map .put ("%show" , SHOW_HELP_STRING );
207- map .put ("%publish" , PUBLISH_HELP_STRING );
208- map .put ("%viz" , VIZ_HELP_STRING );
209- map .put ("%view" , VIEW_HELP_STRING );
210- map .put ("%export" , EXPORT_HELP_STRING );
211- map .put ("%load" , LOAD_HELP_STRING );
212- map .put ("%projects" , PROJECTS_HELP_STRING );
213- map .put ("%repo" , API_BASE_PATH_HELP_STRING );
214-
215- return map ;
221+ private static Map <String , String > commandShortHelpMap = new TreeMap <>();
222+ private static Map <String , String > commandHelpMap = new HashMap <>();
223+ static {
224+ registerHelpString ("%help" , HELP_HELP_SHORT_STRING , HELP_HELP_STRING );
225+ registerHelpString ("%eval" , EVAL_HELP_SHORT_STRING , EVAL_HELP_STRING );
226+ registerHelpString ("%list" , LIST_HELP_SHORT_STRING , LIST_HELP_STRING );
227+ registerHelpString ("%show" , SHOW_HELP_SHORT_STRING , SHOW_HELP_STRING );
228+ registerHelpString ("%publish" , PUBLISH_HELP_SHORT_STRING , PUBLISH_HELP_STRING );
229+ registerHelpString ("%viz" , VIZ_HELP_SHORT_STRING , VIZ_HELP_STRING );
230+ registerHelpString ("%view" , VIEW_HELP_SHORT_STRING , VIEW_HELP_STRING );
231+ registerHelpString ("%export" , EXPORT_HELP_SHORT_STRING , EXPORT_HELP_STRING );
232+ registerHelpString ("%load" , LOAD_HELP_SHORT_STRING , LOAD_HELP_STRING );
233+ registerHelpString ("%projects" , PROJECTS_HELP_SHORT_STRING , PROJECTS_HELP_STRING );
234+ registerHelpString ("%repo" , REPO_HELP_SHORT_STRING , REPO_HELP_STRING );
235+ }
236+
237+ /**
238+ * Adds command helps to the centralized store.
239+ * @param command The name of the magic command, including the '%' prefix
240+ * @param shortDescription
241+ * A one-line description of a command, including the command name with the '%' prefix
242+ * one or two tabs to make the output of the %help command nicely formatted.
243+ * @param detailedHelp
244+ * A multi-line help for a given command, expected to also detail the parameter formatting
245+ *
246+ * @throws IllegalStateException when help for a command is being re-registered
247+ */
248+ public static void registerHelpString (String command , String shortDescription , String detailedHelp ) {
249+ Preconditions .checkState (!commandHelpMap .containsKey (command ), "Help is already registered for command %s" , command );
250+ commandShortHelpMap .put (command , shortDescription );
251+ commandHelpMap .put (command , detailedHelp );
252+ generalHelpCached = null ;
216253 }
217254
218255 public static String getHelpString (String command ) {
0 commit comments