1. const yargs = require('yargs');
  2. function initializeOptions(argv) {
  3. // Default values
  4. const options = {
  5. name: 'World', // Default name
  6. greeting: 'Hello', // Default greeting
  7. uppercase: false, // Default uppercase
  8. count: 1 // Default count
  9. };
  10. // Override defaults with command-line arguments
  11. if (argv.name) {
  12. options.name = argv.name;
  13. }
  14. if (argv.greeting) {
  15. options.greeting = argv.greeting;
  16. }
  17. if (argv.uppercase !== undefined) {
  18. options.uppercase = argv.uppercase;
  19. }
  20. if (argv.count !== undefined) {
  21. options.count = argv.count;
  22. }
  23. return options; // Return the initialized options object
  24. }
  25. // Example usage (for testing purposes)
  26. // const argv = yargs.argv;
  27. // const initializedOptions = initializeOptions(argv);
  28. // console.log(initializedOptions);

Add your comment