Skip to content

Argument type validation? #110

Closed
Closed

Description

@raine

Does yargs have a way to validate argument types?

I have situation where I'm taking in -b and expecting a number value but leaving it empty makes it true. It would simplify my logic if I didn't have to check the value and make yargs show help etc.

Activity

  1. bcoe commented on Mar 1, 2015

    @bcoe
    Member

    Hey @raine it's a bit low level, but yargs has the check method which allows you to perform arbitrary validation on the final parsed arguments prior to returning them to the user:

    https://github.com/bcoe/yargs#checkfn

    What if we modified check to take an argument and a function, and shipped with a few common checks:

    check('b', yargs.isInteger), just a thought.

    Independently of the check feature, I have been kicking around the idea of an number type, which would work similar to boolean, string, array, but would default integer types to 0 -- I'm not convinced this is super useful though, what do you think?

  2. raine commented on Mar 1, 2015

    @raine
    ContributorAuthor

    Without prior knowledge, I was kind of expecting that I could use type to automatically perform some level of validation on the arguments.

    yarsg.option('b', {
      type: 'number',
      alias: 'build'
    });

    Here cmd -b should not become a boolean flag, because it's expecting a number.

    Here's how optionator, another argv parsing lib I quite like does it:

    $ node index.js
    { _: [] }
    $ node index.js -b 1
    { build: 1, _: [] }
    $ node index.js -b
    { _: [] }
    $ node index.js -b "foo"
    Invalid value for option 'build' - expected type Number, received value: foo.

    Code: https://gist.github.com/31ad323a2f7640c2d2b9

  3. bcoe commented on Mar 1, 2015

    @bcoe
    Member

    @raine seems like a reasonable ask, my only question is, when just a flag is given:

    node index.js --foo

    Should this raise an exception, or should we default to 0, array defaults to [], string defaults to '' and boolean defaults to true.

  4. raine commented on Mar 1, 2015

    @raine
    ContributorAuthor

    I don't think it should default to anything, because it seems like misuse of the parameter.

    With cmd --foo I'd perceive the user's intent to be pass a boolean flag. If foo is a number option, the program should abort and inform the user about the mistake. The user's intent most likely is not to pass default, in that case they are more likely omit the parameter completely.

    In my opinion this holds for string arguments as well. It's possible to inadvertently pass an empty string as parameter. How to pass an empty string then, is a good question. Perhaps cmd --foo "".

  5. a-x- commented on Jul 29, 2015

    @a-x-

    My position, that omitted opt must be undefined.
    And set explicit value (most commonly 0) would be do by standart way (using default(key,0) API call)

  6. olsonpm commented on Aug 24, 2015

    @olsonpm

    +1
    I got here looking for type validation - though it doesn't look like a sought after feature.

  7. bcoe commented on Feb 15, 2016

    @bcoe
    Member

    @raine it might not fully address this issue, but there's now a number type in yargs 4.x.

    Give it a spin:

    npm i yargs@next

  8. plroebuck commented on Apr 23, 2016

    @plroebuck

    Shouldn't this issue be closed as of yargs-4?

  9. nexdrew commented on Apr 25, 2016

    @nexdrew
    Member

    @plroebuck Yes, I think so. Thanks for the heads up.

    If anyone finds a deficiency with the type: 'number' validation, or if you'd like to request other types (date maybe?), please open a new issue. Thanks!

  10. nghhoese commented on Jul 3, 2023

    @nghhoese

    seems like there still is no argument type validation on yargs. Would like yargs to show a message when i enter a word into a number argument. instead nothing happends and my application crashes further on because the number is not a number but a NaN

  11. shadowspawn commented on Jul 3, 2023

    @shadowspawn
    Member

    The related open issue to have Yargs show the error for bad number arguments is #1079

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions