Skip to content

Commit 7d90781

Browse files
committed
feat: change cli library
1 parent 6ae20ca commit 7d90781

File tree

11 files changed

+74
-80
lines changed

11 files changed

+74
-80
lines changed

backend/common/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
dependencies {
8-
api "commons-cli:commons-cli:1.9.0"
8+
api 'info.picocli:picocli:4.7.6'
99
implementation project(":core")
1010
implementation group: 'com.github.albfernandez', name: 'juniversalchardet', version: '2.4.0'
1111
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package com.github.kayjamlang.backend;
22

3-
import org.apache.commons.cli.CommandLine;
4-
import org.apache.commons.cli.CommandLineParser;
5-
import org.apache.commons.cli.Options;
6-
73
public interface IBackendCompiler {
8-
void addOptions(Options options);
9-
IBackendOptions parseOptions(CommandLine data);
4+
IBackendOptions createOptionsClass();
105
void compile(IOptions options) throws Exception;
116
}

backend/jvm/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies {
88
implementation project(":core")
99
implementation project(":backend:common")
1010
implementation 'org.ow2.asm:asm:9.7.1'
11+
annotationProcessor 'info.picocli:picocli-codegen:4.7.6'
1112
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
1213
testImplementation project(':core')
1314
}

backend/jvm/src/main/java/com/github/kayjamlang/backend/jvm/JVMBackendCompiler.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import com.github.kayjamlang.core.KayJamFile;
88
import com.github.kayjamlang.core.exceptions.KayJamLexerException;
99
import com.github.kayjamlang.core.exceptions.KayJamParserException;
10-
import org.apache.commons.cli.CommandLine;
11-
import org.apache.commons.cli.CommandLineParser;
12-
import org.apache.commons.cli.Options;
1310
import org.objectweb.asm.ClassWriter;
1411

1512
import java.io.File;
@@ -22,13 +19,8 @@ public class JVMBackendCompiler implements IBackendCompiler {
2219
public static final JVMBackendCompiler INSTANCE = new JVMBackendCompiler();
2320

2421
@Override
25-
public void addOptions(Options options) {
26-
27-
}
28-
29-
@Override
30-
public IBackendOptions parseOptions(CommandLine data) {
31-
return null;
22+
public IBackendOptions createOptionsClass() {
23+
return new JVMBackendOptions();
3224
}
3325

3426
@Override
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.kayjamlang.backend.jvm;
2+
3+
import com.github.kayjamlang.backend.IBackendOptions;
4+
import picocli.CommandLine;
5+
6+
@CommandLine.Command(name = "jvm", mixinStandardHelpOptions = true)
7+
public class JVMBackendOptions implements IBackendOptions {
8+
9+
}

backend/ts/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
dependencies {
88
implementation project(":core")
99
implementation project(":backend:common")
10+
annotationProcessor 'info.picocli:picocli-codegen:4.7.6'
1011
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
1112
testImplementation project(':core')
1213
}

backend/ts/src/main/java/com/github/kayjamlang/backend/ts/TSBackendCompiler.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,18 @@
77
import com.github.kayjamlang.core.KayJamFile;
88
import com.github.kayjamlang.core.exceptions.KayJamLexerException;
99
import com.github.kayjamlang.core.exceptions.KayJamParserException;
10-
import org.apache.commons.cli.CommandLine;
11-
import org.apache.commons.cli.CommandLineParser;
12-
import org.apache.commons.cli.Options;
1310

1411
import java.io.File;
1512
import java.io.FileOutputStream;
1613
import java.io.IOException;
1714
import java.util.List;
18-
import java.util.Map;
1915

2016
public class TSBackendCompiler implements IBackendCompiler {
2117
public static final TSBackendCompiler INSTANCE = new TSBackendCompiler();
2218

2319
@Override
24-
public void addOptions(Options options) {
25-
26-
}
27-
28-
@Override
29-
public IBackendOptions parseOptions(CommandLine data) {
30-
return null;
20+
public IBackendOptions createOptionsClass() {
21+
return new TSBackendOptions();
3122
}
3223

3324
@Override
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.kayjamlang.backend.ts;
2+
3+
import com.github.kayjamlang.backend.IBackendOptions;
4+
import picocli.CommandLine;
5+
6+
@CommandLine.Command(name = "ts", mixinStandardHelpOptions = true)
7+
public class TSBackendOptions implements IBackendOptions {
8+
9+
}

cli/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ dependencies {
2424
implementation project(":backend:common")
2525
implementation project(":backend:jvm")
2626
implementation project(":backend:ts")
27-
implementation "commons-cli:commons-cli:1.9.0"
27+
implementation 'info.picocli:picocli:4.7.6'
28+
annotationProcessor 'info.picocli:picocli-codegen:4.7.6'
2829
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
2930
testImplementation project(':core')
3031
}

cli/scripts/kayjam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env sh
2-
java -jar /usr/share/kayjam/kayjam-cli.jar "$*"
2+
java -jar /usr/share/kayjam/kayjam-cli.jar $*

cli/src/main/java/com/github/kayjamlang/cli/MainCli.java

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,66 @@
44
import com.github.kayjamlang.backend.IBackendOptions;
55
import com.github.kayjamlang.backend.jvm.JVMBackendCompiler;
66
import com.github.kayjamlang.backend.ts.TSBackendCompiler;
7-
import org.apache.commons.cli.*;
7+
import picocli.CommandLine;
8+
import picocli.CommandLine.Command;
9+
import picocli.CommandLine.Option;
10+
import picocli.CommandLine.Parameters;
811

912
import java.io.File;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
1015
import java.util.HashMap;
1116
import java.util.Map;
17+
import java.util.concurrent.Callable;
1218

13-
public class MainCli {
14-
private static final Map<String, IBackendCompiler> compilers = new HashMap<>();
19+
@Command(name = "kayjam", mixinStandardHelpOptions = true)
20+
public class MainCli implements Callable<Integer> {
21+
private final Map<String, IBackendCompiler> compilers = new HashMap<>();
22+
private final String compiler;
23+
private final String[] args;
1524

16-
static {
17-
compilers.put("jvm", JVMBackendCompiler.INSTANCE);
18-
compilers.put("ts", TSBackendCompiler.INSTANCE);
19-
}
25+
@Option(names = {"-o", "--output"}, description = "Folder for output compiled files", required = true)
26+
private String output;
2027

21-
public static void main(String[] args) {
22-
Option typeOption = new Option("t", "type", true, "Backend type: " + compilers.keySet());
23-
typeOption.setRequired(true);
28+
@Option(names = {"-i", "--input"}, description = "Folder for input kayjam files", required = true)
29+
private String input;
2430

25-
Option outputOption = new Option("o", "output", true, "Output folder");
26-
outputOption.setRequired(true);
2731

28-
Option inputOption = new Option("i", "input", true, "Input folder");
29-
inputOption.setRequired(true);
32+
public MainCli(String compiler, String[] args) {
33+
this.compiler = compiler;
34+
this.args = args;
35+
compilers.put("jvm", JVMBackendCompiler.INSTANCE);
36+
compilers.put("ts", TSBackendCompiler.INSTANCE);
37+
}
3038

31-
Options options = new Options();
32-
options.addOption(typeOption);
33-
options.addOption(outputOption);
34-
options.addOption(inputOption);
39+
@Override
40+
public Integer call() throws Exception {
41+
if (compilers.containsKey(compiler)) {
42+
IBackendCompiler backendCompiler = compilers.get(compiler);
43+
IBackendOptions backendOptions = backendCompiler.createOptionsClass();
3544

36-
CommandLineParser cmdLinePosixParser = new DefaultParser();
37-
try {
38-
CommandLine commandLine = cmdLinePosixParser.parse(options, args);
39-
String type = commandLine.getOptionValue(typeOption).replace(" ", "");
40-
System.out.println(compilers.get(type));
41-
System.out.println("\'"+type+"\'");
42-
if (compilers.containsKey(type)) {
43-
IBackendCompiler backendCompiler = compilers.get(type);
44-
backendCompiler.addOptions(options);
45+
CommandLine line = new CommandLine(backendOptions);
46+
line.setUnmatchedArgumentsAllowed(true);
47+
line.parseArgs(args);
4548

46-
commandLine = cmdLinePosixParser.parse(options, args);
47-
IBackendOptions backendOptions = backendCompiler.parseOptions(commandLine);
48-
CliOptions cliOptions = new CliOptions(
49-
backendOptions,
50-
new File(commandLine.getOptionValue(outputOption)),
51-
new File(commandLine.getOptionValue(inputOption))
52-
);
49+
CliOptions cliOptions = new CliOptions(
50+
backendOptions,
51+
new File(output),
52+
new File(input)
53+
);
5354

54-
backendCompiler.compile(cliOptions);
55-
} else {
56-
System.err.println("Unknown backend type: " + type);
57-
printHelp(options);
58-
System.exit(1);
59-
}
60-
} catch (ParseException e) {
61-
printHelp(options);
62-
System.exit(1);
63-
} catch (Exception e) {
64-
e.printStackTrace();
65-
System.exit(1);
55+
backendCompiler.compile(cliOptions);
56+
} else {
57+
System.err.println("Unknown backend type: " + compiler);
58+
return 1;
6659
}
60+
61+
return 0;
6762
}
6863

69-
public static void printHelp(final Options options) {
70-
final String commandLineSyntax = "kayjam -t jvm -i input/ -o output/";
71-
final HelpFormatter helpFormatter = new HelpFormatter();
72-
helpFormatter.printHelp(commandLineSyntax, options);
64+
public static void main(String[] args) {
65+
int exitCode = new CommandLine(new MainCli(args[0], Arrays.copyOfRange(args, 1, args.length)))
66+
.execute(Arrays.copyOfRange(args, 1, args.length));
67+
System.exit(exitCode);
7368
}
7469
}

0 commit comments

Comments
 (0)