Change: tidy up

This commit is contained in:
n loewen 2023-08-26 20:58:49 -04:00
parent 81b106088d
commit bd2a0c300d
1 changed files with 24 additions and 67 deletions

View File

@ -2,33 +2,21 @@
const test = require('node:test'); const test = require('node:test');
// WIP // TODO:
// - building a structured output
// - testing
// - exporting as object/library
// Example
/* // Testing -- constructors
let cpuArguments = {
help: '', // TODO let cpuArguments = new Arguments();
args: [ cpuArguments.add(
{ flags: { short: 'f' }, new Arg({name: 'Filename', shortFlag: 'f', requiresValue: true}),
name: 'Filename', new Arg({name: 'Clock speed', shortFlag: 'c', longFlag: 'clock', requiresValue: true, optional: true}),
requiresValue: true, }, new Arg({name: 'Debug', shortFlag: 'd', longFlag: 'debug', optional: true}),
{ name: 'Clock speed', new Arg({name: 'Pretty-print display', shortFlag: 'p', longFlag: 'pretty', optional: true}),
flags: { short: 'c', long: 'clock' }, new Arg({name: 'Single-step', shortFlag: 's', longFlag: 'step', optional: true}));
requiresValue: true,
optional: true },
{ name: 'Pretty-print display',
flags: { short: 'p', long: 'pretty' },
optional: true, },
{ name: 'Single-step',
flags: { short: 's', long: 'step' },
optional: true, },
{ 'name': 'Debug',
flags: { short: 'd', long: 'debug' },
optional: true,
}
]
};
*/
// Constructors // Constructors
@ -52,26 +40,8 @@ function Arg({
this.optional = optional; this.optional = optional;
}; };
// Test constructors
let cpuArguments = new Arguments(); // MODULE
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}));
///console.log(cpuArguments);
///console.log('flags', cpuArguments.args[0].flags);
// MODULE:
// TODO:
// - building a structured output
// - testing
// - exporting as object
const argsRequiringValues = cpuArguments.args.filter((a) => a.requiresValue); const argsRequiringValues = cpuArguments.args.filter((a) => a.requiresValue);
const requiredArgs = cpuArguments.args.filter((a) => !a.optional); const requiredArgs = cpuArguments.args.filter((a) => !a.optional);
@ -98,40 +68,27 @@ requiredArgs.forEach((a) => {
}); });
// Validate... // Validate...
argvSplit.forEach((currentArg, index) => { argvSplit.forEach((av, index) => {
console.log('checking', currentArg);
// Check for attempts to use --flags that don't exist // Check for attempts to use --flags that don't exist
if (currentArg.startsWith('--')) { let valid = true;
console.log('--'); if (av.startsWith('--')) {
let valid = cpuArguments.args.filter((a) => { return A.flags.long === currentArg.substring(2)}).length > 0; valid = cpuArguments.args.filter((a) => a.flags.long === av.substring(2)).length > 0;
console.log('valid', valid); } else if (av.startsWith('-')) {
if (!valid) { throw new Error(`There is no ${currentArg} option`); } valid = cpuArguments.args.filter((a) => a.flags.short === av.substring(1)).length > 0;
} else if (currentArg.startsWith('-')) {
console.log(currentArg.substring(1));
let valid = cpuArguments.args.filter((a) => { console.log(a); return a.flags.short === currentArg.substring(1)}).length > 0;
if (!valid) { throw new Error(`There is no ${currentArg} option`); }
} }
///console.log(' it\'s a valid argument'); if (!valid) { throw new Error(`There is no ${av} option`); }
///console.log(' argsrequiringvalues', argsRequiringValues);
///console.log(' currentarg', currentArg);
// Check if it requires a value... // Check if it requires a value...
// and if it does, do a cursory check to see if one is likely to be there // and if it does, do a cursory check to see if one is likely to be there
let shouldCheckValue = argsRequiringValues.filter((a) => { let shouldCheckValue = argsRequiringValues.filter((a) => {
/// console.log('a', a); let noDash = av.substring(1);
let noDash = currentArg.substring(1);
return (a.flags.short === noDash || a.flags.long === noDash); return (a.flags.short === noDash || a.flags.long === noDash);
}).length > 0; }).length > 0;
/// console.log(' requires a value?', shouldCheckValue);
/// console.log(' split', argvSplit);
if (shouldCheckValue) { if (shouldCheckValue) {
///console.log(currentArg); let errorMessage = `Value required for ${av}`;
let errorMessage = `Value required for ${currentArg}`;
// If there's nothing after this flag, error; // If there's nothing after this flag, error;
// Or if the next item is a flag... error: // Or if the next item is a flag... error:
/// console.log('idx', index, argvSplit[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); };
} }