diff --git a/src-tools/lib/util/cmdline.js b/src-tools/lib/util/cmdline.js index 7a167b19..cf115a4e 100644 --- a/src-tools/lib/util/cmdline.js +++ b/src-tools/lib/util/cmdline.js @@ -97,6 +97,17 @@ function addOptionValue(optSpec, opts, name, value) { } } +// Check required options. +function checkRequiredOptions(spec, opts) { + let options = spec.options || {}; + for (let optName of Object.getOwnPropertyNames(options || {}).sort()) { + let opt = options[optName]; + if (opt.required && !(optName in opts)) { + throw new TypeError('missing required option --' + optName); + } + } +} + // Parse command line given a clean argv (excluding nodejs etc) and a // global command/option specification object. function parseCommandLine(argv, argSpec) { @@ -104,7 +115,7 @@ function parseCommandLine(argv, argSpec) { var command = void 0; var commandOpts = void 0; var commandPositional = []; - var genericSpec = argSpec.options; + var genericSpec = assert(argSpec); var commandSpec = void 0; function parseLongOption(i, t) { @@ -232,6 +243,14 @@ function parseCommandLine(argv, argSpec) { } } + // Check required options. + if (genericSpec) { + checkRequiredOptions(genericSpec, genericOpts); + } + if (commandSpec) { + checkRequiredOptions(commandSpec, commandOpts); + } + return { genericOpts, command, commandOpts, commandPositional }; } exports.parseCommandLine = parseCommandLine; diff --git a/src-tools/lib/util/fs.js b/src-tools/lib/util/fs.js index a58f094c..d664f66a 100644 --- a/src-tools/lib/util/fs.js +++ b/src-tools/lib/util/fs.js @@ -61,6 +61,10 @@ function pathJoinTest() { // > require('path').join('foo/bar/') // 'foo/bar/' + if (!isDuktape()) { + return; + } + assert(pathJoin('foo/bar/quux.c', '..', 'baz') === 'foo/bar/baz'); assert(pathJoin('foo/bar/', '..', 'baz') === 'foo/baz'); assert(pathJoin('foo/bar', '..', 'baz') === 'foo/baz'); @@ -135,6 +139,10 @@ function dirnameTest() { // > require('path').dirname('foo') // '.' + if (!isDuktape()) { + return; + } + assert(dirname('foo/bar') === 'foo'); assert(dirname('foo/bar/') === 'foo'); assert(dirname('foo/bar//') === 'foo'); @@ -186,6 +194,10 @@ function basenameTest() { // > require('path').basename('foo/..') // '..' + if (!isDuktape()) { + return; + } + assert(basename('foo') === 'foo'); assert(basename('foo/') === 'foo'); assert(basename('foo/bar') === 'bar'); @@ -248,6 +260,11 @@ function readFileYaml(fn) { } exports.readFileYaml = readFileYaml; +function writeFileJson(fn, doc) { + writeFileUtf8(fn, JSON.stringify(doc) + '\n'); +} +exports.writeFileJson = writeFileJson; + function writeFileJsonPretty(fn, doc) { writeFileUtf8(fn, JSON.stringify(doc, null, 4) + '\n'); }