Skip to content

Commit

Permalink
Ensure proper handling of unknown option.
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoathaydes committed Apr 25, 2024
1 parent 207c1b1 commit 323b667
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.athaydes.jgrab.code.StringJavaCode;
import com.athaydes.jgrab.jbuild.JBuildGrabber;
import com.athaydes.jgrab.runner.Grabber;
import com.athaydes.jgrab.runner.JGrabOptions;
import com.athaydes.jgrab.runner.JGrabRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -103,8 +104,8 @@ public static void start( RunArgs runArgs ) {
continue;
}

if ( input.startsWith( JGrabRunner.SNIPPET_OPTION ) ) {
String snippet = input.substring( JGrabRunner.SNIPPET_OPTION.length() );
if ( input.startsWith( JGrabOptions.SNIPPET_OPTION ) ) {
String snippet = input.substring( JGrabOptions.SNIPPET_OPTION.length() );
if ( snippet.isEmpty() ) {
out.println( "ERROR: no snippet provided to execute" );
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.athaydes.jgrab.runner;

import java.io.File;

public abstract class JGrabOptions {
public static final String SNIPPET_OPTION = "-e";

static class Snippet extends JGrabOptions {
final String code;

public Snippet( String code ) {
this.code = code;
}
}

static class JavaFile extends JGrabOptions {
final File file;
final String[] args;

public JavaFile( File file, String[] args ) {
this.file = file;
this.args = args;
}
}

static class StdIn extends JGrabOptions {
}

static class Daemon extends JGrabOptions {
}

static class PrintVersion extends JGrabOptions {
}

static JGrabOptions parseOptions( String[] args ) {
if ( args.length == 0 ) {
return new StdIn();
}
if ( args.length == 1 ) {
if ( args[ 0 ].equals( "--daemon" ) || args[ 0 ].equals( "-d" ) ) {
return new Daemon();
}
if ( args[ 0 ].equals( "--help" ) || args[ 0 ].equals( "-h" ) ) {
return help();
}
if ( args[ 0 ].equals( "--version" ) || args[ 0 ].equals( "-v" ) ) {
return new PrintVersion();
}
}

String first = args[ 0 ];
String[] rest = new String[ args.length - 1 ];
System.arraycopy( args, 1, rest, 0, rest.length );

if ( first.equals( SNIPPET_OPTION ) ) {
String script = String.join( " ", rest );
return new JGrabOptions.Snippet( script );
}

if ( first.startsWith( "-" ) ) {
throw new JGrabError( "Unknown option: " + first );
}

return new JGrabOptions.JavaFile( new File( first ), rest );
}

private static JGrabOptions help() {
System.out.println( "=================== JGrab ===================\n" +
" - https://github.com/renatoathaydes/jgrab -\n" +
"=============================================\n" +
"Jgrab can execute Java code from stdin (if not given any argument),\n" +
"a Java file, or a Java snippet.\n\n" +
"Usage:\n" +
" jgrab [<option> | java_file [java-args*] | -e java_snippet]\n" +
"Options:\n" +
" --daemon -d\n" +
" Starts up the JGrab daemon (used by the jgrab-client).\n" +
" --help -h\n" +
" Shows usage.\n" +
" --version -v\n" +
" Shows version information." );

return null;
}
}
104 changes: 11 additions & 93 deletions jgrab-runner/src/main/java/com/athaydes/jgrab/runner/JGrabRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,11 @@ public class JGrabRunner {

private static final Map<String, ClassLoaderContext> classLoaderCache = new ConcurrentHashMap<>();

public static final String SNIPPET_OPTION = "-e";

private static JGrabOptions parseOptions( String[] args ) {
if ( args.length == 0 ) {
return new JGrabOptions.StdIn();
}
if ( args.length == 1 ) {
if ( args[ 0 ].equals( "--daemon" ) || args[ 0 ].equals( "-d" ) ) {
return new JGrabOptions.Daemon();
}
if ( args[ 0 ].equals( "--help" ) || args[ 0 ].equals( "-h" ) ) {
return help();
}
if ( args[ 0 ].equals( "--version" ) || args[ 0 ].equals( "-v" ) ) {
return version();
}
}

String first = args[ 0 ];
String[] rest = new String[ args.length - 1 ];
System.arraycopy( args, 1, rest, 0, rest.length );

if ( first.equals( SNIPPET_OPTION ) ) {
String script = String.join( " ", rest );
return new JGrabOptions.Snippet( script );
}

return new JGrabOptions.JavaFile( new File( first ), rest );
}

static void error( String reason ) {
throw new JGrabError( reason + "\n\nUsage: jgrab (-e <java_source>) | java_file" );
}

public static void printVersion() {
version();
}

static JGrabOptions version() {
URL jarUrl = JGrabRunner.class.getProtectionDomain().getCodeSource().getLocation();
String version = "UNKNOWN";

Expand All @@ -93,27 +59,6 @@ static JGrabOptions version() {

System.out.println( "JGrab Version: " + version );
System.out.println( "Java Version: " + System.getProperty( "java.version" ) );

return new JGrabOptions.None();
}

private static JGrabOptions help() {
System.out.println( "=================== JGrab ===================\n" +
" - https://github.com/renatoathaydes/jgrab -\n" +
"=============================================\n" +
"Jgrab can execute Java code from stdin (if not given any argument),\n" +
"a Java file, or a Java snippet.\n\n" +
"Usage:\n" +
" jgrab [<option> | java_file [java-args*] | -e java_snippet]\n" +
"Options:\n" +
" --daemon -d\n" +
" Starts up the JGrab daemon (used by the jgrab-client).\n" +
" --help -h\n" +
" Shows usage.\n" +
" --version -v\n" +
" Shows version information." );

return new JGrabOptions.None();
}

private static void run( String currentDir, JGrabOptions options ) throws Exception {
Expand All @@ -128,13 +73,11 @@ private static void run( String currentDir, JGrabOptions options ) throws Except
run( new StringJavaCode( ( ( JGrabOptions.Snippet ) options ).code ), new String[ 0 ] );
} else if ( options instanceof JGrabOptions.Daemon ) {
JGrabDaemon.start( JGrabRunner::run );
} else
//noinspection StatementWithEmptyBody
if ( options instanceof JGrabOptions.None ) {
// nothing to do
} else {
error( "Unknown JGrab option: " + options );
}
} else if ( options instanceof JGrabOptions.PrintVersion ) {
printVersion();
} else {
error( "Unknown JGrab option: " + options );
}
}

private static void run( JavaCode javaCode, String[] args ) {
Expand Down Expand Up @@ -253,13 +196,17 @@ private static Path getTempDir() {

private static void run( String currentDir, String[] args ) {
try {
JGrabOptions options = parseOptions( args );
run( currentDir, options );
var options = JGrabOptions.parseOptions( args );
if ( options != null ) {
run( currentDir, options );
}
} catch ( JGrabError e ) {
System.err.println( e.getMessage() );
System.exit( 1 );
} catch ( Exception e ) {
System.err.println( "Unable to run Java class due to " + e );
e.printStackTrace();
System.exit( 2 );
}
}

Expand All @@ -268,32 +215,3 @@ public static void main( String[] args ) {
}

}

abstract class JGrabOptions {
static class Snippet extends JGrabOptions {
final String code;

public Snippet( String code ) {
this.code = code;
}
}

static class JavaFile extends JGrabOptions {
final File file;
final String[] args;

public JavaFile( File file, String[] args ) {
this.file = file;
this.args = args;
}
}

static class StdIn extends JGrabOptions {
}

static class Daemon extends JGrabOptions {
}

static class None extends JGrabOptions {
}
}
14 changes: 12 additions & 2 deletions jgrab-test/src/test/java/com/athaydes/jgrab/test/JGrabTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.athaydes.jgrab.test;

import org.hamcrest.CoreMatchers;
import org.junit.Test;

import java.util.List;
import java.util.Objects;

import static com.athaydes.jgrab.test.JGrabTestRunner.jgrab;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

Expand All @@ -31,6 +31,8 @@ public void runRunnableClass() throws Exception {
"Hi interface org.apache.felix.shell.Command",
"Ola class jline.console.ConsoleReader" ),
result.stdout.lines().collect( toList() ) );

assertEquals( result.stderr.trim(), "" );
}

@Test
Expand All @@ -42,13 +44,21 @@ public void runScript() throws Exception {
assertEquals( "Result: " + result,
List.of( "5" ),
result.stdout.lines().collect( toList() ) );
assertEquals( result.stderr.trim(), "" );
}

@Test
public void runVersion() throws Exception {
var result = jgrab( "-v" );
result.assertOk();
assertThat( result.stdout, CoreMatchers.startsWith( "JGrab Version" ) );
assertThat( result.stdout, startsWith( "JGrab Version" ) );
assertEquals( result.stderr.trim(), "" );
}

@Test
public void unknownOption() throws Exception {
var result = jgrab( "-f" );
result.assertCode( 1 );
assertEquals( result.stderr.trim(), "Unknown option: -f" );
}
}

0 comments on commit 323b667

Please sign in to comment.