75lb/command-line-args

defaultValue not applied if argument passed without value

qwelias opened this issue · 5 comments

const option = commandLineArgs([
  { name: 'json', alias: 'j', type: Number, defaultValue: 2 },
])

called as command -j

Expected:

option.json; // 2

Actual:

option.json; // null
75lb commented

Hi, this is the correct, defined behaviour.

One of the features of command-line-args is that it's possible to set an empty value (i.e. null).

So, three ways to set an option value. Set an explicit value:

$ command --json something

Set an empty value. This will set null.

$ command --json

Don't set a value. In this case the defaultValue is used.

$ command

Did this behaviour not match your expectations? Do you think it should behave differently?

Hi @75lb,

Yes, considering this is not specified in the docs for defaultValue it was, indeed, quite unexpected.

Because usually when I define default value for anything I expect the value to be either the default one specified or the one passed to this anything, and for me this package is quite unique in it's behaviour.
Maybe you can share examples of such behaviour in other packages/programs?

Also if user specifies type as Number they expect argument to be parsed in accordance to the specified type, and, in this case null does not comply for Number as far as I'm aware, so that's unexpected too.

75lb commented

considering this is not specified in the docs for defaultValue

OK, then i will improve the docs.

Maybe you can share examples of such behaviour in other packages/programs?

For example, a web-server application might have a --stack option to supply optional, additional middleware. Use cases below.

Use the default, built-in stack.

$ server 

Use a custom stack.

$ server --stack middleware1.js middleware2.js

Specifically set an empty stack (meaning you don't want to use the default stack or a custom stack).

$ server --stack

null does not comply for Number

Yes, i can see your point here. So, in your case, if a user runs the following command, what would you expect to happen? Do you think command should fail because no number was supplied? Or do you think the command should use the defaultValue for --number and succeed?

$ command --number

Expected (pseudo code):

for each passed argument 
value = input
if no value then value = defaultValue
if no value then value = null
if type and value not of type then throw error
assign value for argument in question

It's only my vision tho, but it seems quite explicit and should answer to your questions.

On the other hand it would most probably introduce breaking changes, so feel free to close the issue if you think updating the docs is good enough treatment.

75lb commented

Yes, in the short-term I will update the docs but also plan to update the library in the near-future. Will add more parsing strategy options to give more control over the behaviour. Closing this as the current behaviour is correct but will add another comment once new features land.