From 71e2458a6c4adb477b0ab7db773a8e0116637367 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Fri, 22 Jan 2016 07:57:26 -0800 Subject: [PATCH] Add API for accessing command-line arguments. - Add process module with Process class. - Add "arguments" and "allArguments" methods. - Docs for same. - Support passing additional arguments to command line. - Add "--help" support to command line. --- doc/site/modules/index.markdown | 1 + doc/site/modules/process/index.markdown | 5 ++ doc/site/modules/process/process.markdown | 38 ++++++++++++ doc/site/modules/process/template.html | 74 +++++++++++++++++++++++ doc/site/modules/template.html | 2 + src/cli/main.c | 12 ++-- src/cli/modules.c | 7 +++ src/module/process.c | 23 +++++++ src/module/process.h | 9 +++ src/module/process.wren | 6 ++ src/module/process.wren.inc | 7 +++ test/process/all_arguments.wren | 11 ++++ test/process/arguments.wren | 6 ++ util/xcode/wren.xcodeproj/project.pbxproj | 14 +++++ 14 files changed, 211 insertions(+), 4 deletions(-) create mode 100644 doc/site/modules/process/index.markdown create mode 100644 doc/site/modules/process/process.markdown create mode 100644 doc/site/modules/process/template.html create mode 100644 src/module/process.c create mode 100644 src/module/process.h create mode 100644 src/module/process.wren create mode 100644 src/module/process.wren.inc create mode 100644 test/process/all_arguments.wren create mode 100644 test/process/arguments.wren diff --git a/doc/site/modules/index.markdown b/doc/site/modules/index.markdown index 0b6948ec..dff56ecc 100644 --- a/doc/site/modules/index.markdown +++ b/doc/site/modules/index.markdown @@ -44,5 +44,6 @@ applications that want to embed Wren. [libuv]: http://libuv.org * [io](io) +* [process](process) * [scheduler](scheduler) * [timer](timer) diff --git a/doc/site/modules/process/index.markdown b/doc/site/modules/process/index.markdown new file mode 100644 index 00000000..4475cf42 --- /dev/null +++ b/doc/site/modules/process/index.markdown @@ -0,0 +1,5 @@ +^title Module "process" + +The process module exposes a single class for working with operating processes. + +* [Process](process.html) diff --git a/doc/site/modules/process/process.markdown b/doc/site/modules/process/process.markdown new file mode 100644 index 00000000..ff3787ea --- /dev/null +++ b/doc/site/modules/process/process.markdown @@ -0,0 +1,38 @@ +^title Process Class + +The Process class lets you work with operating processes, including the +currently running one. + +## Static Methods + +### **allArguments** + +The list of command-line arguments that were passed when the Wren process was +spawned. This includes the Wren executable itself, the path to the file being +run (if any), and any other options passed to Wren itself. + +If you run: + + :::bash + $ wren file.wren arg + +This returns: + + :::wren + System.print(Process.allArguments) //> ["wren", "file.wren", "arg"] + +### **arguments** + +The list of command-line arguments that were passed to your program when the +Wren process was spawned. This does not include arguments handled by Wren +itself. + +If you run: + + :::bash + $ wren file.wren arg + +This returns: + + :::wren + System.print(Process.arguments) //> ["arg"] diff --git a/doc/site/modules/process/template.html b/doc/site/modules/process/template.html new file mode 100644 index 00000000..5d26689b --- /dev/null +++ b/doc/site/modules/process/template.html @@ -0,0 +1,74 @@ + + + + +{title} – Wren + + + + + + +
+
+
+

wren

+

a classy little scripting language

+
+
+
+
+ + +
+

{title}

+ {html} +
+
+ + + diff --git a/doc/site/modules/template.html b/doc/site/modules/template.html index 2f34e82c..7dd9550e 100644 --- a/doc/site/modules/template.html +++ b/doc/site/modules/template.html @@ -37,6 +37,7 @@

cli

