Skip to content

Commit

Permalink
Merge pull request #866 from null-a/cmd-line-args
Browse files Browse the repository at this point in the history
Make command line args work for compiled programs.
  • Loading branch information
stuhlmueller committed Jul 12, 2017
2 parents c1d62c4 + 7189142 commit 0a4a6b3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ When run with::
Will produce the following output::

{ _: ['model.wppl'], 'my-flag': true, 'my-num': 100, 'my-str': 'hello' }

Once compiled, a program takes its arguments directly, i.e. the ``--``
separator is not required.
11 changes: 11 additions & 0 deletions src/args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var parseArgs = require('minimist');

function makeGlobal(programFile, argv) {
global.argv = parseArgs([programFile].concat(argv));
}

module.exports = {
makeGlobal: makeGlobal
};
5 changes: 4 additions & 1 deletion webppl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var parseV8 = require('./src/errors/parsers').parseV8;
var showError = require('./src/errors/node').showError;
var paramsConfig = require('./src/params/config');
var parseArgs = require('minimist');
var args = require('./src/args');
var _ = require('lodash');

function topK(s, x) {
Expand Down Expand Up @@ -56,6 +57,8 @@ var lines = function(ar) {

function compile(code, packages, verbose, debug, programFile, outputFile) {
var compiledCode = 'var webppl = require("' + require.resolve('./src/main') + '");\n';
compiledCode += 'var args = require("'+require.resolve('./src/args')+'");\n';
compiledCode += 'args.makeGlobal(__filename, process.argv.slice(2));\n';
packages.forEach(function(pkg) {
if (pkg.js) { compiledCode += 'var ' + pkg.js.identifier + ' = require("' + pkg.js.path + '");\n'; }
pkg.headers.forEach(function(header) {
Expand Down Expand Up @@ -150,7 +153,7 @@ function main() {
}

// Parse arguments for program.
global.argv = parseArgs([programFile].concat(argv['--']));
args.makeGlobal(programFile, argv['--']);

processCode(code, packages, argv.verbose, argv.debug, programFile, outputFile);
}
Expand Down

0 comments on commit 0a4a6b3

Please sign in to comment.