Skip to content

Commit

Permalink
add -c flag for Starlark-in-java
Browse files Browse the repository at this point in the history
**Related issues:**
- add -c flag [#35](bazelbuild/starlark#35)
- improve starlark-in-java installation documentation [#24](bazelbuild/starlark#24)

**Examples:**
$ bazel run :Starlark -- -c "print(1)"
1
$ bazel run :Starlark -- -c "print(1)" arg3
USAGE: Starlark [-c "\<cmdLineProgram\>" | \<fileName\>]

Closes #7683.

PiperOrigin-RevId: 239028987
  • Loading branch information
Quarz0 authored and copybara-github committed Mar 18, 2019
1 parent 4d65f44 commit 9303df6
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/main/java/com/google/devtools/starlark/Starlark.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,20 @@ public void readEvalPrintLoop() {
}

/** Execute a Starlark file. */
public int execute(String path) {
public int executeFile(String path) {
String content;
try {
content = new String(Files.readAllBytes(Paths.get(path)), CHARSET);
return execute(content);
} catch (Exception e) {
e.printStackTrace();
return 1;
}
}

/** Execute a Starlark command. */
public int execute(String content) {
try {
BuildFileAST.eval(env, content);
return 0;
} catch (EvalException e) {
Expand All @@ -119,10 +129,12 @@ public static void main(String[] args) {
int ret = 0;
if (args.length == 0) {
new Starlark().readEvalPrintLoop();
} else if (args.length == 1) {
ret = new Starlark().execute(args[0]);
} else if (args.length == 1 && !args[0].equals("-c")) {
ret = new Starlark().executeFile(args[0]);
} else if (args.length == 2 && args[0].equals("-c")) {
ret = new Starlark().execute(args[1]);
} else {
System.err.println("too many arguments");
System.err.println("USAGE: Starlark [-c \"<cmdLineProgram>\" | <fileName>]");
ret = 1;
}
System.exit(ret);
Expand Down

0 comments on commit 9303df6

Please sign in to comment.