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
Determine if default was used? #513
Determine if default was used? #513
Comments
There's no official method provided by yargs, but instead you could keep the original Boolean(process.argv.indexOf("yourOption") > -1) // Returns true if "yourOption" was passed Does that solve your problem? |
This is the exact same fix I already applied. It's unfortunate that yargs doesn't have a feature like this. Thanks for your help. |
@FallingSnow There's also another workaround that makes it slightly easier to support aliases (and other parsing translations): you can use a fake default value that you can check for and swap out after parsing. Here's an example using a custom var yargs = require('yargs')
.option('foo', {
default: '__some_unlikely_value_as_fake_default__'
})
.check(function (argv) {
if (argv.foo === yargs.getOptions().default.foo) {
console.log('foo not specified by user')
argv.foo = 'real_default'
}
return true
})
console.log(yargs.argv.foo) Besides the workarounds, what kind of support/API for this would you like to see in yargs? Something like |
Thanks for the work around. I would like to see a simple boolean that identifies if yargs used the default. For example yargs = {
parsed: {
foo: {
defaultUsed: true,
aliasUsed: false,
count: 1
}
}
} But something like a |
I'm not entirely certain if we should add this to the API, because of the following reasons:
Any thoughts? |
function userSetOption(option) {
let formattedOption;
if (option.length > 1) {
formattedOption = '--' + option;
}
else {
formattedOption = '-' + option;
}
if (process.argv.indexOf(option) > -1)
return true;
// Handle aliases for same option
for (let aliasIndex in yargs.choices().parsed.aliases[option]) {
let alias = yargs.choices().parsed.aliases[option][aliasIndex];
let formattedAlias;
if (alias.length > 1) {
formattedAlias = '--' + alias;
}
else {
formattedAlias = '-' + alias;
}
if (process.argv.indexOf(formattedAlias) > -1) {
return true;
}
}
return false;
} |
@FallingSnow Ah yeah I didn't notice you have to check for function userSetOption(option) {
function searchForOption(option) {
(process.argv.indexOf(option) > -1) && return true;
return false
}
if (searchForOption(`-${option}`) || searchForOption(`--${option}`))
return true;
// Handle aliases for same option
for (let aliasIndex in yargs.choices().parsed.aliases[option]) {
let alias = yargs.choices().parsed.aliases[option][aliasIndex];
if (searchForOption(`-${alias}`) || searchForOption(`--${alias}`))
return true;
}
return false;
} |
Ah, very nifty, thx. |
Hi, I was also looking for this option. My use case is to check if a user explicitly wants a certain option value. Lets say I have a set of options with default values (a, b, c) and a config file where I can also defined these values: {
"a": value1,
"b": value2,
"c": true
} I want to "merge" the options from both sources with the following priority:
Imagine a user has a config file they can't or don't want to modify and instead just want to explicitly override an option for a quick test. I know you can workaround this but it would be helpful (only if it's easy and minimal to implement) to have yargs provide this functionality internally. Thanks for reading this and awesome library 👍 |
Additional discussion in the yargs-parser concerning determine if a yargs.argv argument was set explicitly by the user. |
👍 +1 for this feature as well... |
i also like the idea of the and not only for the command callbacks, but the coerce callbacks as well could include the command that was used for globally defined options that apply to all commands, as referenced here #1266 :) |
I really would benefit from such a feature myself. In my scenario, I have an output file argument which has a default value. I also have a flag option. If it's off, the app output goes straight to the console but if it's on then it goes into the specified file. This way, the following will cause output to go to the console: cli ... the following will output to the default file location: cli -toFile ... and the following will output to a non-default file: cli -toFile -o myfile.txt What I wish for is being able to "forgive" my user for the following input: cli -o myfile.txt Having the user specifying a custom file should automatically imply the |
Updating @maxrimue 's code in this comment, the
It seems it can just be omitted: function userSetOption(option) {
function searchForOption(option) {
return process.argv.indexOf(option) > -1;
}
if (searchForOption(`-${option}`) || searchForOption(`--${option}`)) {
return true;
}
// Handle aliases for same option
for (let aliasIndex in yargs.parsed.aliases[option]) {
let alias = yargs.parsed.aliases[option][aliasIndex];
if (searchForOption(`-${alias}`) || searchForOption(`--${alias}`))
return true;
}
return false;
} |
A bit late to the party, I guess, but I would like to have this feature as well. |
I've stumbled across this issue, too and found a way via the middleware and the yargs instance to identify the usage of default values:
Returns
My question is: Is this an official way since the documentation does not tell something. |
Since yargs.parsed is deprecated you can set a
Returns
|
@xemle Setting an @maxrimue Given that it appears IMO this does seem to be a feature that should be built in to yargs. It doesn't seem uncommon that config files might be used in conjunction with yargs to resolve the final settings for a CLI app. Without knowing which arguments were explicitly defined on the command line there is no way to resolve whether a setting should come from a config file or the command line. Knowing which arguments were explicitly defined doesn't seem like that type of thing that we should need to hack our way into especially given all the things that yargs already does in regards to aliases and camelCase conversions. |
@dlong500 I agree that
yargs uses a flat list of arguments which need to be reflected than on the config object via @dlong500 If you have an idea how to solve nested config with yargs, please let me know. |
Is there a way to determine if a yargs.argv argument was set explicitly by the user rather than by the default?
The text was updated successfully, but these errors were encountered: