Socket
Socket
Sign inDemoInstall

cli

Package Overview
Dependencies
Maintainers
0
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cli - npm Package Compare versions

Comparing version 0.3.6 to 0.3.7

4

cli.js

@@ -636,3 +636,3 @@ /**

line += optional ? ' (Default is ' + optional + ')' : '';
console.error(line);
console.error(line.replace('%s', '%\0s'));

@@ -745,3 +745,3 @@ seen_opts.push(short);

}
return default_val || cli.fatal(err_msg);
return default_val != null ? default_val : cli.fatal(err_msg);
}

@@ -748,0 +748,0 @@ return value;

@@ -25,2 +25,2 @@ #!/usr/bin/env node

this.ok('Listening on port ' + options.port);
});
});
{ "name" : "cli",
"description" : "A tool for rapidly building command line apps",
"version" : "0.3.6",
"version" : "0.3.7",
"homepage" : "http://github.com/chriso/cli",

@@ -5,0 +5,0 @@ "keywords" : ["cli","command line","opts","parseopt","opt","args","console","argsparse","optparse","daemon","autocomplete","command","autocompletion"],

@@ -15,58 +15,70 @@ **cli is a toolkit for rapidly building command line apps - it includes:**

#!/usr/bin/env node
require('cli').withStdinLines(function(lines, newline) {
this.output(lines.sort().join(newline));
});
```javascript
#!/usr/bin/env node
require('cli').withStdinLines(function(lines, newline) {
this.output(lines.sort().join(newline));
});
```
Try it out
$ ./sort.js < input.txt
```bash
$ ./sort.js < input.txt
```
Let's add support for an `-n` switch to use a numeric sort, and a `-r` switch to reverse output - only 5 extra lines of code (!)
var cli = require('cli'), options = cli.parse();
cli.withStdinLines(function(lines, newline) {
lines.sort(!options.n ? null : function(a, b) {
return parseInt(a) > parseInt(b);
});
if (options.r) lines.reverse();
this.output(lines.join(newline));
```javascript
var cli = require('cli'), options = cli.parse();
cli.withStdinLines(function(lines, newline) {
lines.sort(!options.n ? null : function(a, b) {
return parseInt(a) > parseInt(b);
});
if (options.r) lines.reverse();
this.output(lines.join(newline));
});
```
### static.js
Let's create a static file server with daemon support to see the opts parser + plugins in use - note: this requires `npm install creationix daemon`
var cli = require('cli').enable('daemon', 'status'); //Enable 2 plugins
```javascript
var cli = require('cli').enable('daemon', 'status'); //Enable 2 plugins
cli.parse({
log: ['l', 'Enable logging'],
port: ['p', 'Listen on this port', 'number', 8080],
serve: [false, 'Serve static files from PATH', 'path', './public']
});
cli.parse({
log: ['l', 'Enable logging'],
port: ['p', 'Listen on this port', 'number', 8080],
serve: [false, 'Serve static files from PATH', 'path', './public']
});
cli.main(function(args, options) {
var server, middleware = [];
if (options.log) {
this.debug('Enabling logging');
middleware.push(require('creationix/log')());
}
cli.main(function(args, options) {
var server, middleware = [];
this.debug('Serving files from ' + options.serve);
middleware.push(require('creationix/static')('/', options.serve, 'index.html'));
server = this.createServer(middleware).listen(options.port);
this.ok('Listening on port ' + options.port);
});
if (options.log) {
this.debug('Enabling logging');
middleware.push(require('creationix/log')());
}
this.debug('Serving files from ' + options.serve);
middleware.push(require('creationix/static')('/', options.serve, 'index.html'));
server = this.createServer(middleware).listen(options.port);
this.ok('Listening on port ' + options.port);
});
```
To output usage information
$ ./static.js --help
```bash
$ ./static.js --help
```
To create a daemon that serves files from */tmp*, run
$ ./static.js -ld --serve=/tmp
```bash
$ ./static.js -ld --serve=/tmp
```

@@ -79,22 +91,30 @@ For more examples, see [./examples](https://github.com/chriso/cli/tree/master/examples)

cli.withStdin(callback); //callback receives stdin as a string
cli.withStdinLines(callback); //callback receives stdin split into an array of lines (lines, newline)
```javascript
cli.withStdin(callback); //callback receives stdin as a string
cli.withStdinLines(callback); //callback receives stdin split into an array of lines (lines, newline)
```
cli also has a lower level method for working with input line by line (see [./examples/cat.js](https://github.com/chriso/cli/blob/master/examples/cat.js) for an example).
cli.withInput(file, function (line, newline, eof) {
if (!eof) {
this.output(line + newline);
}
});
```javascript
cli.withInput(file, function (line, newline, eof) {
if (!eof) {
this.output(line + newline);
}
});
```
*Note: `file` can be omitted if you want to work with stdin*
To output a progress bar, call
To output a progress bar, call
cli.progress(progress); //Where 0 <= progress <= 1
```javascript
cli.progress(progress); //Where 0 <= progress <= 1
```
To spawn a child process, use
cli.exec(cmd, callback); //callback receives the output of the process (split into lines)
```javascript
cli.exec(cmd, callback); //callback receives the output of the process (split into lines)
```

@@ -106,5 +126,7 @@ cli also comes bundled with kof's [node-natives](https://github.com/kof/node-natives) (access with cli.native) and creationix' [stack](https://github.com/creationix/stack) (access with cli.createServer)

Plugins are a way of adding common opts and can be enabled using
cli.enable(plugin1, [plugin2, ...]); //To disable, use the equivalent disable() method
```javascript
cli.enable(plugin1, [plugin2, ...]); //To disable, use the equivalent disable() method
```
**help** - *enabled by default*

@@ -124,7 +146,9 @@

cli.debug(msg); //Only shown when using --debug
cli.error(msg);
cli.fatal(msg); //Exits the process after outputting msg
cli.info(msg);
cli.ok(msg);
```javascript
cli.debug(msg); //Only shown when using --debug
cli.error(msg);
cli.fatal(msg); //Exits the process after outputting msg
cli.info(msg);
cli.ok(msg);
```

@@ -134,7 +158,7 @@ `-k,--no-color` will omit ANSI color escapes from the output

**glob** - *requires* `npm install glob`
Enables glob matching of arguments
**daemon** - *requires* `npm install daemon`
Adds `-d,--daemon ARG` for daemonizing the process and controlling the resulting daemon

@@ -177,2 +201,2 @@

OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc