Project

General

Profile

1
package eu.dnetlib.iis.core.java;
2

    
3
import org.apache.commons.cli.CommandLine;
4
import org.apache.commons.cli.CommandLineParser;
5
import org.apache.commons.cli.GnuParser;
6
import org.apache.commons.cli.HelpFormatter;
7
import org.apache.commons.cli.Option;
8
import org.apache.commons.cli.OptionBuilder;
9
import org.apache.commons.cli.Options;
10
import org.apache.commons.cli.ParseException;
11
import org.apache.commons.lang3.StringUtils;
12

    
13
/**
14
 * 
15
 * @author Mateusz Kobos
16
 *
17
 */
18
public class CmdLineParser {
19
	/** HACK: make the names of various types of parameters of the program 
20
	* more readable, e.g. "--Input_person=..." instead of "-Iperson=...",
21
	* "--Output_merged=..." instead of "-Omerged=...". I wasn't able to
22
	* get such notation so far using the Apache CLI. */
23
	public static String constructorPrefix = "C";
24
	public static String inputPrefix = "I";
25
	public static String outputPrefix = "O";
26
	public static String specialParametersPrefix = "S";
27
	/** HACK: This field should be removed since this list of special 
28
	 * parameters is empty, thus not used anywhere.*/
29
	public static String[] mandatorySpecialParameters = 
30
			new String[]{};
31
	public static String processParametersPrefix = "P";
32
	
33
	public static CommandLine parse(String[] args) {
34
		Options options = new Options();
35
		@SuppressWarnings("static-access")
36
		Option constructorParams = OptionBuilder.withArgName("STRING")
37
				.hasArg()
38
				.withDescription("Constructor parameter")
39
				.withLongOpt("ConstructorParam")
40
				.create(constructorPrefix);
41
		options.addOption(constructorParams);
42
		@SuppressWarnings("static-access")
43
		Option inputs = OptionBuilder.withArgName("portName=URI")
44
				.hasArgs(2)
45
				.withValueSeparator()
46
				.withDescription("Path binding for a given input port")
47
				.withLongOpt("Input")
48
				.create(inputPrefix);
49
		options.addOption(inputs);
50
		@SuppressWarnings("static-access")
51
		Option outputs = OptionBuilder.withArgName("portName=URI")
52
				.hasArgs(2)
53
				.withValueSeparator()
54
				.withDescription("Path binding for a given output port")
55
				.create(outputPrefix);
56
		options.addOption(outputs);
57
		@SuppressWarnings("static-access")
58
		Option specialParameter = OptionBuilder.withArgName("parameter_name=string")
59
				.hasArgs(2)
60
				.withValueSeparator()
61
				.withDescription(String.format("Value of special parameter. "
62
						+ "These are the mandatory parameters={%s}",
63
						StringUtils.join(mandatorySpecialParameters, ",")))
64
				.create(specialParametersPrefix);
65
		options.addOption(specialParameter);
66
		@SuppressWarnings("static-access")
67
		Option otherParameter = OptionBuilder.withArgName("parameter_name=string")
68
				.hasArgs(2)
69
				.withValueSeparator()
70
				.withDescription(
71
						String.format("Value of some other parameter."))
72
				.create(processParametersPrefix);
73
		options.addOption(otherParameter);
74
		
75
		Option help = new Option("help", "print this message");
76
		options.addOption(help);
77

    
78
		CommandLineParser parser = new GnuParser();
79
		try {
80
			CommandLine cmdLine = parser.parse(options, args);
81
			if(cmdLine.hasOption("help")){
82
				HelpFormatter formatter = new HelpFormatter();
83
				formatter.printHelp("", options );
84
				System.exit(1);
85
			}
86
			return cmdLine;
87
		} catch (ParseException e) {
88
			throw new CmdLineParserException(
89
					"Parsing command line arguments failed", e);
90
		}
91
		
92
	}
93
}
(1-1/11)