package/package.json000644 000765 000024 0000000631 12143217774013025 0ustar00000000 000000 { "name": "argv", "description": "CLI Argument Parser", "url": "http://codenothing.github.com/argv/", "keywords": [ "cli", "argv", "options" ], "author": "Corey Hart ", "version": "0.0.2", "main": "./index.js", "engines": { "node": ">=0.6.10" }, "scripts": { "test": "make test" }, "dependencies": { }, "devDependencies": { "nlint": "0.0.4", "munit": "0.0.5" } } package/.npmignore000644 000765 000024 0000000035 12143217774012534 0ustar00000000 000000 node_modules/ build/results/ package/README.md000644 000765 000024 0000010354 12143217774012021 0ustar00000000 000000 # argv argv is a nodejs module that does command line argument parsing. [![Build Status](https://travis-ci.org/codenothing/argv.png?branch=master)](https://travis-ci.org/codenothing/argv) ### Installation ```bash $ npm install argv ``` ### Usage ```js var argv = require( 'argv' ); var args = argv.option( options ).run(); -> { targets: [], options: {} } ``` ### Run Runs the argument parser on the global arguments. Custom arguments array can be used by passing into this method ```js // Parses default arguments 'process.argv.slice( 2 )' argv.run(); // Parses array instead argv.run([ '--option=123', '-o', '123' ]); ``` ### Options argv is a strict argument parser, which means all options must be defined before parsing starts. ```js argv.option({ name: 'option', short: 'o', type: 'string', description: 'Defines an option for your script', example: "'script --opiton=value' or 'script -o value'" }); ``` ### Modules Modules are nested commands for more complicated scripts. Each module has it's own set of options that have to be defined independently of the root options. ```js argv.mod({ mod: 'module', description: 'Description of what the module is used for', options: [ list of options ] }); ``` ### Types Types convert option values to useful js objects. They are defined along with each option. * **string**: Ensure values are strings * **path**: Converts value into a fully resolved path. * **int**: Converts value into an integer * **float**: Converts value into a float number * **boolean**: Converts value into a boolean object. 'true' and '1' are converted to true, everything else is false. * **csv**: Converts value into an array by splitting on comma's. * **list**: Allows for option to be defined multiple times, and each value added to an array * **[list|csv],[type]**: Combo type that allows you to create a list or csv and convert each individual value into a type. ```js argv.option([ { name: 'option', type: 'csv,int' }, { name: 'path', short: 'p', type: 'list,path' } ]); // csv and int combo $ script --option=123,456.001,789.01 -> option: [ 123, 456, 789 ] // list and path combo $ script -p /path/to/file1 -p /path/to/file2 -> option: [ '/path/to/file1', '/path/to/file2' ] ``` You can also create your own custom type for special conversions. ```js argv.type( 'squared', function( value ) { value = parseFloat( value ); return value * value; }); argv.option({ name: 'square', short: 's', type: 'squared' }); $ script -s 2 -> 4 ``` ### Version Defining the scripts version number will add the version option and print it out when asked. ```js argv.version( 'v1.0' ); $ script --version v1.0 ``` ### Info Custom information can be displayed at the top of the help printout using this method ```js argv.info( 'Special script info' ); $ script --help Special script info ... Rest of Help Doc ... ``` ### Clear If you have competing scripts accessing the argv object, you can clear out any previous options that may have been set. ```js argv.clear().option( [new options] ); ``` ### Help argv injects a default help option initially and on clears. The help() method triggers the help printout. ```js argv.help(); ``` ---- ### License ``` The MIT License Copyright (c) 2012-2013 Corey Hart 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. ``` package/LICENSE000644 000765 000024 0000002064 12143217774011546 0ustar00000000 000000 The MIT License Copyright (c) 2012-2013 Corey Hart 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. package/index.js000644 000765 000024 0000000055 11776234273012210 0ustar00000000 000000 module.exports = require( './lib/argv.js' ); package/lib/argv.js000644 000765 000024 0000025510 12146111461012572 0ustar00000000 000000 var PATH = require( 'path' ), toString = Object.prototype.toString, rhome = /^\~\//, rroot = /^\//, rlistcsv = /^(list|csv)\,[a-z]+$/, rdash = /^\-/, rddash = /^\-\-/, ristarget = /^[^\-]/, SCRIPT_NAME = ( process.argv[ 1 ] || '' ).split( '/' ).pop(), helpOption = { name: 'help', short: 'h', type: function(){ return true; }, description: 'Displays help information about this script', example: "'" + SCRIPT_NAME + " -h' or '" + SCRIPT_NAME + " --help'", onset: function( args ) { self.help( args.mod ); process.exit( 0 ); } }, boolTest = function( value ) { return value == 'true' || value == 'false' || value == '1' || value == '0'; }, self; // argv module module.exports = self = { // Default script name name: SCRIPT_NAME, // Default description to the triggered script 'node script' description: 'Usage: ' + SCRIPT_NAME + ' [options]', // Modules mods: {}, // Shorthand options short: { h: helpOption }, // Options options: { help: helpOption }, // List of common types types: { string: function( value ) { return value.toString(); }, path: function( value ) { var end = value[ value.length - 1 ] == '/'; if ( rhome.exec( value ) ) { value = PATH.normalize( process.env.HOME + '/' + value.replace( rhome, '' ) ); } else if ( ! rroot.exec( value ) ) { value = PATH.normalize( process.cwd() + '/' + value ); } return value + ( end && value[ value.length - 1 ] != '/' ? '/' : '' ); }, 'int': function( value ) { return parseInt( value, 10 ); }, 'float': function( value ) { return parseFloat( value, 10 ); }, 'boolean': function( value ) { return ( value == 'true' || value === '1' ); }, list: function( value, name, options ) { if ( ! options[ name ] ) { options[ name ] = []; } options[ name ].push( value ); return options[ name ]; }, csv: function( value ) { return value.split( ',' ); }, 'listcsv-combo': function( type ) { var parts = type.split( ',' ), primary = parts.shift(), secondary = parts.shift(); return function( value, name, options, args ) { // Entry is going to be an array if ( ! options[ name ] ) { options[ name ] = []; } // Channel to csv or list if ( primary == 'csv' ) { value.split( ',' ).forEach(function( val ) { options[ name ].push( self.types[ secondary ]( val, name, options, args ) ); }); } else { options[ name ].push( self.types[ secondary ]( value, name, options, args ) ); } return options[ name ]; }; } }, // Creates custom type function type: function( name, callback ) { if ( self.isObject( name ) ) { for ( var i in name ) { if ( self.isFunction( name[ i ] ) ) { self.types[ i ] = name[ i ]; } } } else if ( callback === undefined ) { return self.types[ name ]; } else if ( self.isFunction( callback ) ) { self.types[ name ] = callback; } else if ( callback === null && self.types.hasOwnProperty( name ) ) { delete self.types[ name ]; } return self; }, // Setting version number, and auto setting version option version: function( v ) { self.option({ _version: v, name: 'version', type: function(){ return true; }, description: 'Displays version info', example: self.name + " --version", onset: function( args ) { console.log( v + "\n" ); process.exit( 0 ); } }); return self; }, // Adding options to definitions list option: function( mod, object ) { if ( object === undefined ) { object = mod; mod = undefined; } // Iterate over array for multi entry if ( self.isArray( object ) ) { object.forEach(function( entry ) { self.option( mod, entry ); }); } // Handle edge case else if ( ! self.isObject( object ) ) { throw new Error( 'No option definition provided' + ( mod ? ' for module ' + mod : '' ) ); } // Handle module definition else if ( object.mod ) { self.mod( object ); } // Correct the object else { if ( ! object.name ) { throw new Error( 'No name provided for option' ); } else if ( ! object.type ) { throw new Error( 'No type proveded for option' ); } // Attach tester for value on booleans // to avoid false targets object.test = object.test || ( object.type == 'boolean' ? boolTest : null ); object.description = object.description || ''; object.type = self.isFunction( object.type ) ? object.type : self.isString( object.type ) && rlistcsv.exec( object.type ) ? self.types[ 'listcsv-combo' ]( object.type ) : self.isString( object.type ) && self.types[ object.type ] ? self.types[ object.type ] : self.types.string; // Apply to module if ( mod ) { if ( ! self.mods[ mod ] ) { self.mods[ mod ] = { mod: mod, options: {}, short: {} }; } // Attach option to submodule mod = self.mods[ mod ]; mod.options[ object.name ] = object; // Attach shorthand if ( object.short ) { mod.short[ object.short ] = object; } } // Apply to root options else { self.options[ object.name ] = object; // Attach shorthand option if ( object.short ) { self.short[ object.short ] = object; } } } return self; }, // Creating module mod: function( object ) { var mod; // Allow multi mod setup if ( self.isArray( object ) ) { object.forEach(function( value ) { self.mod( value ); }); } // Handle edge case else if ( ! self.isObject( object ) ) { throw new Error( 'No mod definition provided' ); } // Force mod name else if ( ! object.mod ) { throw new Error( "Expecting 'mod' entry for module" ); } // Create object if not already done so else if ( ! self.mods[ object.mod ] ) { self.mods[ object.mod ] = { mod: object.mod, options: {}, short: {} }; } // Setup mod = self.mods[ object.mod ]; mod.description = object.description || mod.description; // Attach each option self.option( mod.mod, object.options ); return self; }, // Cleans out current options clear: function(){ var version = self.options.version; // Clean out modes and reapply help option self.short = {}; self.options = {}; self.mods = {}; self.option( helpOption ); // Re-apply version if set if ( version ) { self.option( version ); } return self; }, // Description setup info: function( mod, description ) { if ( description === undefined ) { self.description = mod; } else if ( self.mods[ mod ] ) { self.mods[ mod ] = description; } return self; }, // Prints out the help doc help: function( mod ) { var output = [], name, option; // Printing out just a module's definitions if ( mod && ( mod = self.mods[ mod ] ) ) { output = [ '', mod.description, '' ]; for ( name in mod.options ) { option = mod.options[ name ]; output.push( "\t--" +option.name + ( option.short ? ', -' + option.short : '' ) ); output.push( "\t\t" + option.description ); if ( option.example ) { output.push( "\t\t" + option.example ); } // Spacing output.push( "" ); } } // Printing out just the root options else { output = [ '', self.description, '' ]; for ( name in self.options ) { option = self.options[ name ]; output.push( "\t--" +option.name + ( option.short ? ', -' + option.short : '' ) ); output.push( "\t\t" + option.description ); if ( option.example ) { output.push( "\t\t" + option.example ); } // Spacing output.push( "" ); } } // Print out the output console.log( output.join( "\n" ) + "\n\n" ); return self; }, // Runs the arguments parser _run: function( argv ) { var args = { targets: [], options: {} }, opts = self.options, shortOpts = self.short, skip = false; // Allow for passing of arguments list argv = self.isArray( argv ) ? argv : process.argv.slice( 2 ); // Switch to module's options when used if ( argv.length && ristarget.exec( argv[ 0 ] ) && self.mods[ argv[ 0 ] ] ) { args.mod = argv.shift(); opts = self.mods[ args.mod ].options; shortOpts = self.mods[ args.mod ].short; } // Iterate over arguments argv.forEach(function( arg, i ) { var peek = argv[ i + 1 ], option, index, value; // Allow skipping of arguments if ( skip ) { return ( skip = false ); } // Full option '--option' else if ( rddash.exec( arg ) ) { arg = arg.replace( rddash, '' ); // Default no value to true if ( ( index = arg.indexOf( '=' ) ) !== -1 ) { value = arg.substr( index + 1 ); arg = arg.substr( 0, index ); } else { value = 'true'; } // Be strict, if option doesn't exist, throw and error if ( ! ( option = opts[ arg ] ) ) { throw "Option '--" + arg + "' not supported"; } // Send through type converter args.options[ arg ] = option.type( value, arg, args.options, args ); // Trigger onset callback when option is set if ( self.isFunction( option.onset ) ) { option.onset( args ); } } // Shorthand option '-o' else if ( rdash.exec( arg ) ) { arg = arg.replace( rdash, '' ); if ( arg.length > 1 ) { arg.split( '' ).forEach(function( character ) { if ( ! ( option = shortOpts[ character ] ) ) { throw "Option '-" + character + "' not supported"; } args.options[ option.name ] = option.type( 'true', option.name, args.options, args ); }); } else { // Ensure that an option exists if ( ! ( option = shortOpts[ arg ] ) ) { throw "Option '-" + arg + "' not supported"; } // Check next option for target association if ( peek && option.test && option.test( peek, option.name, args.options, args ) ) { value = peek; skip = true; } else if ( peek && ! option.test && ristarget.exec( peek ) ) { value = peek; skip = true; } else { value = 'true'; } // Convert it args.options[ option.name ] = option.type( value, option.name, args.options, args ); // Trigger onset callback when option is set if ( self.isFunction( option.onset ) ) { option.onset( args ); } } } // Targets else { args.targets.push( arg ); } }); return args; }, run: function( argv ) { try { return self._run( argv ); } catch ( e ) { if ( ! self.isString( e ) ) { throw e; } console.log( "\n" + e + ". Trigger '" + self.name + " -h' for more details.\n\n" ); process.exit( 1 ); } } }; // Type tests "Boolean Number String Function Array Date RegExp Object Error".split(' ').forEach(function( method ) { if ( method == 'Array' ) { return ( self.isArray = Array.isArray ); } else if ( method == 'Error' ) { self.isError = function( object ) { return object && ( object instanceof Error ); }; return; } var match = '[object ' + method + ']'; self[ 'is' + method ] = function( object ) { return object !== undefined && object !== null && toString.call( object ) == match; }; }); package/.travis.yml000644 000765 000024 0000000045 12143217774012647 0ustar00000000 000000 language: node_js node_js: - "0.8" package/build/lint.js000644 000765 000024 0000000061 12000504215013114 0ustar00000000 000000 require( 'nlint' ).render( __dirname + '/../' ); package/build/test.js000644 000765 000024 0000000541 12143217774013153 0ustar00000000 000000 global.munit = require( 'munit' ); global.argv = require( '../' ); // Only stop test suite when running make test if ( ! process.env.NODE_TEST_NO_SKIP ) { munit.defaults.settings.stopOnFail = true; } // Render all tests munit.render( __dirname + '/../test/', { junit: __dirname + '/results/', junitPrefix: process.version.replace( /\./g, '_' ) }); package/build/full.sh000755 000765 000024 0000001026 12143217774013136 0ustar00000000 000000 #!/bin/bash cd `dirname $0` cd ../ SRCROOT=${PWD} # Clean out and rebuild before running through each enviorment make clean echo "===== NVM DIR::: $NVM_DIR ====" # Find each availiable node version and test with that version for i in $( ls $NVM_DIR ) do if [[ $i =~ ^v ]]; then echo "" echo "" echo "=== Node $i ===" echo "" # Run test suite "$NVM_DIR/$i/bin/node" "$SRCROOT/build/test.js" # Any non successful exit should be treated as full error RESULT=$? if [[ $RESULT != 0 ]]; then exit 1 fi fi done package/Makefile000644 000765 000024 0000000266 12143217774012203 0ustar00000000 000000 all: test clean: @rm -rf build/results/ lint: @node build/lint.js test: clean lint @node build/test.js test-all: @NODE_TEST_NO_SKIP=1 make test test-full: @./build/full.sh package/CHANGELOG.md000644 000765 000024 0000000142 12146112401012325 0ustar00000000 000000 ## v0.0.2 * Transfering help and version option after calling clear ## v0.0.1 Initial Release package/.nodelint.json000644 000765 000024 0000000132 12143217774013320 0ustar00000000 000000 { "ignore": [ "node_modules/" ], "linters": { "jshint": { "boss": true } } } package/test/test-core.js000644 000765 000024 0000004726 12143217774013772 0ustar00000000 000000 munit( 'Root.Options', function( assert ) { argv.clear().option([ { name: 'bool', short: 'b', type: 'boolean' }, { name: 'path', short: 'p', type: 'path' }, { name: 'int', short: 'i', type: 'int' }, { name: 'float', short: 'f', type: 'float' }, { name: 'csv', short: 'c', type: 'csv' }, { name: 'list', short: 'l', type: 'list' }, { name: 'listpath', short: 't', type: 'list,path' }, { name: 'csvcombo', type: 'csv,int' }, { name: 'bool2', short: 'x', type: 'boolean' }, { name: 'bool3', short: 'y', type: 'boolean' }, { name: 'bool4', short: 'z', type: 'boolean' } ]); // Tests [ { name: 'basic', args: '-b false --path=/a/b/c -i 123.456 --float=123.456 -c a,b,c -l a -l b -l c', options: { bool: false, path: '/a/b/c', 'int': 123, 'float': 123.456, csv: [ 'a', 'b', 'c' ], list: [ 'a', 'b', 'c' ] } }, { name: 'path-relative', args: '-p a/b/c', options: { path: process.cwd() + '/a/b/c' } }, { name: 'path-home', args: '-p ~/a/b/c', options: { path: process.env.HOME + '/a/b/c' } }, { name: 'listpath', args: '-t ~/a/b/c -t /a/b/c -t a/b/c', options: { listpath: [ process.env.HOME + '/a/b/c', '/a/b/c', process.cwd() + '/a/b/c' ] } }, { name: 'csvcombo', args: '--csvcombo=123,456.99,0.00001', options: { csvcombo: [ 123, 456, 0 ] } }, { name: 'targets', args: 'targ1 -p /a/b/c targ2 targ3 --bool targ4', targets: [ 'targ1', 'targ2', 'targ3', 'targ4' ], options: { path: '/a/b/c', bool: true } }, { name: 'bool-target-test', args: 'targ1 -b targ2', targets: [ 'targ1', 'targ2' ], options: { bool: true } }, { name: 'bool-multi-option', args: 'targ1 -bxy targ2 -z targ3', targets: [ 'targ1', 'targ2', 'targ3' ], options: { bool: true, bool2: true, bool3: true, bool4: true } } ].forEach(function( object ) { var args = argv.run( argv.isArray( object.args ) ? object.args : object.args.split( ' ' ) ); // Option parsing assert.deepEqual( object.name + '::options', args.options, object.options ); // Target comparison if ( object.targets ) { assert.deepEqual( object.name + '::targets', args.targets, object.targets ); } else { assert.ok( object.name + '::targets', ! args.targets.length ); } }); }); package/test/test-custom.js000644 000765 000024 0000002325 12143217774014345 0ustar00000000 000000 munit( 'Custom', function( assert ) { // Add custom types argv.type({ 'special-int': function(){ return 777; }, 'special-float': function(){ return 777.777; } }); // Setup options argv.clear().option([ { name: 'foo', type: 'special-int' }, { name: 'bar', type: 'special-float' } ]); // Tests [ { name: 'types', args: '--foo=123 --bar=456', options: { foo: 777, bar: 777.777 } } ].forEach(function( object ) { var args = argv.run( argv.isArray( object.args ) ? object.args : object.args.split( ' ' ) ); // Option parsing assert.deepEqual( object.name + '::options', args.options, object.options ); // Target comparison if ( object.targets ) { assert.deepEqual( object.name + '::targets', args.targets, object.targets ); } else { assert.ok( object.name + '::targets', ! args.targets.length ); } }); // Deletion checks assert.ok( 'Int Exists', argv.types[ 'special-int' ] ); assert.ok( 'Float Exists', argv.types[ 'special-float' ] ); argv.type( 'special-int', null ); argv.type( 'special-float', null ); assert.ok( 'Delete Int', ! argv.types[ 'special-int' ] ); assert.ok( 'Delete Float', ! argv.types[ 'special-float' ] ); }); package/test/test-mods.js000644 000765 000024 0000003330 12143217774013772 0ustar00000000 000000 munit( 'Modules', function( assert ) { argv.clear().option([ { name: 'bool', short: 'a', type: 'boolean' }, { name: 'bool2', short: 'b', type: 'boolean' }, { name: 'bool3', short: 'c', type: 'boolean' }, { name: 'path', short: 'p', type: 'path' }, { mod: 'mod', options: [ { name: 'bool', short: 'a', type: 'boolean' }, { name: 'bool2', short: 'b', type: 'boolean' }, { name: 'bool3', short: 'c', type: 'boolean' }, { name: 'path', short: 'p', type: 'path' } ] } ]); [ { name: 'root', args: 'targ1 -p /a/b/c targ2 -abc targ3', targets: [ 'targ1', 'targ2', 'targ3' ], options: { path: '/a/b/c', bool: true, bool2: true, bool3: true } }, { name: 'mod', args: 'mod targ1 -ab', mod: 'mod', targets: [ 'targ1' ], options: { bool: true, bool2: true } }, { name: 'mod-path', args: 'mod targ1 -p /a/b/c', mod: 'mod', targets: [ 'targ1' ], options: { path: '/a/b/c' } } ].forEach(function( object ) { var args = argv.run( argv.isArray( object.args ) ? object.args : object.args.split( ' ' ) ); // Option parsing assert.deepEqual( object.name + '::options', args.options, object.options ); // Mod comparison if ( object.mod ) { assert.equal( object.name + '::mod', args.mod, object.mod ); } else { assert.ok( object.name + '::mod', ! args.mod ); } // Target comparison if ( object.targets ) { assert.deepEqual( object.name + '::targets', args.targets, object.targets ); } else { assert.ok( object.name + '::targets', ! args.targets.length ); } }); }); package/test/test-version.js000644 000765 000024 0000000604 12143217774014516 0ustar00000000 000000 munit( 'Root.version', { priority: munit.PRIORITY_LOW }, function( assert ) { argv.clear(); if ( argv.options.version ) { delete argv.options.version; } assert.empty( 'Version Start', argv.options.version ); argv.version( "1.2.4" ); assert.exists( 'Version Option', argv.options.version ); argv.clear(); assert.exists( 'Version Option after Clear', argv.options.version ); });