Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add 'type' to option specs

  • Loading branch information...
commit c2a6080ef1a98f746eb72442a4c91108fece2a11 1 parent 1d83065
@harthur authored
Showing with 32 additions and 4 deletions.
  1. +4 −0 README.md
  2. +7 −4 nomnom.js
  3. +21 −0 test/type.js
View
4 README.md
@@ -174,6 +174,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.
+#### type
+
+If you don't want the option JSON-parsed, specify type `"string"`.
+
#### hidden
Option won't be printed in the usage
View
11 nomnom.js
@@ -30,6 +30,13 @@ function ArgParser() {
}
}
var name = option.name || arg;
+
+ if (option.type != "string") {
+ try { // try to infer type by JSON parsing the string
+ value = JSON.parse(value)
+ } catch(e) {}
+ }
+
if(option.list) {
if(!options[name])
options[name] = [value];
@@ -361,10 +368,6 @@ Arg = function(str) {
else if(full)
value = fullMatch[1] ? false : fullMatch[3];
- try { // try to infer type by JSON parsing the string
- value = JSON.parse(value)
- } catch(e) {}
-
return {
str: str,
chars: chars,
View
21 test/type.js
@@ -0,0 +1,21 @@
+var assert = require("assert"),
+ nomnom = require("../nomnom");
+
+
+var options = nomnom.opts({
+ num1 : {
+ position: 0
+ },
+ num2: {
+ position: 1,
+ type: "string"
+ },
+ num3: {
+ string: "-n count",
+ type: "string"
+ }
+}).parseArgs(["3", "4", "-n", "8"]);
+
+assert.strictEqual(options.num1, 3);
+assert.strictEqual(options.num2, "4");
+assert.strictEqual(options.num3, "8")
Please sign in to comment.
Something went wrong with that request. Please try again.