@@ -64,6 +65,7 @@ diff --git a/src/cli/main.c b/src/cli/main.c index beb2f2f5..8e280179 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -1,22 +1,26 @@ #include #include +#include "process.h" #include "vm.h" #include "wren.h" int main(int argc, const char* argv[]) { - if (argc < 1 || argc > 2) + if (argc == 2 && strcmp(argv[1], "--help") == 0) { - fprintf(stderr, "Usage: wren [file]\n"); - return 64; // EX_USAGE. + printf("Usage: wren [file] [arguments...]\n"); + printf(" --help Show command line usage\n"); + return 0; } + + processSetArguments(argc, argv); if (argc == 1) { runRepl(); } - else if (argc == 2) + else { runFile(argv[1]); } diff --git a/src/cli/modules.c b/src/cli/modules.c index 024156e3..a0cdf5d4 100644 --- a/src/cli/modules.c +++ b/src/cli/modules.c @@ -4,6 +4,7 @@ #include "modules.h" #include "io.wren.inc" +#include "process.wren.inc" #include "scheduler.wren.inc" #include "timer.wren.inc" @@ -17,6 +18,7 @@ extern void fileClose(WrenVM* vm); extern void fileDescriptor(WrenVM* vm); extern void fileReadBytes(WrenVM* vm); extern void fileSize(WrenVM* vm); +extern void processAllArguments(WrenVM* vm); extern void stdinReadStart(WrenVM* vm); extern void stdinReadStop(WrenVM* vm); extern void schedulerCaptureMethods(WrenVM* vm); @@ -109,6 +111,11 @@ static ModuleRegistry modules[] = STATIC_METHOD("readStop_()", stdinReadStop) END_CLASS END_MODULE + MODULE(process) + CLASS(Process) + STATIC_METHOD("allArguments", processAllArguments) + END_CLASS + END_MODULE MODULE(scheduler) CLASS(Scheduler) STATIC_METHOD("captureMethods_()", schedulerCaptureMethods) diff --git a/src/module/process.c b/src/module/process.c new file mode 100644 index 00000000..435e22a0 --- /dev/null +++ b/src/module/process.c @@ -0,0 +1,23 @@ +#include "process.h" +#include "wren.h" + +int numArgs; +const char** args; + +void processSetArguments(int argc, const char* argv[]) +{ + numArgs = argc; + args = argv; +} + +void processAllArguments(WrenVM* vm) +{ + wrenEnsureSlots(vm, 2); + wrenSetSlotNewList(vm, 0); + + for (int i = 0; i < numArgs; i++) + { + wrenSetSlotString(vm, 1, args[i]); + wrenInsertInList(vm, 0, -1, 1); + } +} \ No newline at end of file diff --git a/src/module/process.h b/src/module/process.h new file mode 100644 index 00000000..3b01f702 --- /dev/null +++ b/src/module/process.h @@ -0,0 +1,9 @@ +#ifndef process_h +#define process_h + +#include "wren.h" + +// Stores the command line arguments passed to the CLI. +void processSetArguments(int argc, const char* argv[]); + +#endif diff --git a/src/module/process.wren b/src/module/process.wren new file mode 100644 index 00000000..9f6b4fe9 --- /dev/null +++ b/src/module/process.wren @@ -0,0 +1,6 @@ +class Process { + // TODO: This will need to be smarter when wren supports CLI options. + static arguments { allArguments[2..-1] } + + foreign static allArguments +} diff --git a/src/module/process.wren.inc b/src/module/process.wren.inc new file mode 100644 index 00000000..87ef4e88 --- /dev/null +++ b/src/module/process.wren.inc @@ -0,0 +1,7 @@ +// Generated automatically from src/module/process.wren. Do not edit. +static const char* processModuleSource = +"class Process {\n" +" static arguments { allArguments[2..-1] }\n" +"\n" +" foreign static allArguments\n" +"}\n"; diff --git a/test/process/all_arguments.wren b/test/process/all_arguments.wren new file mode 100644 index 00000000..28bc744d --- /dev/null +++ b/test/process/all_arguments.wren @@ -0,0 +1,11 @@ +import "process" for Process + +var args = Process.allArguments +System.print(args is List) // expect: true +System.print(args.count) // expect: 2 + +// Includes wren executable and file being run. +System.print(args[0].endsWith("wrend")) // expect: true +System.print(args[1]) // expect: test/process/all_arguments.wren + +// TODO: Test passing additional args once we have an API to spawn a process. diff --git a/test/process/arguments.wren b/test/process/arguments.wren new file mode 100644 index 00000000..9edb5178 --- /dev/null +++ b/test/process/arguments.wren @@ -0,0 +1,6 @@ +import "process" for Process + +// No additional arguments are passed to the test. +System.print(Process.arguments) // expect: [] + +// TODO: Test passing additional args once we have an API to spawn a process. diff --git a/util/xcode/wren.xcodeproj/project.pbxproj b/util/xcode/wren.xcodeproj/project.pbxproj index c250b022..7d129dc9 100644 --- a/util/xcode/wren.xcodeproj/project.pbxproj +++ b/util/xcode/wren.xcodeproj/project.pbxproj @@ -37,6 +37,10 @@ 29A4273A1BDBE435001E6E22 /* wren_opt_random.wren.inc in Sources */ = {isa = PBXBuildFile; fileRef = 29A427331BDBE435001E6E22 /* wren_opt_random.wren.inc */; }; 29A4273B1BDBE435001E6E22 /* wren_opt_random.wren.inc in Sources */ = {isa = PBXBuildFile; fileRef = 29A427331BDBE435001E6E22 /* wren_opt_random.wren.inc */; }; 29C8A9331AB71FFF00DEC81D /* vm.c in Sources */ = {isa = PBXBuildFile; fileRef = 29C8A9311AB71FFF00DEC81D /* vm.c */; }; + 29D025E31C19CD1000A3BB28 /* process.c in Sources */ = {isa = PBXBuildFile; fileRef = 29D025E01C19CD1000A3BB28 /* process.c */; }; + 29D025E41C19CD1000A3BB28 /* process.c in Sources */ = {isa = PBXBuildFile; fileRef = 29D025E01C19CD1000A3BB28 /* process.c */; }; + 29D025E51C19CD1000A3BB28 /* process.wren.inc in Sources */ = {isa = PBXBuildFile; fileRef = 29D025E21C19CD1000A3BB28 /* process.wren.inc */; }; + 29D025E61C19CD1000A3BB28 /* process.wren.inc in Sources */ = {isa = PBXBuildFile; fileRef = 29D025E21C19CD1000A3BB28 /* process.wren.inc */; }; 29DC149F1BBA2FCC008A8274 /* vm.c in Sources */ = {isa = PBXBuildFile; fileRef = 29C8A9311AB71FFF00DEC81D /* vm.c */; }; 29DC14A01BBA2FD6008A8274 /* timer.c in Sources */ = {isa = PBXBuildFile; fileRef = 2901D7621B74F4050083A2C8 /* timer.c */; }; 29DC14A11BBA2FEC008A8274 /* scheduler.c in Sources */ = {isa = PBXBuildFile; fileRef = 291647C21BA5EA45006142EE /* scheduler.c */; }; @@ -128,6 +132,9 @@ 29D009AB1B7E39A8000CE58C /* slots.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = slots.h; path = ../../test/api/slots.h; sourceTree = ""; }; 29D009AC1B7E39A8000CE58C /* value.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = value.c; path = ../../test/api/value.c; sourceTree = ""; }; 29D009AD1B7E39A8000CE58C /* value.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = value.h; path = ../../test/api/value.h; sourceTree = ""; }; + 29D025E01C19CD1000A3BB28 /* process.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = process.c; path = ../../src/module/process.c; sourceTree = ""; }; + 29D025E11C19CD1000A3BB28 /* process.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = process.h; path = ../../src/module/process.h; sourceTree = ""; }; + 29D025E21C19CD1000A3BB28 /* process.wren.inc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.pascal; name = process.wren.inc; path = ../../src/module/process.wren.inc; sourceTree = ""; }; 29F384111BD19706002F84E0 /* io.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = io.h; path = ../../src/module/io.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -157,6 +164,9 @@ 29F384111BD19706002F84E0 /* io.h */, 29729F2E1BA70A620099CA20 /* io.c */, 29729F301BA70A620099CA20 /* io.wren.inc */, + 29D025E11C19CD1000A3BB28 /* process.h */, + 29D025E01C19CD1000A3BB28 /* process.c */, + 29D025E21C19CD1000A3BB28 /* process.wren.inc */, 291647C31BA5EA45006142EE /* scheduler.h */, 291647C21BA5EA45006142EE /* scheduler.c */, 291647CD1BA5ED26006142EE /* scheduler.wren.inc */, @@ -354,6 +364,8 @@ 29A427341BDBE435001E6E22 /* wren_opt_meta.c in Sources */, 29205C9B1AB4E6430073018D /* wren_debug.c in Sources */, 29205C9D1AB4E6430073018D /* wren_utils.c in Sources */, + 29D025E51C19CD1000A3BB28 /* process.wren.inc in Sources */, + 29D025E31C19CD1000A3BB28 /* process.c in Sources */, 29729F311BA70A620099CA20 /* io.c in Sources */, 29A427361BDBE435001E6E22 /* wren_opt_meta.wren.inc in Sources */, 29205C9E1AB4E6430073018D /* wren_value.c in Sources */, @@ -367,6 +379,7 @@ buildActionMask = 2147483647; files = ( 29A427371BDBE435001E6E22 /* wren_opt_meta.wren.inc in Sources */, + 29D025E41C19CD1000A3BB28 /* process.c in Sources */, 29729F321BA70A620099CA20 /* io.c in Sources */, 29932D541C210F8D00099DEE /* lists.c in Sources */, 291647C81BA5EC5E006142EE /* modules.c in Sources */, @@ -383,6 +396,7 @@ 29DC14A71BBA301A008A8274 /* wren_utils.c in Sources */, 29DC14A81BBA301D008A8274 /* wren_value.c in Sources */, 29A4273B1BDBE435001E6E22 /* wren_opt_random.wren.inc in Sources */, + 29D025E61C19CD1000A3BB28 /* process.wren.inc in Sources */, 29DC14A91BBA302F008A8274 /* wren_vm.c in Sources */, 29DC14AA1BBA3032008A8274 /* main.c in Sources */, 293D46961BB43F9900200083 /* call.c in Sources */,