Created
March 23, 2023 00:46
-
-
Save Inplex-sys/50658457458fcf56a2eb1db43d9ee8eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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