Change: Move tests to a separate file, set up exporting from arg-parser.js
This commit is contained in:
parent
bd2a0c300d
commit
79c5473bf0
|
|
@ -5,28 +5,8 @@ const test = require('node:test');
|
||||||
// TODO:
|
// TODO:
|
||||||
// - building a structured output
|
// - building a structured output
|
||||||
// - testing
|
// - testing
|
||||||
// - exporting as object/library
|
|
||||||
|
|
||||||
|
|
||||||
// Testing -- constructors
|
|
||||||
|
|
||||||
let cpuArguments = new Arguments();
|
|
||||||
cpuArguments.add(
|
|
||||||
new Arg({name: 'Filename', shortFlag: 'f', requiresValue: true}),
|
|
||||||
new Arg({name: 'Clock speed', shortFlag: 'c', longFlag: 'clock', requiresValue: true, optional: true}),
|
|
||||||
new Arg({name: 'Debug', shortFlag: 'd', longFlag: 'debug', optional: true}),
|
|
||||||
new Arg({name: 'Pretty-print display', shortFlag: 'p', longFlag: 'pretty', optional: true}),
|
|
||||||
new Arg({name: 'Single-step', shortFlag: 's', longFlag: 'step', optional: true}));
|
|
||||||
|
|
||||||
|
|
||||||
// Constructors
|
|
||||||
|
|
||||||
function Arguments(argumentsObject=[], helpText='') {
|
|
||||||
this.help = helpText;
|
|
||||||
this.args = argumentsObject;
|
|
||||||
Arguments.prototype.add = (...a) => a.forEach((x) => argumentsObject.push(x));
|
|
||||||
};
|
|
||||||
|
|
||||||
function Arg({
|
function Arg({
|
||||||
name = 'Flag',
|
name = 'Flag',
|
||||||
shortFlag = null,
|
shortFlag = null,
|
||||||
|
|
@ -41,10 +21,15 @@ function Arg({
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// MODULE
|
function Arguments(argumentsObject=[], helpText='') {
|
||||||
|
this.help = helpText;
|
||||||
|
this.args = argumentsObject;
|
||||||
|
|
||||||
const argsRequiringValues = cpuArguments.args.filter((a) => a.requiresValue);
|
Arguments.prototype.add = (...a) => a.forEach((x) => argumentsObject.push(new Arg(x)));
|
||||||
const requiredArgs = cpuArguments.args.filter((a) => !a.optional);
|
|
||||||
|
this.parse = function() {
|
||||||
|
const argsRequiringValues = this.args.filter((a) => a.requiresValue);
|
||||||
|
const requiredArgs = this.args.filter((a) => !a.optional);
|
||||||
|
|
||||||
let argv = process.argv.slice(2);
|
let argv = process.argv.slice(2);
|
||||||
|
|
||||||
|
|
@ -92,4 +77,12 @@ argvSplit.forEach((av, index) => {
|
||||||
if (typeof argvSplit[index + 1] === 'undefined') { throw new Error(errorMessage); }
|
if (typeof argvSplit[index + 1] === 'undefined') { throw new Error(errorMessage); }
|
||||||
else if (argvSplit[index + 1].startsWith('-')) { throw new Error(errorMessage); };
|
else if (argvSplit[index + 1].startsWith('-')) { throw new Error(errorMessage); };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO return something useful
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
Arguments: Arguments,
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
const argParser = require('./arg-parser.js')
|
||||||
|
|
||||||
|
// TODO...
|
||||||
|
|
||||||
|
console.log(argParser);
|
||||||
|
|
||||||
|
let cpuArguments = new argParser.Arguments();
|
||||||
|
cpuArguments.add(
|
||||||
|
{name: 'Filename', shortFlag: 'f', requiresValue: true},
|
||||||
|
{name: 'Clock speed', shortFlag: 'c', longFlag: 'clock', requiresValue: true, optional: true},
|
||||||
|
{name: 'Debug', shortFlag: 'd', longFlag: 'debug', optional: true},
|
||||||
|
{name: 'Pretty-print display', shortFlag: 'p', longFlag: 'pretty', optional: true},
|
||||||
|
{name: 'Single-step', shortFlag: 's', longFlag: 'step', optional: true});
|
||||||
|
|
||||||
|
console.log(cpuArguments); //.parse();
|
||||||
|
console.log(cpuArguments.parse()); //.parse();
|
||||||
Loading…
Reference in New Issue