Sitelet https://github.com/nodejs/node/issues/48839
Skip to content

util: parseArgs - if the option key is not being passed it should also throw an error #48839

Description

@ErickWendel

Version

v21.0.0-pre

Platform

Darwin MacBook-Pro-7.local 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

Using the code below, it doesn't throw an error if the options are not provided.

// index.mjs
import { parseArgs } from 'node:util'

const { values } = parseArgs({
    strict: true,
    options: {
        file: {
            type: 'string',
            short: 'f'
        },
    },
})
console.log('args:', values)
// args: [Object: null prototype] {}

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

Should throw an error for both missing options and values

What do you see instead?

Only throw for missing values

Additional information

@nodejs/tooling

Activity

  1. bakkot commented on Jul 19, 2023

    @bakkot
    Contributor

    By "missing options", do you mean that the user of the CLI app doesn't pass the --file option?

    If so: in most CLI applications, most options are optional. So that's definitely not the correct default; it would need to be opt in. For which, see #44564.

    I'm also not sure what you mean by "throw for missing values". This utility doesn't (and shouldn't) throw if the user fails to pass any values; that's also up to you to validate.

  2. added
    utilIssues and PRs related to the built-in util module.
    on Jul 20, 2023
  3. ErickWendel commented on Jul 20, 2023

    @ErickWendel
    MemberAuthor

    By "missing options", do you mean that the user of the CLI app doesn't pass the --file option?

    If so: in most CLI applications, most options are optional. So that's definitely not the correct default; it would need to be opt in. For which, see #44564.

    I'm also not sure what you mean by "throw for missing values". This utility doesn't (and shouldn't) throw if the user fails to pass any values; that's also up to you to validate.

    Oh this issue you mentioned is exactly what I meant. I'm gonna close it here and keep there! thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions