How to use the clean function from nopt

Find comprehensive JavaScript nopt.clean code examples handpicked from public code repositorys.

nopt.clean is a function in the nopt library that can be used to clean and validate input arguments for a Node.js program based on a set of defined options.

431
432
433
434
435
436
437
438
439
440
441
442
}


function validate (cl) {
  // warn about invalid configs at every level.
  cl.list.forEach(function (conf) {
    nopt.clean(conf, configDefs.types)
  })


  nopt.clean(cl.root, configDefs.types)
}
fork icon0
star icon1
watch icon1

+ 9 other calls in file

445
446
447
448
449
450
451
452
453
454
  obj[_valid] = true

  nopt.invalidHandler = (k, val, type) =>
    this.invalidHandler(k, val, type, obj.source, where)

  nopt.clean(obj.data, this.types, this.typeDefs)

  nopt.invalidHandler = null
  return obj[_valid]
}
fork icon0
star icon0
watch icon207

How does nopt.clean work?

nopt.clean is a function in the nopt library that can be used to clean and validate input arguments for a Node.js program based on a set of defined options.

When nopt.clean is called, it takes two arguments: an object representing the input arguments to be cleaned, and an object that defines the expected options and their types.

The input arguments object is then parsed and validated against the defined options object. Any invalid or unexpected arguments are removed, and the remaining arguments are returned as a cleaned object that matches the defined options.

By validating and cleaning input arguments in this way, nopt.clean can help to ensure that the program is running with valid and expected arguments, reducing the potential for errors or unexpected behavior.

Overall, nopt.clean provides a simple and effective way to clean and validate input arguments for a Node.js program, making it a useful tool for building robust and reliable applications.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const nopt = require("nopt");

// Define the expected options and their types
const knownOpts = {
  env: String,
  port: Number,
  debug: Boolean,
};

// Define shorthand options
const shortHands = {
  e: ["--env"],
  p: ["--port"],
  d: ["--debug"],
};

// Parse the input arguments
const parsedArgs = nopt(knownOpts, shortHands, process.argv, 2);

// Clean the input arguments
const cleanedArgs = nopt.clean(parsedArgs, knownOpts);

console.log(cleanedArgs);

In this example, we define an object knownOpts that represents the expected options and their types for our program, including an env option that is a string, a port option that is a number, and a debug option that is a boolean. We also define shorthand options using the shortHands object. We then use nopt to parse the input arguments, including the knownOpts and shortHands objects, as well as the process.argv array and a starting index of 2. Finally, we use nopt.clean to clean the parsed arguments based on the knownOpts object, removing any invalid or unexpected arguments and returning a cleaned object that matches the expected options. This example shows how nopt.clean can be used to ensure that input arguments are valid and expected, reducing the potential for errors or unexpected behavior in a Node.js program.