-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can load source files!
- Loading branch information
Showing
8 changed files
with
218 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PROGRAM testing <_< >_> >_> >_> >_> program_vége |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ppke.itk.xplang.ui; | ||
|
||
import net.sourceforge.argparse4j.ArgumentParsers; | ||
import net.sourceforge.argparse4j.impl.Arguments; | ||
import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
import net.sourceforge.argparse4j.inf.Namespace; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
class OptionParser { | ||
private static final Logger log = LoggerFactory.getLogger("Root.UI.ArgumentParser"); | ||
|
||
private final ArgumentParser parser; | ||
|
||
OptionParser() { | ||
parser = ArgumentParsers.newArgumentParser("xplang", true) | ||
.version(String.format("This is XPLanG version %s", Program.getVersion().describe())) | ||
.defaultHelp(true); | ||
|
||
parser.addArgument("source") | ||
.metavar("<source-file>") | ||
.type(Arguments.fileType().acceptSystemIn().verifyCanRead().verifyIsFile()) | ||
.nargs(1) | ||
.help("Source file"); | ||
|
||
parser.addArgument("--version") | ||
.action(Arguments.version()); // FIXME this exists with a System.exit(0). That's sort of not ideal. | ||
} | ||
|
||
RunConfig parseOptions(String[] args) { | ||
log.debug("Parsing command line arguments: {}", Arrays.asList(args)); | ||
|
||
try { | ||
Namespace res = parser.parseArgs(args); | ||
List<File> files = res.get("source"); | ||
return new RunConfig(Program.Action.getDefaultAction(), files.get(0)); | ||
} catch(ArgumentParserException e) { | ||
log.error("Argument error: {}", e.getMessage()); | ||
parser.handleError(e); | ||
return new RunConfig(Program.Action.NONE, null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ppke.itk.xplang.ui; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Configures the program's behaviour. | ||
*/ | ||
class RunConfig { | ||
private final Program.Action action; | ||
private final File sourceFile; | ||
|
||
RunConfig(Program.Action action, File sourceFile) { | ||
this.action = action; | ||
this.sourceFile = sourceFile; | ||
} | ||
|
||
/** | ||
* The course of action the program should take. | ||
* @return | ||
*/ | ||
Program.Action getAction() { | ||
return action; | ||
} | ||
|
||
/** | ||
* Where is the source code the program should operate on? | ||
* @return the File object representing the source code. The file is weakly guaranteed to exist and be readable. | ||
* If the {@code name} property of the File is set to '-', that is a signal the program should read the | ||
* source code from the standard input stream. | ||
*/ | ||
File getSourceFile() { | ||
return sourceFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package ppke.itk.xplang.ui; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertSame; | ||
|
||
public class OptionParserTest { | ||
private OptionParser parser; | ||
|
||
@Before | ||
public void setUp() { | ||
this.parser = new OptionParser(); | ||
} | ||
|
||
@Test | ||
public void getHelp() { | ||
RunConfig config = parser.parseOptions(new String[]{"--help"}); | ||
assertSame("Requesting the help screen should result in NONE action", | ||
Program.Action.NONE, config.getAction() | ||
); | ||
|
||
config = parser.parseOptions(new String[]{"--help", "example.plang"}); | ||
assertSame("The --help switch and a source file should result in NONE action", | ||
Program.Action.NONE, config.getAction() | ||
); | ||
} | ||
|
||
//@Test // FIXME don't run until --version does System.exit(1) | ||
public void getVersion() { | ||
RunConfig config = parser.parseOptions(new String[]{"--version"}); | ||
assertSame("Requesting the version screen should result in NONE action", | ||
Program.Action.NONE, config.getAction() | ||
); | ||
|
||
config = parser.parseOptions(new String[]{"--version", "example.prog"}); | ||
assertSame("The --version switch and a source file should result in NONE action", | ||
Program.Action.NONE, config.getAction() | ||
); | ||
} | ||
|
||
@Test | ||
public void specifyingSourceFiles() { | ||
RunConfig config = parser.parseOptions(new String[]{"example.prog"}); | ||
assertSame("Existing file as source input should be accepted, and should trigger the default action", | ||
Program.Action.getDefaultAction(), config.getAction() | ||
); | ||
assertEquals("Existing file as source input should be accepted.", | ||
"example.prog", config.getSourceFile().getName() | ||
); | ||
} | ||
|
||
@Test | ||
public void stdInSource() { | ||
RunConfig config = parser.parseOptions(new String[]{"-"}); | ||
assertSame("- as source input should be accepted, and should trigger the default action", | ||
Program.Action.getDefaultAction(), config.getAction() | ||
); | ||
assertEquals("- as source input should be accepted (representing the standard input)", | ||
new File("-"), config.getSourceFile() | ||
); | ||
} | ||
|
||
@Test | ||
public void errors() { | ||
RunConfig config = parser.parseOptions(new String[]{"--invalid", "example.prog"}); | ||
assertSame("Invalid options should result in a NONE action", | ||
Program.Action.NONE, config.getAction() | ||
); | ||
|
||
config = parser.parseOptions(new String[]{"nonesuch.prog"}); | ||
assertSame("Specifying nonexistent files should result in a NONE action", | ||
Program.Action.NONE, config.getAction() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this file exists |