-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLineParser.java
53 lines (50 loc) · 1.77 KB
/
CommandLineParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.HashMap;
import java.util.Map;
/**
* Created by Carlos on 1/25/2017.
*/
public class CommandLineParser {
protected static final String TOKEN = "token";
protected static final String MESSAGE = "message";
protected static final String CHANNEL = "channel";
public static Map parseArguments(String[] args) {
if (args.length > 6 || args.length < 5) {
usage();
return null;
}
Map arguments = new HashMap<String, String>();
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-token":
arguments.put(TOKEN, args[i + 1]);
i += 1;
break;
case "-getusers":
arguments.put(args[i].substring(1), "");
break;
case "-channel":
arguments.put(CHANNEL, args[i + 1]);
i += 1;
break;
case "-post":
arguments.put(MESSAGE, args[i + 1]);
i += 1;
break;
default:
System.out.println(args[i]);
break;
}
}
if (arguments.containsKey("getusers") && arguments.containsKey(MESSAGE)) {
usage();
return null;
}
return arguments;
}
private static void usage() {
System.out.println("Please follow one of the following formats:");
System.out.println("java MySlackBot -token <token> -getusers -channel <channel_name>\nor");
System.out.println("java MySlackBot -token <token> -post <message> -channel <channel_name>");
System.out.println("\nThe arguments could go in any order.");
}
}