Skip to content

Make Option.env() behave as a getter when called with no arguments#1971

Closed
aweebit wants to merge 1 commit intotj:developfrom
aweebit:feature/env-getter
Closed

Make Option.env() behave as a getter when called with no arguments#1971
aweebit wants to merge 1 commit intotj:developfrom
aweebit:feature/env-getter

Conversation

@aweebit
Copy link
Copy Markdown
Contributor

@aweebit aweebit commented Aug 15, 2023

Another tiny enhancement third-party libraries could benefit from (like #1970).

Problem

I had to use the internal Option.envVar to build informative error messages in the async-commander library I am currently working on.

Solution

Make Option.env() return the value of the variable when it is called with no parameters.

ChangeLog

Added

  • Option.env() now returns the previously provided environment variable name when called with no argument

@aweebit aweebit changed the title Make Option.env() behave as a getter when no name is supplied Make Option.env() behave as a getter when called with no arguments Aug 15, 2023
@shadowspawn
Copy link
Copy Markdown
Collaborator

shadowspawn commented Aug 15, 2023

Unless the property name starts with an underscore, it is public. e.g. option.public, option._private So it is fine to access option.envVar.

Option was once a simple prototype class with all public properties. As new features were added and Option became more full-featured, the existing properties were preserved and the all-public pattern was continued. (Which does lead to some naming gymnastics to have both a property and a chaining setter.)

@aweebit
Copy link
Copy Markdown
Contributor Author

aweebit commented Aug 15, 2023

Unless the property name starts with an underscore, it is public. e.g. option.public, option._private So it is fine to access option.envVar.

It is not included in the typings, though, that's why I though it was private. Had to include it in the manually augmented module declaration along with Command._args because of that, see typings/commander.d.ts.

So do you think it should be added to the library's type declarations instead? Or can we also keep the change I suggest here? It's not always easy to remember which methods are setter-only and which can also be used as getters. Since there is no other meaningful interpretation for an Option.env() call with no arguments, I think it's okay if we keep it.

@shadowspawn
Copy link
Copy Markdown
Collaborator

shadowspawn commented Aug 15, 2023

It is not included in the typings, though, that's why I though it was private.

Oops, that explains it, I had wondered why you hadn't just used the property!

So do you think it should be added to the library's type declarations instead?

Yes.

Or can we also keep the change I suggest here?

No. Option does not have any properties using the overloaded getter/setter pattern that is used quite a bit in Command.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants