electron 0.2.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.npmignore CHANGED
@@ -1,5 +1,7 @@
1
1
  .git*
2
+ docs/
2
3
  support/
4
+ examples/
3
5
  test/
4
6
  .DS_Store
5
7
  coverage.html
package/History.md CHANGED
@@ -1,4 +1,39 @@
1
1
 
2
+ 0.4.1 / 2014-12-15
3
+ ==================
4
+
5
+ * Merge pull request #7 from RobertWHurst/patch-1
6
+ * Fix broken output in non TTY mode
7
+ * Merge pull request #6 from tunnckoCore/patch-1
8
+ * closes #4, rename displayHelp to showHelp
9
+
10
+ 0.4.0 / 2013-01-21
11
+ ==================
12
+
13
+ * test: add and use bootstrap for common testing env
14
+ * deps: update drip to 1.1.x
15
+ * Merge pull request #1 from jasonrm/test-fix
16
+ * Fix failing tests due to incorrect usage of length/lengthOf with Chai.
17
+
18
+ 0.3.0 / 2012-10-07
19
+ ==================
20
+
21
+ * update comments to reflect multiword params
22
+ * add support for multiword argv paramters and tests
23
+
24
+ 0.2.1 / 2012-06-11
25
+ ==================
26
+
27
+ * fix doc asset links
28
+ * generate docs
29
+ * comment typos
30
+ * doc website header
31
+ * readme
32
+ * argv export default to using process.argv
33
+ * added travis support
34
+ * commentt program header
35
+ * fix bug that caused error on multiple calls to `program.colorize`
36
+
2
37
  0.2.0 / 2012-06-10
3
38
  ==================
4
39
 
package/README.md CHANGED
@@ -1,4 +1,149 @@
1
- electron
2
- ========
1
+ # Electron [![Build Status](https://secure.travis-ci.org/logicalparadox/electron.png?branch=master)](http://travis-ci.org/logicalparadox/electron)
3
2
 
4
- Event based CLI framework.
3
+ > A simple command-line interface framework for [node.js](http://nodejs.org).
4
+
5
+ #### Features
6
+
7
+ - reimagined `process.argv` parsing utility
8
+ - framework for single or multiple command programs
9
+ - automatic `--help` command generation with multiple theming options
10
+ - built in cli coloring
11
+ - chainable api
12
+
13
+ ## Quick Start Guide
14
+
15
+ This "Quick Start Guide" and the full API reference can be found
16
+ on [electron's documentation website](http://alogicalparadox.com/electron).
17
+
18
+ #### Installation
19
+
20
+ The `electron` package is available through [npm](http://npmjs.org). It is recommended
21
+ that you add it to your project's `package.json`.
22
+
23
+ ```bash
24
+ npm install electron
25
+ ```
26
+
27
+ #### Parsing Arguments
28
+
29
+ The argument parsing utility can be used independently of the program
30
+ framework. Just pass the `process.argv` from any node modules and your
31
+ ready to go.
32
+
33
+ The following command execution...
34
+
35
+ ```bash
36
+ $ node cli.js build --minify --out saved.min.js
37
+ ```
38
+
39
+ Could be captured as so...
40
+
41
+ ```javascript
42
+ var argv = require('electron').argv();
43
+
44
+ // objects
45
+ argv.commands; // [ 'build' ]
46
+ argv.modes; // [ 'minify' ]
47
+ argv.params; // { out: 'saved.min.js' }
48
+
49
+ // helpers
50
+ argv.command('build'); // true
51
+ argv.mode('m', 'minify'); // true
52
+ argv.param('o', 'out'); // 'saved.min.js'
53
+ ```
54
+
55
+ Recommend reading the "Argument Parsing Utility" section of the
56
+ [documentation](http://alogicalpardox.com/electron)
57
+ to learn about the methodologies and specifics of each of the helpers.
58
+
59
+ #### Your First Program
60
+
61
+ To construct your first program, simply execute the electron export
62
+ with a parameter of the namespace you wish to use for your program.
63
+ Then proceed to define your settings and commands.
64
+
65
+ ```javascript
66
+ var myApp = require('../lib/myapp')
67
+ , program = require('electron')('myapp');
68
+
69
+ /**
70
+ * Define your program settings
71
+ */
72
+
73
+ program
74
+ .name('My Cool App')
75
+ .desc('http://docs.mycoolapp.com')
76
+ .version(myApp.version);
77
+
78
+ /**
79
+ * Define your first command
80
+ */
81
+
82
+ program
83
+ .command('build')
84
+ .desc('start a build task')
85
+ .option('-m, --minify', 'flag to set enable minification')
86
+ .option('-o, --out [file.js]', 'name of output file')
87
+ .action(function (argv) {
88
+ var minify = argv.mode('m', 'minify')
89
+ , savefile = argv.param('o', 'out')
90
+ , cwd = argv.cwd;
91
+
92
+ program.colorize();
93
+ console.log('Welcome to myApp'.gray + myApp.version);
94
+ console.log('It works if it ends with '.gray + 'myApp ' + 'ok'.green);
95
+ // etc...
96
+ });
97
+
98
+ /**
99
+ * Parse argv and execute respective command
100
+ */
101
+
102
+ program.parse();
103
+ ```
104
+
105
+ Your `-h, --help` and `-v, --version` will be generated for you automatically.
106
+
107
+ Recommend reading the "Program Framework" and "Constructing Commands" sections
108
+ of the [documentation](http://alogicalpardox.com/electron)
109
+ to learn about all of the available chainable commands and theming options
110
+ available to construct your programs.
111
+
112
+ ## Tests
113
+
114
+ Tests are writting in [Mocha](http://github.com/visionmedia/mocha) using
115
+ the [Chai](http://chaijs.com) `should` BDD assertion library. To make sure you
116
+ have that installed, clone this repo, install dependacies using `npm install`.
117
+
118
+ $ npm test
119
+
120
+ ## Contributors
121
+
122
+ Interested in contributing? Fork to get started. Contact [@logicalparadox](http://github.com/logicalparadox)
123
+ if you are interested in being regular contributor.
124
+
125
+ * Jake Luer ([@logicalparadox](http://github.com/logicalparadox))
126
+
127
+ ## License
128
+
129
+ (The MIT License)
130
+
131
+ Copyright (c) 2012 Jake Luer <jake@alogicalparadox.com>
132
+
133
+ Permission is hereby granted, free of charge, to any person obtaining a copy
134
+ of this software and associated documentation files (the "Software"), to deal
135
+ in the Software without restriction, including without limitation the rights
136
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
137
+ copies of the Software, and to permit persons to whom the Software is
138
+ furnished to do so, subject to the following conditions:
139
+
140
+ The above copyright notice and this permission notice shall be included in
141
+ all copies or substantial portions of the Software.
142
+
143
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
144
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
145
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
146
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
147
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
148
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
149
+ THE SOFTWARE.
@@ -11,7 +11,7 @@
11
11
  module.exports = Args;
12
12
 
13
13
  /**
14
- * ## Argument Parsing
14
+ * ## Argument Parsing Utility
15
15
  *
16
16
  * The electron argument parser takes the node.js standard
17
17
  * `process.argv` array and constructs an object with helpers
@@ -30,8 +30,8 @@ module.exports = Args;
30
30
  * ##### Commands
31
31
  *
32
32
  * Commands are the simplest of arguments. They are any arguments
33
- * that are listed to that do not start with the `-` prefix. Essentially,
34
- * they are a list of keys.
33
+ * that are listed to that do not start with the `-` or `--` prefix.
34
+ * Essentially, they are a list of keys.
35
35
  *
36
36
  * // $ node cli.js hello universe
37
37
  * argv.commands === [ 'hello', 'universe' ];
@@ -58,7 +58,15 @@ module.exports = Args;
58
58
  * , w: 'now'
59
59
  * };
60
60
  *
61
- * @header Argument Parsing
61
+ * You can also specify paramters with multiple words by surrounding the
62
+ * phrase with double-quotes.
63
+ *
64
+ * // $ node cli.js --say "hello universe"
65
+ * argv.params === {
66
+ * say: 'hello universe'
67
+ * };
68
+ *
69
+ * @header Argument Parsing Utility
62
70
  */
63
71
 
64
72
  function Args (args) {
@@ -144,7 +152,8 @@ Args.prototype.param = filter('params');
144
152
  function processArgs (args) {
145
153
  var param_key = null
146
154
  , parts = args.slice(2)
147
- , input = this;
155
+ , input = this
156
+ , isStr = false;
148
157
 
149
158
  function checkParamKey () {
150
159
  if (param_key !== null) {
@@ -153,6 +162,10 @@ function processArgs (args) {
153
162
  }
154
163
  }
155
164
 
165
+ function appendStr (key, str) {
166
+ input.params[key] += ' ' + str;
167
+ }
168
+
156
169
  parts.forEach(function (part) {
157
170
  if (part.substr(0, 2) === '--') {
158
171
  checkParamKey();
@@ -183,8 +196,19 @@ function processArgs (args) {
183
196
  part = Number(part) || part
184
197
 
185
198
  if (param_key !== null) {
186
- input.params[param_key] = part;
187
- param_key = null;
199
+ if (part[0] === '"') {
200
+ isStr = true;
201
+ input.params[param_key] = part.substr(1);
202
+ } else if (isStr && part[part.length - 1] === '"') {
203
+ isStr = false;
204
+ appendStr(param_key, part.substr(0, part.length - 1));
205
+ param_key = null;
206
+ } else if (isStr) {
207
+ appendStr(param_key, part);
208
+ } else {
209
+ input.params[param_key] = part;
210
+ param_key = null;
211
+ }
188
212
  } else {
189
213
  input.commands.push(part);
190
214
  }
@@ -8,7 +8,7 @@
8
8
  * External dependancies
9
9
  */
10
10
 
11
- var Drip = require('drip')
11
+ var EventEmitter = require('drip').EnhancedEmitter
12
12
  , tty = require('tty')
13
13
  , util = require('util');
14
14
 
@@ -56,7 +56,28 @@ module.exports = Program;
56
56
  /**
57
57
  * ## Program Framework
58
58
  *
59
+ * The primary export of the electron module is a function
60
+ * that composes a new program framework. The returned
61
+ * `program` is a chainable api that allow you to change
62
+ * settings, define commands, and start launch the program.
59
63
  *
64
+ * The primary argument provided on construction is the base
65
+ * name used through the help documentation. In a majority of
66
+ * of cases, this would be the command executed from your terminal
67
+ * used launch the program.
68
+ *
69
+ * In the case of scripts with the header of `#!/usr/bin/env node`,
70
+ * you should use a variant of the following.
71
+ *
72
+ * var program = electron('microscope');
73
+ *
74
+ * If you are however launching your program from a `.js` file,
75
+ * the recommended construction pattern is the following.
76
+ *
77
+ * var program = electrong('node microscope.js');
78
+ *
79
+ * You can then chain any of the following commands to further
80
+ * define your application and commands.
60
81
  *
61
82
  * @header Program Framework
62
83
  */
@@ -67,8 +88,9 @@ function Program (base, opts) {
67
88
  * @param {Object} options
68
89
  */
69
90
 
70
- Drip.call(this, { delimeter: ' ' });
91
+ EventEmitter.call(this, { delimeter: ' ' });
71
92
  this.commands = [];
93
+ this._colorized = false;
72
94
  this.opts = defaults(opts || {}, {
73
95
  useColors: istty
74
96
  , version: null
@@ -89,7 +111,7 @@ function Program (base, opts) {
89
111
  * Inherit from Drip event emitter.
90
112
  */
91
113
 
92
- util.inherits(Program, Drip);
114
+ util.inherits(Program, EventEmitter);
93
115
 
94
116
  /**
95
117
  * ### .command (name)
@@ -231,7 +253,7 @@ Program.prototype.parse = function (args) {
231
253
 
232
254
  argv.cwd = this.opts.cwd;
233
255
  if (argv.mode('help', 'h')) {
234
- displayHelp.call(this);
256
+ this.showHelp();
235
257
  } else if (argv.mode('version', 'v')) {
236
258
  displayVersion.call(this);
237
259
  } else if (!command.length) {
@@ -415,6 +437,7 @@ Program.prototype.theme = function (name, spec) {
415
437
  * Just in case, if the current program is not running as a TTY,
416
438
  * no string changes will be made.
417
439
  *
440
+ * program.colorize();
418
441
  * console.log('hello universe'.green);
419
442
  *
420
443
  * ##### Colors
@@ -432,6 +455,7 @@ Program.prototype.theme = function (name, spec) {
432
455
  */
433
456
 
434
457
  Program.prototype.colorize = function (noColors) {
458
+ if (this._colorized) return this;
435
459
  var self = this
436
460
  , colors = {
437
461
  'red': 31
@@ -447,11 +471,15 @@ Program.prototype.colorize = function (noColors) {
447
471
  Object.keys(colors).forEach(function (color) {
448
472
  Object.defineProperty(String.prototype, color,
449
473
  { get: function () {
450
- if (noColors || !self.opts.useColors) return this;
474
+ if (noColors || !self.opts.useColors) return this + '';
451
475
  return '\033[' + colors[color] + 'm' + this + '\033[0m';
452
- }
476
+ }
477
+ , configurable: true
453
478
  });
454
479
  });
480
+
481
+ this._colorized = true;
482
+ return this;
455
483
  };
456
484
 
457
485
  /*!
@@ -497,22 +525,22 @@ function mountCommands () {
497
525
  * @api private
498
526
  */
499
527
 
500
- function displayVersion () {
528
+ function displayVersion() {
501
529
  process.stdout.write(this.opts.version + '\n');
502
530
  process.exit();
503
531
  }
504
532
 
505
533
  /*!
506
- * displayHelp ()
534
+ * ### displayHelp()
507
535
  *
508
536
  * Execute the custom help theme or find the respective
509
537
  * electron theme and execute.
510
538
  *
511
539
  * @ctx program
512
- * @api private
540
+ * @api public
513
541
  */
514
542
 
515
- function displayHelp () {
543
+ Program.prototype.showHelp = function () {
516
544
  if ('function' === typeof this.opts.theme)
517
545
  return this.opts.theme.call(this);
518
546
 
package/lib/electron.js CHANGED
@@ -27,13 +27,14 @@ exports.defineProgram = defineProgram;
27
27
  * Electron version
28
28
  */
29
29
 
30
- exports.version = '0.2.0';
30
+ exports.version = '0.4.0';
31
31
 
32
32
  /*!
33
33
  * Argv parsing factory
34
34
  */
35
35
 
36
36
  exports.argv = function (args) {
37
+ args = args || process.argv;
37
38
  var argv = new Args(args);
38
39
  return argv;
39
40
  };
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "author": "Jake Luer <jake@alogicalparadox.com>",
3
3
  "name": "electron",
4
- "description": "Event-based tiny CLI famework.",
5
- "version": "0.2.0",
4
+ "description": "A simple command-line interface framework for node.js.",
5
+ "keywords": [ "cli", "command", "option", "parser", "argv" ],
6
+ "homepage": "http://alogicalparadox.com/electron",
7
+ "license": "MIT",
8
+ "version": "0.4.1",
6
9
  "repository": {
7
10
  "type": "git",
8
11
  "url": "git://github.com/logicalparadox/electron.git"
@@ -12,7 +15,7 @@
12
15
  "test": "make test"
13
16
  },
14
17
  "dependencies": {
15
- "drip": "0.3.x"
18
+ "drip": "1.1.x"
16
19
  },
17
20
  "devDependencies": {
18
21
  "mocha": "*"
@@ -1,30 +0,0 @@
1
- {
2
- "locals": {
3
- "title": "Electron"
4
- }
5
-
6
- , "plugins": [
7
- { "name": "code"
8
- , "files": [
9
- { "name": "index"
10
- , "title": "Electron Argument Parsing"
11
- , "file": "../../lib/electron/args.js"
12
- , "description": ":)"
13
- , "template": "index"
14
- , "render-file": false }
15
- , { "name": "index"
16
- , "title": "Electron Program Framework"
17
- , "file": "../../lib/electron/program.js"
18
- , "description": ":)"
19
- , "template": "index"
20
- , "render-file": false }
21
- , { "name": "index"
22
- , "title": "Electron Command Construction"
23
- , "file": "../../lib/electron/command.js"
24
- , "description": ":)"
25
- , "template": "index"
26
- , "render-file": false }
27
- ]
28
- }
29
- ]
30
- }
@@ -1,19 +0,0 @@
1
- ---
2
- title: Electron
3
- ---
4
-
5
- > A tiny cli framework for node.js.
6
-
7
- ## Features
8
-
9
- - tbd
10
-
11
- ## Installation
12
-
13
- ##### Node
14
-
15
- `electron` package is available through [npm](http://npmjs.org).
16
-
17
- ```bash
18
- npm install electron
19
- ```