Skip to content

Commit ac9e3f1

Browse files
author
a5right
committed
simple cmd implementation
1 parent 7b2a484 commit ac9e3f1

File tree

12 files changed

+542
-0
lines changed

12 files changed

+542
-0
lines changed

Diff for: .gitattributes

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+

Diff for: .gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
.idea
4+
5+
# Ignore Gradle build output directory
6+
build
7+
app/bin

Diff for: app/build.gradle

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
6+
* User Manual available at https://docs.gradle.org/8.0.2/userguide/building_java_projects.html
7+
*/
8+
9+
plugins {
10+
// Apply the application plugin to add support for building a CLI application in Java.
11+
id 'application'
12+
id 'java'
13+
}
14+
15+
16+
sourceCompatibility = "1.8";
17+
18+
19+
repositories {
20+
// Use Maven Central for resolving dependencies.
21+
mavenCentral()
22+
}
23+
24+
dependencies {
25+
// Use JUnit Jupiter for testing.
26+
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
27+
28+
// This dependency is used by the application.
29+
// implementation 'com.google.guava:guava:31.1-jre'
30+
31+
implementation 'commons-cli:commons-cli:1.4'
32+
}
33+
34+
application {
35+
// Define the main class for the application.
36+
mainClass = 'write.your.own.jvm.App'
37+
}
38+
39+
tasks.named('test') {
40+
// Use JUnit Platform for unit tests.
41+
useJUnitPlatform()
42+
}
43+
44+
if (hasProperty('buildScan')) {
45+
buildScan {
46+
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
47+
termsOfServiceAgree = 'yes'
48+
}
49+
}

Diff for: app/src/main/java/write/your/own/jvm/Cmd.java

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package write.your.own.jvm;
2+
3+
4+
import org.apache.commons.cli.*;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
10+
public class Cmd {
11+
private final String classpath;
12+
private final String mainClass;
13+
private final List<String> args;
14+
15+
public Cmd(String classpath, String mainClass, List<String> args) {
16+
this.classpath = classpath;
17+
this.mainClass = mainClass;
18+
this.args = args;
19+
}
20+
21+
public static Cmd parseArgs(String[] args) {
22+
// create Options object
23+
Options options = new Options();
24+
// add option
25+
options.addOption("h", false, "print help message");
26+
options.addOption("v", false, "print version");
27+
// -cp equals -classpath
28+
options.addOption("cp", true, "class path where to find");
29+
30+
try {
31+
// Create a parser
32+
CommandLineParser parser = new DefaultParser();
33+
// parse the options passed as command line arguments
34+
CommandLine commandLine = parser.parse(options, args);
35+
if (commandLine.hasOption("h")) {
36+
printHelp();
37+
System.exit(0);
38+
}
39+
40+
if (commandLine.hasOption("v")) {
41+
printVersion();
42+
}
43+
44+
if (commandLine.hasOption("cp")) {
45+
printClasspath(commandLine.getOptionValue("cp"));
46+
}
47+
String cp = commandLine.getOptionValue("cp");
48+
String[] leftArgs = commandLine.getArgs();
49+
return new Cmd(cp, leftArgs[0], Arrays.asList(leftArgs).subList(1, leftArgs.length));
50+
} catch (ParseException e) {
51+
e.printStackTrace();
52+
printHelp();
53+
}
54+
return null;
55+
}
56+
57+
private static void printClasspath(String classpath) {
58+
Log.d("classpath: " + classpath);
59+
}
60+
61+
private static void printVersion() {
62+
Log.d("version: 0.1.0");
63+
}
64+
65+
private static void printHelp() {
66+
Log.d("usage: <mc> [args] [-options]");
67+
Log.d("mc: main class name");
68+
Log.d("args: main class args");
69+
Log.d(" -h usage help");
70+
Log.d(" -v current version");
71+
Log.d(" -cp class path which need to load");
72+
}
73+
74+
public String getClasspath() {
75+
return classpath;
76+
}
77+
78+
public String getMainClass() {
79+
return mainClass;
80+
}
81+
82+
public List<String> getArgs() {
83+
return args;
84+
}
85+
}

Diff for: app/src/main/java/write/your/own/jvm/Log.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package write.your.own.jvm;
2+
3+
public class Log {
4+
public static void d(String message) {
5+
System.out.println(message);
6+
}
7+
8+
}

Diff for: app/src/main/java/write/your/own/jvm/Main.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This Java source file was generated by the Gradle 'init' task.
3+
*/
4+
package write.your.own.jvm;
5+
6+
public class Main {
7+
public static void main(String[] args) {
8+
Cmd cmd = Cmd.parseArgs(args);
9+
printArgs(cmd);
10+
}
11+
12+
private static void printArgs(Cmd cmd) {
13+
Log.d("classpath: " + cmd.getClasspath());
14+
Log.d("main class: " + cmd.getMainClass());
15+
Log.d("args: " + cmd.getArgs().toString());
16+
}
17+
}

Diff for: app/src/test/java/write/your/own/jvm/MainTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* This Java source file was generated by the Gradle 'init' task.
3+
*/
4+
package write.your.own.jvm;
5+
6+
import org.junit.jupiter.api.Test;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class MainTest {
10+
@Test void appHasAGreeting() {
11+
// App classUnderTest = new App();
12+
// assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
13+
}
14+
}

Diff for: gradle/wrapper/gradle-wrapper.jar

60.2 KB
Binary file not shown.

Diff for: gradle/wrapper/gradle-wrapper.properties

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
4+
networkTimeout=10000
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)