Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track which arguments have been defaulted #1334

Open
charlesspitzer opened this issue Apr 19, 2019 · 8 comments
Open

feat: track which arguments have been defaulted #1334

charlesspitzer opened this issue Apr 19, 2019 · 8 comments

Comments

@charlesspitzer
Copy link

Say I have the arguments --abc N. If I make the default for abc be 5, then yargs will create the .abc value with 5. However, this doesn't tell me if --abc was actually seen on the command line. Is there a way to see an argument was given at all?

@charlesspitzer
Copy link
Author

charlesspitzer commented Apr 19, 2019

that's not exactly what i'm after.

if i have
const yargs = require('yargs')
.option('abc', {default: 5,type: 'number'})
.option('def', {default:false,type:'boolean'})
.option('ghi', {default:false,type:'boolean'});;

and the command line
cmd --ghi

there doesn't seem to be a way to tell if --abc was specified, nor if --def is using the default value, because yargs.abc will === 5 and yargs.def will be false in this cmd invocation (ie: the presence of a default causes that to be instantiated, rather than it being undefined as if it didn't appear on the command line).

what's needed is some boolean on each argument to tell if it was actually present on the command line or if the default created the field in the yargs object.

@bcoe bcoe added the question label May 1, 2019
@bcoe
Copy link
Member

bcoe commented May 1, 2019

@charlesspitzer out of curiosity, what's your use-case for knowing whether or not the value was defaulted? You can get the list of defaulted values from the yargs object itself, and at least know whether the value is the same as the default.

@charlesspitzer
Copy link
Author

charlesspitzer commented May 1, 2019

i have a command line which needs to enforce: if --arg1 is given, then either --arg2 OR --arg3 is required. if --arg1 has a default, it is instantiated, but there seems to be no way to see, when checking when --arg2 or --arg3 is given, whether --arg1 was instantiated via the default option or whether it was present on the command line. those 2 cases can mean different things in certain cases.

@bcoe bcoe changed the title Presence of an argument? feat: track which arguments have been defaulted May 1, 2019
@bcoe
Copy link
Member

bcoe commented Sep 29, 2019

see: yargs/yargs-parser#203

@bcoe
Copy link
Member

bcoe commented Nov 5, 2019

@charlesspitzer mind taking a look at #1472, and letting me know if this would do the trick for you?

@giacomocerquone
Copy link

giacomocerquone commented Jan 1, 2020

@bcoe yes, that pr would be ok to me too.
My use case is to warn the user that the script I wrote is defaulting to some value.

@bcoe
Copy link
Member

bcoe commented Feb 6, 2020

@giacomocerquone unfortunately I've had next to no time for open-source these days, but I'll make an effort to address @coreyfarrell's concerns on that PR "soon".

@giacomocerquone
Copy link

@bcoe absolutely, take your time, no worries here, we're all grateful for the work and the effort you put in this project :)

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

Successfully merging a pull request may close this issue.

3 participants