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.
This commit is contained in:
Bob Nystrom
2016-01-22 07:57:26 -08:00
parent 860220869a
commit 71e2458a6c
14 changed files with 211 additions and 4 deletions
+8 -4
View File
@@ -1,22 +1,26 @@
#include <stdio.h>
#include <string.h>
#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]);
}
+7
View File
@@ -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)
+23
View File
@@ -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);
}
}
+9
View File
@@ -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
+6
View File
@@ -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
}
+7
View File
@@ -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";