{"action":"created","issue":{"url":"https://api.github.com/repos/yargs/yargs/issues/875","repository_url":"https://api.github.com/repos/yargs/yargs","labels_url":"https://api.github.com/repos/yargs/yargs/issues/875/labels{/name}","comments_url":"https://api.github.com/repos/yargs/yargs/issues/875/comments","events_url":"https://api.github.com/repos/yargs/yargs/issues/875/events","html_url":"https://github.com/yargs/yargs/issues/875","id":226888510,"number":875,"title":"Default value only if flag called","user":{"login":"pedro93","id":3718654,"avatar_url":"https://avatars3.githubusercontent.com/u/3718654?v=3","gravatar_id":"","url":"https://api.github.com/users/pedro93","html_url":"https://github.com/pedro93","followers_url":"https://api.github.com/users/pedro93/followers","following_url":"https://api.github.com/users/pedro93/following{/other_user}","gists_url":"https://api.github.com/users/pedro93/gists{/gist_id}","starred_url":"https://api.github.com/users/pedro93/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pedro93/subscriptions","organizations_url":"https://api.github.com/users/pedro93/orgs","repos_url":"https://api.github.com/users/pedro93/repos","events_url":"https://api.github.com/users/pedro93/events{/privacy}","received_events_url":"https://api.github.com/users/pedro93/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-05-07T19:52:31Z","updated_at":"2017-05-17T04:14:51Z","closed_at":null,"body":"Lets say I have a cmd line tool (prog) with yargs options like so:\r\n```js\r\n.option('config', {\r\n    'describe': 'Path to config file',\r\n    'type': 'string',\r\n    'default': 'package.json'\r\n  })\r\n```\r\nIf I call: ```$ prog``` I expect the value of config to be not be set\r\nIf I call: ```$ prog --config``` No value set explicitly, but option called explicitly then ```config === package.json```\r\nIf I call: ```$ prog --config=some.json``` I expect the value of config to be some.json\r\n\r\nIs this possible natively in yargs? I have been unsuccessful so far and have to have a workaround like this (without default value in corresponding config yarg option)\r\n```js\r\nif(argv.config !== undefined)\r\n let config = argv.config.length ? getConfigFrom(argv.config) : getConfigFrom('package.json');\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/yargs/yargs/issues/comments/301980705","html_url":"https://github.com/yargs/yargs/issues/875#issuecomment-301980705","issue_url":"https://api.github.com/repos/yargs/yargs/issues/875","id":301980705,"user":{"login":"bcoe","id":194609,"avatar_url":"https://avatars0.githubusercontent.com/u/194609?v=3","gravatar_id":"","url":"https://api.github.com/users/bcoe","html_url":"https://github.com/bcoe","followers_url":"https://api.github.com/users/bcoe/followers","following_url":"https://api.github.com/users/bcoe/following{/other_user}","gists_url":"https://api.github.com/users/bcoe/gists{/gist_id}","starred_url":"https://api.github.com/users/bcoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bcoe/subscriptions","organizations_url":"https://api.github.com/users/bcoe/orgs","repos_url":"https://api.github.com/users/bcoe/repos","events_url":"https://api.github.com/users/bcoe/events{/privacy}","received_events_url":"https://api.github.com/users/bcoe/received_events","type":"User","site_admin":false},"created_at":"2017-05-17T04:14:51Z","updated_at":"2017-05-17T04:14:51Z","body":"@pedro93 I think your best bet might be using the `coerce` functionality:\r\n\r\nhttps://github.com/yargs/yargs#coercekey-fn\r\n\r\nthis would allow you to check whether the value of `config` is `false`; and default it to package.json ... it was my hope that `coerce` would help people address some of the edge-cases like this."}}
{"actor":{"display_login":"bcoe"}}