jk icon indicating copy to clipboard operation
jk copied to clipboard

Required input parameter

Open dlespiau opened this issue 5 years ago • 2 comments

In those cases, input parameters cannot be given a default value and the user needs to provide one value when running the script. It would be good in the parameter API would let you describe a parameter as required. It could be done by introducing a 3rd argument to the parameter functions, an object with options.

std.param.String('user', undefined, { required: true });

The String function would throw an exception if the 'user' parameter has not been given.

dlespiau avatar Apr 02 '19 16:04 dlespiau

Is this a convenience for

const user = std.param.String('user', undefined);
if (user === undefined) {
  throw new Error('parameter "user" required but not supplied');
}

squaremo avatar Apr 02 '19 16:04 squaremo

Yes, indeed. I did something similar is a script I'm writing:

function required(name, v) {
  if (v === undefined) {
    throw new Error(`'${name}' parameter must be provided`);
  }
}

required("user", user);

dlespiau avatar Apr 02 '19 16:04 dlespiau