Skip to content

Instantly share code, notes, and snippets.

@Inplex-sys
Created March 23, 2023 00:46
Show Gist options
  • Save Inplex-sys/50658457458fcf56a2eb1db43d9ee8eb to your computer and use it in GitHub Desktop.
Save Inplex-sys/50658457458fcf56a2eb1db43d9ee8eb to your computer and use it in GitHub Desktop.
class ArgumentManager {
constructor() {
this.arguments = {};
}
parseArguments() {
const args = process.argv.slice(2);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg.startsWith('--')) {
const key = arg.slice(2);
let value = true;
if (i < args.length - 1 && !args[i + 1].startsWith('--')) {
value = args[i + 1];
i++;
}
this.arguments[key] = value;
}
}
}
getArgument(key) {
return this.arguments[key];
}
hasArgument(key) {
return key in this.arguments;
}
}
// Example usage
const argumentManager = new ArgumentManager();
argumentManager.parseArguments();
if (argumentManager.hasArgument('help')) {
console.log('Usage: node app.js [--help] [--verbose]');
process.exit();
}
const isVerbose = argumentManager.getArgument('verbose');
console.log(`Verbose mode: ${isVerbose}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment