17
17
import io .clusterless .subpop .options .OutputOptions ;
18
18
import io .clusterless .subpop .util .Verbosity ;
19
19
import io .clusterless .subpop .util .VersionProvider ;
20
- import io .micronaut .configuration .picocli .PicocliRunner ;
21
20
import org .slf4j .Logger ;
22
21
import org .slf4j .LoggerFactory ;
23
22
import picocli .CommandLine ;
24
23
import picocli .CommandLine .Command ;
25
24
26
25
import java .io .IOException ;
26
+ import java .io .UncheckedIOException ;
27
+ import java .util .Arrays ;
27
28
import java .util .List ;
29
+ import java .util .concurrent .Callable ;
28
30
29
31
@ Command (
30
32
name = "subpop" ,
33
35
versionProvider = VersionProvider .class ,
34
36
sortOptions = false
35
37
)
36
- public class Main implements Runnable {
38
+ public class Main implements Callable < Integer > {
37
39
private static final Logger LOG = LoggerFactory .getLogger (Main .class );
38
40
39
41
@ CommandLine .Mixin
@@ -57,10 +59,50 @@ public class Main implements Runnable {
57
59
protected Float supportRatio = null ;
58
60
59
61
public static void main (String [] args ) throws Exception {
60
- PicocliRunner .run (Main .class , args );
62
+ Main main = new Main ();
63
+
64
+ CommandLine commandLine = new CommandLine (main );
65
+
66
+ try {
67
+ commandLine .parseArgs (args );
68
+ } catch (CommandLine .MissingParameterException | CommandLine .UnmatchedArgumentException e ) {
69
+ System .err .println (e .getMessage ());
70
+ commandLine .usage (System .out );
71
+ System .exit (-1 );
72
+ }
73
+
74
+ if (commandLine .isUsageHelpRequested ()) {
75
+ commandLine .usage (System .out );
76
+ return ;
77
+ } else if (commandLine .isVersionHelpRequested ()) {
78
+ commandLine .printVersionHelp (System .out );
79
+ return ;
80
+ }
81
+
82
+ int exitCode = 0 ;
83
+
84
+ try {
85
+ exitCode = commandLine .execute (args );
86
+ } catch (Exception e ) {
87
+ System .err .println (e .getMessage ());
88
+
89
+ if (main .verbosity .isVerbose ()) {
90
+ e .printStackTrace (System .err );
91
+ }
92
+
93
+ System .exit (-1 ); // get exit code from exception
94
+ }
95
+
96
+ System .exit (exitCode );
61
97
}
62
98
63
- public void run () {
99
+ @ Override
100
+ public Integer call () throws Exception {
101
+ if (inputOptions .inputs ().isEmpty () && hasStdIn () == 0 ) {
102
+ LOG .info ("no input data" );
103
+ throw new IllegalArgumentException ("no input data" );
104
+ }
105
+
64
106
ItemStoreReader itemStoreReader = ItemStoreReader .builder ()
65
107
.withSeparator (inputOptions .delimiter ())
66
108
.withHasHeader (inputOptions .hasHeader ())
@@ -75,10 +117,17 @@ public void run() {
75
117
.build ();
76
118
77
119
try {
78
- ItemStore itemStore = itemStoreReader .read (inputOptions .inputs ());
120
+ ItemStore itemStore ;
121
+ if (inputOptions .inputs ().isEmpty ()) {
122
+ LOG .info ("reading from stdin" );
123
+ itemStore = itemStoreReader .read (System .in );
124
+ } else {
125
+ LOG .info ("reading from files: {}" , inputOptions .inputs ());
126
+ itemStore = itemStoreReader .read (inputOptions .inputs ());
127
+ }
79
128
80
- if (!itemStore .containsAllClasses (classValue )) {
81
- throw new IllegalArgumentException ("class value not found" );
129
+ if (!itemStore .containsAllClasses (classValue )) {
130
+ throw new IllegalArgumentException ("class value not found: {}" + Arrays . toString ( classValue ) );
82
131
}
83
132
84
133
CPTree cpTree = new CPTree (itemStore );
@@ -92,5 +141,15 @@ public void run() {
92
141
} catch (CsvValidationException | IOException e ) {
93
142
throw new RuntimeException (e );
94
143
}
144
+
145
+ return 0 ;
146
+ }
147
+
148
+ private static int hasStdIn () {
149
+ try {
150
+ return System .in .available ();
151
+ } catch (IOException e ) {
152
+ throw new UncheckedIOException (e .getMessage (), e );
153
+ }
95
154
}
96
155
}
0 commit comments