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));});
Command Line Arguments Parser
cli takes an object as a map for the arguments you wish to parse.
Each property/key in the object is the long version of the argument i.e. --file
The array associated with it is the options to apply to that argument.
Example
cli.parse({ file: [ 'f', 'A file to process', 'file', temp.log ], // -f, --file FILE A file to process time: [ 't', 'An access time', 'time', false], // -t, --time TIME An access time work: [ false, 'What kind of work to do', 'string', 'sleep' ] // --work STRING What kind of work to do});
Explanation of array options
A short name, single letter i.e. -f, or false if no short name is supported for this option
A description of the option
The type of object the argument should map too.
Below is a list of the return types followed by a description and a list of
valid values you can use for this option to get desired type of Object back.
as-is: What you enter, is what you get
- 'string', 1, true
int: Is converted to an Integer wrapped in a Number Object
- 'int', 'number', 'num',
- 'time', 'seconds', 'secs', 'minutes', 'mins'
- 'x', 'n'
date: Is converted to a Date Object
- 'date', 'datetime', 'date_time'
- float: Is converted to a Float wrapped in a Number Object
- 'float', 'decimal'
- file: Is converted to a String Object if it is a valid path
- 'path', 'file', 'directory', 'dir'
- email: Converted to a String Object if it is a valid email format
- 'email'
- url: Converted to a String Object if it is a valid URL format
- 'url', 'uri', 'domain', 'host'
ip: Converted to a String Object if it is a valid IP Address format
- 'ip'
true: Converted to true if argument is present on command line
- 'bool', 'boolean', 'on'
- false: Converted to false if argument is present on command line
- 'false', 'off', false, 0
A default value for this option if one is not given on the command line
Helper methods
cli has methods that collect stdin (newline is auto-detected as \n or \r\n)
cli.withStdin(callback); //callback receives stdin as a stringcli.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 for an example).
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
//cli.toType(object); If a Built-in type, returns the name of the type as a lower cased Stringcli.toType([]); // 'array'cli.toType(new Date()); // 'date'cli.toType(1); // 'integer'cli.toType(1.1); // 'float'cli.toType(Math); // 'math'cli.toType(/a/); // 'regex'cli.toType(JSON); // 'json'
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.