Adding parsedOptions #65

Closed
wants to merge 1 commit into from
View
9 Readme.md
@@ -12,6 +12,11 @@
Options with commander are defined with the `.option()` method, also serving as documentation for the options. The example below parses args and options from `process.argv`, leaving remaining args as the `program.args` array which were not consumed by options.
+In addition, since the program object has a number of other
+properties associated with it, the simple
+list of options that were parsed from the command line can
+be read from program.parsedOptions.
+
```js
#!/usr/bin/env node
@@ -33,6 +38,8 @@ console.log('you ordered a pizza with:');
if (program.peppers) console.log(' - peppers');
if (program.pineapple) console.log(' - pineappe');
if (program.bbq) console.log(' - bbq');
+//access via .parsedOptions
+if (program.parsedOptions['bbq']) console.log(' - bbq');
@tj
Owner
tj added a note Jun 19, 2012

also this could just be program.parsedOptions.bbq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
console.log(' - %s cheese', program.cheese);
```
@@ -259,4 +266,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
2 lib/commander.js
@@ -87,6 +87,7 @@ Option.prototype.is = function(arg){
function Command(name) {
this.commands = [];
this.options = [];
@tj
Owner
tj added a note Jun 19, 2012

we could change this.options to this._options (or whatever) and use that instead of .parsedOptions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ this.parsedOptions = {};
this.args = [];
this.name = name;
}
@@ -323,6 +324,7 @@ Command.prototype.option = function(flags, description, fn, defaultValue){
// reassign
self[name] = val;
}
+ self.parsedOptions[name] = self[name];
});
return this;