Commits

Joey Mazzarelli committed b9fc019

Comments (0)

Files changed (1)

 
 === Examples ===
 
-==== Example 1 ====
+==== Example 1 -- Simple "getting started" example ====
 {{{
 #!javascript
 /**
 }}}
 
 
-==== Example 2 ====
+==== Example 2 -- Showing more features ====
 {{{
 #!javascript
 /**
 process.exit(0);
 }}}
 
+==== Example 3 -- Showing conflict detection ====
 
+{{{
+#!javascript
+/**
+ * Simple example that is broken by design (conflicting options)
+ *
+ * Examples:
+ *   $ node example3.js
+ *   > Conflicting flags: -v
+ */
+
+var opts = require('./opts');
+
+var options = [
+  { short       : 'v'
+  , description : 'Show version and exit'
+  },
+  { short       : 'v'
+  , description : 'Be verbose'
+  },
+];
+
+opts.parse(options);
+puts('Example 3');
+process.exit(0);
+}}}
+==== Example 4 -- Shows how to use named arguments and using from within a library ====
+
+{{{
+#!javascript
+/**
+ * Advanced example using namespaces for a library and named arguments
+ *
+ * Run:
+ *   node example4.js --help
+ * and play with the options to see the behavior.
+ */
+
+var opts = require('./opts')
+  , puts = require('sys').puts
+  , host = 'localhost'; // default host value
+
+// Example of using some library in the same app
+var libOpts = [
+  { short       : 'l'
+  , long        : 'list'
+  , description : 'Show the library list'
+  , callback    : function () { puts('mylib list!'); },
+  },
+];
+opts.add(libOpts, 'mylib');
+
+var options = [
+  { short       : 'l' // deliberately conflicting with 'mylib' option
+  , long        : 'list'
+  , description : 'List all files'
+  },
+  { short       : 'd'
+  , long        : 'debug'
+  , description : 'Set a debug level'
+  , value       : true
+  },
+];
+
+var arguments = [ { name : 'script' , required : true }
+                , { name : 'timeout' }
+                ];
+
+opts.parse(options, arguments, true);
+
+var debug = opts.get('d') || 'info'  // default debug value
+  , list  = opts.get('list');
+
+var script  = opts.arg('script')
+  , timeout = opts.arg('timeout') || 30;
+
+
+if (list) puts('List arg was set');
+puts('Debug level is: ' + debug);
+puts('Script is: ' + script);
+puts('Timeout is: ' + timeout);
+
+process.exit(0);
+}}}