75lb / command-line-args

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

Multiple as a single type? #114

Closed OmgImAlexis closed 3 years ago

OmgImAlexis commented 3 years ago

Is it possible to have multiple values returned as a single custom type?

For example the wiki shows this.

$ node example/type.js package.json nothing.js
{ file:
   [ FileDetails { filename: 'package.json', exists: true },
     FileDetails { filename: 'nothing.js', exists: false } ] }

Whereas I'm wanting this.

$ node example/type.js package.json nothing.js
{ file:
   [ FileDetails { filenames: ['package.json', 'nothing.js'], exists: true } ] }
75lb commented 3 years ago

Hi, no the type function specifies a type for each individual value, by design..

In your case, if you start with this..

const options = commandLineArgs(..)

.. and options.file contains an array of FileDetails instances then you could pass them into a FileList class..

class FileList {
  constructor (files) {
    this.files = files
    this.exists = files.every(f => f.exists)
  }
}

const fileList = new FileList(options.file)
OmgImAlexis commented 3 years ago

Unfortunately in my case that won’t work as it’s not files; I was just using that as an example.

I’ll have to just use strings and combine then after the fact.