ArguMints
A Powerful Command Line Arguments Preprocessor and Data Mining Tool
Benefits
CLI
- argv
- opt
- flags
- argc
- keyValue
Data Conversion
- convert cli input (argv or keyValue) into JS primitives automatically.
Data Mining
- Supports JSON input (in either argv or keyValue formats) which are converted into deep JS Objects, that you can mine.
- aggregate file content using the '@' file marker
- search argument input using regular expression arguments
Robustness
- Uses Underscore JS for mining and copying / filtering / selection techniques.
Usage
Default ArguMints Instance
var myArgs = myArguMints;
Roll your own instance
var lib = ; var myArgs = ;
- or -
// the one liner var myArgs = 'argumints';
Basic Examples
NOTE: Examples all from test materials included with ArguMints package!
Options, Flags, and Argv
// cmd line equivalent // C:\argumints> node argumints-test.js -xzfv --option arg0 arg1 // NOTE: verbose is true here because failing a check for an option will // revert to checking for the corresponding flag (usign first char of option name) myArgs; console; // true console; // true console; // true console; // true console; // arg0 console; // arg1 console; // 2
The Key=Value Store
// cmd line equivalent // C:\argumints> node argumints-test.js x=2 y=3 myArgs; console; console;
Advanced Examples
Text File Expansion
// cmd line equivalent // C:\argumints> node argumints-test.js @test/testargs2.json myArgs; console; // output the text file content
Test Materials:
JSON Expansion in line
JSON File Expansion works like regular File Expansion, with the additional benefit of grafting the resulting JSON Data into either the keyStore, or onto the argv list after expansion, depending on how you structured the input ArguMint (see what I did there??).
// Ex. 1 // cmd line equivalent // C:\argumints> node argumints-test.js '{\"aProperty\":\"testValue\"}' myArgs; console; // testValue // Ex. 2 myArgs; console; // testValue // Ex. 3 // cmd line equivalent // C:\argumints> node argumints-test.js [1,2,3,4] myArgs; console; // 3
JSON File Expansion
// cmd line equivalent // C:\argumints> node argumints-test.js @test/testargs1.json myArgs; console; // 1 console; // true console; // buckle console; // aProperty console; // null
Test Materials:
Recursive JSON File Expansion
// cmd line equivalent // C:\argumints> node argumints-test.js @test/testargs2.json myArgs; console; console;
Test Materials:
Recursive JSON File Expansion and Mining (Advanced)
// cmd line equivalent // C:\argumints> node argumints-test.js @test/testargs3.json myArgs; console; console; // dig in to the values... console; console; console; console;
Test Materials: - all files below were loaded because of templatization starting with testargs3.json
Bulk File Expansion
Expansion in Array
ArguMintsverbose = true; // append some more 'command line' args. myArgs; //results //test.txt contents loaded into the first array slot. //testargs1.json contents loaded, expanded into a JSON object // and placed into the second array slot. // the resulting array is placed in the first slot of // Argumints.commandTable.argList // (since it has no key=value) format, and is not a flag.
Test Materials:
Expansion as a Key=Value store
ArguMintsverbose = true; // append some more 'command line' args. myArgs; //results //test.txt contents are stored in the command table with key 'x'. //testargs1.json contents are expanded into a JSON Object, and stored // in the command table with key 'y'
Test Materials:
Circular Expansion Protection
If you accidentally circularly reference files during an expansion chain, you'll get an exception as show below. This avoids infinite loops and stack overflows when doing really crazy things with ArguMints.
ArguMintsverbose = true; myArgs; //results // file refers to itself by having its contents be a valid url expansion, pointing to its own url. // an ArguMintsException is thrown due to Circular Expansion detection. // Each Argument is individually protected against circular expansion, so you CAN refer to the same file twice, as long as // its under different argument expansions.
Test Materials:
Notes on File Expansion
Files are expanded using the '@' character to denote resource location. This is either an absolute or relative local resource. And can be a Windows or Linux Path. Windows paths that have Spaces in the name must be surrounded by quotes!
The Following Formats are acceptable.
"@C:\Program Files\Blah"
"@Program Files\Blah"
@..\another\place\for\file.txt
@my/file/is/relative.txt
@/abs/linux/path/or/root/of/curr/win/drive
@\root\of\curr\win\drive
@relative\win\path
File contents can be in JSON format, which means their content will be expanded into a JavaScript object. Any properties of said object that are of String type, and follow the file expansion nomenclature will also be expanded. Circular Expansion is not allowed.
Regular Expressions
using '--minty-match-argv' option we perform aggregate data mining and data gathering.
NOTE: Regular expressions must be surrounded by `` backticks to alert ArguMints that this is indeed treated as RegEx (and not a file path like /hey/g).
CLI
// match all instances of the substring 'ord' or 'famil' within the two textfiles provided. // matches are cumulative. myArgs; console; // result /* [ 'ord', 'word', 'ordinal', 'ordinary', 'ordered', 'ordained', 'ford', 'Nordic', 'family', 'Family', 'families', 'Familiar', 'FaMiLy', 'FaMiLiAr' ] */
Test Materials:
From File
You can save a regExp in a file and readily use it as a JavaScript RegExp() object.
// cmd line argumints-test.js --minty-match-argv @test/testMatcher.txt @test/testMatcher2.txt @test/regExpOrd.txt myArgs; console;
Test Materials:
OR USING KEY VALUE STORE
// cmd line argumints-test.js --minty-match-kv @test/regExp.txt @test/testMatcher.txt @test/testMatcher2.txt myArgs; console; // use the reg ex again and again! var regExp = myArgs; console; //['myemail@address.com']
Test Materials:
The Options {} Object:
You may pass an options
object to ArguMints
so it will understand how to retort
to your input.
Below are the default values used by ArguMints
should none be passed in. Also, the myArguMints
object
var options = treatBoolStringsAsBoolean:true // argv of "true/false" are converted to boolean treatNullStringsAsNull:true // argv of "null" converted to null treatRegExStringsAsRegEx:true // argv of "/*.*/g" converted to RegEx("*.*","g"); treatNumberStringsAsNumbers:true // argv of "1" converted to Number(1) treatUndefinedStringsAsUndefined:true // argv of "undefined" converted to undefined enableArgFileLoading:true // argv of @filename.txt will load contents of filename.txt (if true) otherwise, no change is made to value. ignoreJson:false // ignore json formatting and return the literal string value. ;
Requirements
Node
Installation
npm install argumints
API
Methods
.retort(arrayOfArgs=null, onStartCb=null, onArgExpandCb=null)
Immediately analyzes the input arguments to the current running node process, as well as the additional arrayOfArgs
passed in.
params
arrayOfArgs1
- an Array of 'string' values that will be expanded, or null
onStartCb
- a function
with signature function(userAgs)
or null
. If specified, the function will be called upon starting the retort, after gathering the input arguments.
onArgExpandCb
- a function
with signature function(argVal, expandedVal, idx, cnt)
or null
. If specified, the function will be called upon the processing of each individual argument in arrayOfArgs
allowing you to act after each one. The arguments for the function are the original argument, the expanded value, the current argument idx
, and,the count of arguments.
You Can call retort()
multiple times, with an array of arguments, or with no arguments. Arguments will be treated the same as if they were passed in via command line.
myArgs; { console; // 3 } { console; }
getUserArgs()
Returns all arguments passed in originally by the user (prior to expansion)
// node argumints-test.js arg1 arg2 --opt1 --opt2 -fxzy key1=val1 key2=val2 console; //output is [arg1, arg2, --opt1, --opt2, -fxzy, key1=val1, key2=val2]
getScriptArgs()
Returns the script arguments which are either of length 1 or 2, depending on where you are running code from (node command line or windows command line via node executable).
// node argumints-test.js arg1 arg2 --opt1 --opt2 key1=val1 key2=val2 console; //output is [C:\path\to\node.exe, C:\path\to\script.js] // OR [C:\path\to\node.exe] when running in Node Command Line.
.argc()
Returns the number of positional user arguments.
.argv(atIndex=null)
Returns the ordered argument passed into the Node Script at the specified index, or all items if the index is not specified.
// node argumints-test.js helpMe! thank you -fx --opt1 console; // output: helpMe! // get them all console; // output: [helpMe!,thank,you]
.opt(optName)
Returns true if the option at optName
was set
If no optName
is specified, a copy of the internal _opt table is returned for inspection.
// argumints-test --verbose console; // output: true // get them all console; // output: {verbose:true}
In addition to using opt
as a check, you can specify some 'built in' options
to enhance the minty flavour of ArguMints
.
Built in options
The Following Options will change the internal behavior of ArguMints
--minty-match-argv
--minty-match-kv
--minty-dump
--minty-append-dup-keys
--minty-op-add
--minty-op-sub
--minty-op-div
--minty-op-mul
--minty-op-sqrt
--minty-op-sqr
--minty-op-ln
--minty-no-cache
--minty-verbose
--minty-clear-opts
--minty-clear-flags
--minty-match-argv
setting this option will match all ordered arguments which aren't RegularExpressions against all ordered arguments which are regular expressions, and aggregate the matches. This is useful when spanning multiple files for patterns (i.e. email addresses, etc).
// the first two positional arguments are files containing text blobs. // the third positional argument is a text file containing a regular expression which // will expand to a RegExp instance, and match against the content of the files. myArgs; var matches = myArgs;
--minty-match-kv
much like minty-match-argv, except this iterates through keyStore values rather than positional arguments. you must only set one of these two keys, they are mutually exclusive.
myArgs; var matches = myArgs;
--minty-dump
Once retort completes, a minty-dump (of the commandTableData) will be displayed on console. smells great...
--minty-append-dup-keys
If the same key
is set as a keyValue
across one (or multiple) calls to retort()
, the results are aggregated.
This only works for Array (pushes or concatenates), Number (adds unless otherwise specified) and String types (concatenates)
// node .\argumints-test.js --minty-append-dup-keys "key1=some text" "key1=, more text" "key1=, even more" --minty-dump myArgs; console; // output: 221B Baker Street // -- OR -- myArgs; console; // output: true
Mutually Exclusive Operation Modifiers
--minty-op-add
Use this with --minty-append-dup-keys
, this will decide the current operation to be performed. this is enabled by default unless another type is specified.
--minty-op-div
Uses a divide operation when --minty-append-dup-keys
is set.
--minty-op-sub
Uses a subtract operation when --minty-append-dup-keys
is set.
--minty-op-mul
Uses a multiply operation when --minty-append-dup-keys
is set.
--minty-op-sqrt
Uses a square root operation when --minty-append-dup-keys
is set.
--minty-op-sqr
Uses a square operation when --minty-append-dup-keys
is set.
--minty-op-ln
Uses a natural log operation when --minty-append-dup-keys
is set.
--minty-op-tan
Uses a tangent (Math.tan(argAt))
operation--minty-append-dup-keys
is set.
--minty-op-atan
Uses a tangent (Math.atan(argAt))
operation--minty-append-dup-keys
is set.
--minty-op-sin
Uses a tangent (Math.sin(argAt))
operation--minty-append-dup-keys
is set.
--minty-op-cos
Uses a tangent (Math.cos(argAt))
operation--minty-append-dup-keys
is set.
--minty-op-exp
Agreggates (Math.exp(argAt))
or e^x
operations on a key if set one or more times and --minty-append-dup-keys
is set.
--minty-no-cache
File contents are not cached. Normally expanding a file will cache the file content, so further expansions in arguments using the same file will be faster. However, if your script modifies the file between retorts, or even while retorting, you can set this option to force it to reload upon each expansion request
myArgs; //result: test1.txt is reloaded 3 times
--minty-clear-opts and --minty-clear-flags
This allows you to change how minty works during a retort. For example, within the expansion callback (3rd parameter) if this is set, your flags/options are wiped clean and the next iteration will result in different behavior. Further more, you can push or insert arguments into minty as it is retorting as long as they are inserted after the current processing point.
myArgs;
.flag(flagName=undefined)
returns true if a flag by name is enabled. Flag names are a single character. If no flagName is passed, a copy of the internal _opt table is returned for inspection.
// argumints-test --verbose console; // true
.keyValue(key=undefined)
Returns the value stored under key
in the keyStore. The Key Store is populated using the format key=value
when passing in arguments. you can also specify files, i.e. key=@test/test.txt
If no key
is specified, a copy of the entire keyStore is returned for your inspection.
// argumints-test x=2 console; // 2
.matches(start=-1,end=-1)
Returns any matches after a retort with a regular expression and having --minty-match-argv or --minty-match-kv enagbled.
if start
or end
are specified, it will return a 'slice' of the matches contained within.
// argumints-test --minty-verbose --minty-match-argv @test/testMatcher2.txt @test/regExpOrd.txt console; // true
.insertArg(arg, xArgsFromHere);
This allows you to insert arguments directly in front of the current retort position. This means you can do fun things like factorials. NOTE: This is not meant to replace good old native code doing math, but its great for scripting and doing dynamic templated calculations and datamining.
// the factorial function, ArguMints style! var fact = 1; defaultMints; console; // output 6
.pushArg(moreArgs)
Pushes additional arguments onto the user argument queue. This can only be done during a retort, thus you must call this from the retort callback you pass into retort. be careful that you have a way to stop pushing arguments to avoid an infinitely growing args list.
// the summation function, ArguMints style! var sum = 0; var t = 15; // first x numbers defaultMints; console;
.reset(newOptions=undefined)
Resets the ArguMints instance. You must call retort()
again to re-parse the argument inputs, however, this gives you the option to append new arguments onto the instance.
// reset all data, keep current options myArgs; // reset all data, and overwrite new options myArgs;
License
The MIT License (MIT)
Copyright (c) 2016 Decoded4620
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.