75lb / command-line-args

A mature, feature-complete library to parse command-line options.
MIT License
679 stars 107 forks source link

Support for type Date #90

Closed jose-nunez closed 5 years ago

jose-nunez commented 5 years ago

Hi. Thanks for this package! Is it possible to convert arguments to Date, the same way you do with String or Number? Something like:

const args = [
    { name: 'name', alias: 'n', type: String },
    { name: 'age', alias: 'a', type: Number },
    { name: 'birth', type: Date }
  ]

I built a wrapper that will convert Dates and also Json objects from the input. I'll try to bring them to the package one day

75lb commented 5 years ago

Yes, the type property is just a setter function - see here for details. The example you wrote above will work fine.

jose-nunez commented 5 years ago

Got it. This is the working example:

const args = [
    { name: 'name', alias: 'n', type: String },
    { name: 'age', alias: 'a', type: Number },
    { name: 'birth', type: myDate => new Date(myDate) }
  ]

The first one returns a string

Thanks a lot!