Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

option can have a 'choices' list of possible values

  • Loading branch information...
commit 561de3cdb3b642f9d1a65aa7d97cb14cd3245600 1 parent 4978af4
@harthur authored
Showing with 53 additions and 12 deletions.
  1. +17 −11 README.md
  2. +4 −0 nomnom.js
  3. +1 −1  package.json
  4. +31 −0 test/choices.js
View
28 README.md
@@ -43,6 +43,19 @@ for [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
npm install nomnom
+# More Details
+Nomnom supports args like `-d`, `--debug`, `--no-debug`, `--file=test.txt`, `--file test.txt`, `-f test.txt`, `-xvf`, and positionals. Positionals are arguments that don't fit the `-a` or `--atomic` format and aren't attached to an option.
+
+Values are JSON parsed, so `--debug=true --count=3 --file=log.txt` would give you:
+
+```
+{
+ "debug": true,
+ "count": 3,
+ "file": "log.txt"
+}
+```
+
# Commands
Nomnom supports command-based interfaces (e.g. with git: `git add -p` and `git rebase -i` where `add` and `rebase` are the commands):
@@ -75,17 +88,6 @@ parser.parseArgs();
Each command generates its own usage message when `-h` or `--help` is specified with the command.
-# More Details
-Nomnom supports args like `-d`, `--debug`, `--no-debug`, `--file=test.txt`, `--file test.txt`, `-f test.txt`, `-xvf`, and positionals. Positionals are arguments that don't fit the `-a` or `--atomic` format and aren't attached to an option.
-
-Values are JSON parsed, so `--debug=true --count=3 --file=log.txt` would give you:
-
- {
- "debug": true,
- "count": 3,
- "file": "log.txt"
- }
-
# Usage
Nomnom prints out a usage message if `--help` or `-h` is an argument. Usage for these options in `test.js`:
@@ -174,6 +176,10 @@ If the option has a `position` and `list` is `true`, all positional args includi
If this is set to `true` and the option isn't in the args, a message will be printed and the program will exit.
+#### choices
+
+A list of the possible values for the option (e.g. `['run', 'test', 'open']`). If the parsed value isn't in the list a message will be printed and the program will exit.
+
#### type
If you don't want the option JSON-parsed, specify type `"string"`.
View
4 nomnom.js
@@ -36,6 +36,10 @@ function ArgParser() {
value = JSON.parse(value)
} catch(e) {}
}
+
+ if (option.choices && option.choices.indexOf(value) == -1) {
+ parser.print(name + " must be one of: " + option.choices.join(", "));
+ }
if(option.list) {
if(!options[name])
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "nomnom",
"description": "Option parser with generated usage and commands",
- "version": "0.5.0",
+ "version": "0.6.0",
"author": "Heather Arthur <fayearthur@gmail.com>",
"repository": {
"type": "git",
View
31 test/choices.js
@@ -0,0 +1,31 @@
+var nomnom = require("../nomnom"),
+ assert = require("assert");
+
+var opts = {
+ command : {
+ position: 0,
+ choices: ["run", "test"]
+ },
+ count: {
+ string: '-n count',
+ choices: [1, 2, 3]
+ }
+}
+
+nomnom().opts(opts).printFunc(function(str) {
+ assert.equal(str, "command must be one of: run, test");
+}).parseArgs(["not"]);
+
+nomnom().opts(opts).printFunc(function(str) {
+ assert.ok(false);
+}).parseArgs(["run"]);
+
+
+nomnom().opts(opts).printFunc(function(str) {
+ assert.equal(str, "count must be one of: 1, 2, 3");
+}).parseArgs(["test", "-n", 7]);
+
+
+nomnom().opts(opts).printFunc(function(str) {
+ assert.ok(false);
+}).parseArgs(["test", "-n", 3]);
Please sign in to comment.
Something went wrong with that request. Please try again.