2626import java .util .regex .Pattern ;
2727
2828public class Slicer {
29+ protected static final String HELP_HEADER = "Java SDG Slicer: extract a slice from a Java program. At least" +
30+ " the \" -c\" flag must be used to specify the slicing criterion." ;
31+
2932 protected static final Pattern SC_PATTERN ;
3033 protected static final File DEFAULT_OUTPUT_DIR = new File ("./slice/" );
3134 protected static final Options OPTIONS = new Options ();
@@ -39,38 +42,37 @@ public class Slicer {
3942 }
4043
4144 static {
42- OptionGroup criterionOptionGroup = new OptionGroup ();
43- criterionOptionGroup .addOption (Option
45+ OPTIONS .addOption (Option
4446 .builder ("f" ).longOpt ("file" )
4547 .hasArg ().argName ("CriterionFile.java" ).type (File .class )
4648 .desc ("The file that contains the slicing criterion." )
4749 .build ());
48- criterionOptionGroup .addOption (Option
50+ OPTIONS .addOption (Option
4951 .builder ("l" ).longOpt ("line" )
5052 .hasArg ().argName ("lineNumber" ).type (Integer .class )
5153 .desc ("The line that contains the statement of the slicing criterion." )
5254 .build ());
53- criterionOptionGroup .addOption (Option
55+ OPTIONS .addOption (Option
5456 .builder ("v" ).longOpt ("var" )
5557 .hasArgs ().argName ("variableName" ).valueSeparator (',' )
5658 .desc ("The name of the variable of the slicing criterion. Not setting this option is" +
5759 " equivalent to selecting an empty set; setting multiple variables is allowed," +
5860 " separated by commas" )
5961 .build ());
60- criterionOptionGroup .addOption (Option
62+ OPTIONS .addOption (Option
6163 .builder ("n" ).longOpt ("number" )
6264 .hasArgs ().argName ("occurrenceNumber" ).valueSeparator (',' )
6365 .desc ("The occurrence number of the variable(s) selected. If this argument is not set, it will" +
6466 " default to the first appearance of each variable. If the occurrence number must be set" +
6567 " for every variable in the same order." )
6668 .build ());
67- OPTIONS .addOptionGroup (criterionOptionGroup );
6869 OPTIONS .addOption (Option
6970 .builder ("c" ).longOpt ("criterion" )
7071 .hasArg ().argName ("file#line[:var[!occurrence]]" )
7172 .desc ("The slicing criterion, in the format \" file#line:var\" . Optionally, the occurrence can be" +
72- " appended as \" !occurrence\" . This option replaces \" -f\" , \" -l\" , \" -v\" and \" -n\" , and" +
73- " functions in a similar way: the variable and occurrence may be skipped or declared multiple times." )
73+ " appended as \" !occurrence\" . This option may be replaced by \" -f\" , \" -l\" , \" -v\" and \" -n\" ." +
74+ " If both are specified, the others are discarded. It functions in a similar way: the variable" +
75+ " and occurrence may be skipped or declared multiple times." )
7476 .build ());
7577 OPTIONS .addOption (Option
7678 .builder ("i" ).longOpt ("include" )
@@ -107,7 +109,7 @@ public class Slicer {
107109 public Slicer (String ... cliArgs ) throws ParseException {
108110 cliOpts = new DefaultParser ().parse (OPTIONS , cliArgs );
109111 if (cliOpts .hasOption ('h' ))
110- throw new ParseException ( OPTIONS . toString () );
112+ printHelp ( );
111113 if (cliOpts .hasOption ('c' )) {
112114 Matcher matcher = SC_PATTERN .matcher (cliOpts .getOptionValue ("criterion" ));
113115 if (!matcher .matches ())
@@ -132,7 +134,7 @@ public Slicer(String... cliArgs) throws ParseException {
132134 setScVars (cliOpts .getOptionValues ('v' ));
133135 }
134136 } else {
135- throw new ParseException ("Slicing criterion not specified: either use \" -c\" or \" -f\" and \" l\" ." );
137+ throw new ParseException ("Slicing criterion not specified: either use \" -c\" or \" -f\" and \" - l\" ." );
136138 }
137139
138140 if (cliOpts .hasOption ('o' ))
@@ -229,7 +231,7 @@ public void slice() throws ParseException {
229231 }
230232
231233 SDG sdg ;
232- switch (cliOpts .getOptionValue ("type" )) {
234+ switch (cliOpts .getOptionValue ("type" , "ESSDG" )) {
233235 case "SDG" : sdg = new SDG (); break ;
234236 case "ASDG" : sdg = new ASDG (); break ;
235237 case "PSDG" : sdg = new PSDG (); break ;
@@ -266,6 +268,13 @@ protected String getDisclaimer(CompilationUnit.Storage s) {
266268 scFile , scLine , String .join (", " , scVars ), s .getPath ());
267269 }
268270
271+ protected void printHelp () {
272+ HelpFormatter formatter = new HelpFormatter ();
273+ formatter .setWidth (120 );
274+ formatter .printHelp ("java -jar sdg-cli.jar" , HELP_HEADER , OPTIONS , "" , true );
275+ System .exit (0 );
276+ }
277+
269278 public static void main (String ... args ) {
270279 try {
271280 new Slicer (args ).slice ();
0 commit comments