18
18
*/
19
19
final class ServerArguments implements ArgumentReceiver {
20
20
21
- static final int MIN_PORT = 0 ;
22
- static final int MAX_PORT = 65535 ;
23
- static final int DEFAULT_PORT = 0 ; // Default value for unset port number.
24
- static final String HELP = "--help" ;
25
- static final String HELP_SHORT = "-h" ;
26
- static final String PORT_NUMBER = "--port-number " ;
27
- static final String PORT_NUMBER_SHORT = "-p" ;
28
- static final String PORT_NUMBER_POSITIONAL = "<port_number >" ;
29
- private int portNumber = DEFAULT_PORT ;
21
+ private static final int MIN_PORT = 0 ;
22
+ private static final int MAX_PORT = 65535 ;
23
+ private static final int DEFAULT_PORT = 0 ; // Default value for unset port number.
24
+ private static final String HELP = "--help" ;
25
+ private static final String HELP_SHORT = "-h" ;
26
+ private static final String PORT = "--port" ;
27
+ private static final String PORT_SHORT = "-p" ;
28
+ private static final String PORT_POSITIONAL = "<port >" ;
29
+ private int port = DEFAULT_PORT ;
30
30
private boolean help = false ;
31
31
32
32
@@ -39,17 +39,20 @@ static ServerArguments create(String[] args) {
39
39
serverArguments .printHelp (arguments );
40
40
}
41
41
if (!positional .isEmpty ()) {
42
- serverArguments .portNumber = serverArguments .validatePortNumber (positional .getFirst ());
42
+ serverArguments .port = serverArguments .validatePortNumber (positional .getFirst ());
43
43
}
44
44
return serverArguments ;
45
45
}
46
46
47
47
@ Override
48
48
public void registerHelp (HelpPrinter printer ) {
49
49
printer .option (HELP , HELP_SHORT , "Print this help output." );
50
- printer .param (PORT_NUMBER , PORT_NUMBER_SHORT , "PORT_NUMBER" ,
51
- "The port number to be used by the Smithy Language Server. Default port number is 0 if not specified." );
52
- printer .option (PORT_NUMBER_POSITIONAL , null , "Positional port-number." );
50
+ printer .param (PORT , PORT_SHORT , "PORT" ,
51
+ "The port to use for talking to the client. When not specified, or set to 0, "
52
+ + "standard in/out is used. Standard in/out is preferred, "
53
+ + "so usually this shouldn't be specified." );
54
+ printer .option (PORT_POSITIONAL , null , "Deprecated: use --port instead. When not specified, or set to 0, "
55
+ + "standard in/out is used. Standard in/out is preferred, so usually this shouldn't be specified." );
53
56
}
54
57
55
58
@ Override
@@ -63,40 +66,44 @@ public boolean testOption(String name) {
63
66
64
67
@ Override
65
68
public Consumer <String > testParameter (String name ) {
66
- if (name .equals (PORT_NUMBER_SHORT ) || name .equals (PORT_NUMBER )) {
69
+ if (name .equals (PORT_SHORT ) || name .equals (PORT )) {
67
70
return value -> {
68
- portNumber = validatePortNumber (value );
71
+ port = validatePortNumber (value );
69
72
};
70
73
}
71
74
return null ;
72
75
}
73
76
74
- public int getPortNumber () {
75
- return portNumber ;
77
+ int port () {
78
+ return port ;
76
79
}
77
80
78
- public boolean help () {
81
+ boolean help () {
79
82
return help ;
80
83
}
81
84
82
- public int validatePortNumber (String portNumberStr ) {
85
+ public boolean useSocket () {
86
+ return port != 0 ;
87
+ }
88
+
89
+ private int validatePortNumber (String portStr ) {
83
90
try {
84
- int portNumber = Integer .parseInt (portNumberStr );
91
+ int portNumber = Integer .parseInt (portStr );
85
92
if (portNumber < MIN_PORT || portNumber > MAX_PORT ) {
86
- throw new CliError ("Invalid port number!" );
93
+ throw new CliError ("Invalid port number: should be an integer between "
94
+ + MIN_PORT + " and " + MAX_PORT + ", inclusive." );
87
95
} else {
88
96
return portNumber ;
89
97
}
90
98
} catch (NumberFormatException e ) {
91
- throw new CliError ("Invalid port number!" );
99
+ throw new CliError ("Invalid port number: Can not parse " + portStr );
92
100
}
93
101
}
94
102
95
-
96
103
private void printHelp (Arguments arguments ) {
97
104
CliPrinter printer = CliPrinter .fromOutputStream (System .out );
98
- HelpPrinter helpPrinter = HelpPrinter .fromArguments ("java -jar smithy-lsp.jar " , arguments );
99
- helpPrinter .summary ("Options for the Smithy Language Server: " );
105
+ HelpPrinter helpPrinter = HelpPrinter .fromArguments ("smithy-language-server " , arguments );
106
+ helpPrinter .summary ("Run the Smithy Language Server. " );
100
107
helpPrinter .print (AnsiColorFormatter .AUTO , printer );
101
108
printer .flush ();
102
109
}
0 commit comments