mirror of
https://github.com/Wessel/larg.git
synced 2026-06-06 07:25:44 +02:00
Upload main files
Added: package.json README.md test.js index.js Changed: LICENSE
This commit is contained in:
5
LICENSE
5
LICENSE
@@ -1,6 +1,7 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Wessel T
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2019-present Wessel "wesselgame" T
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
30
README.md
Normal file
30
README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# larg
|
||||
> A lightweight Node.js argument parser which can be used in various ways
|
||||
|
||||
> [GitHub](https://www.github.com/PassTheWessel/larg) **|** [NPM](https://www.npmjs.com/package/larg)
|
||||
|
||||
## Installing
|
||||
```sh
|
||||
$ yarn add larg # Install w/ Yarn (the superior package manager)
|
||||
$ npm i larg # Install w/ NPM
|
||||
```
|
||||
|
||||
## Usage
|
||||
##### Code
|
||||
```js
|
||||
const argv = require( 'larg' )( process.argv.slice( 2 ) );
|
||||
console.log( argv );
|
||||
```
|
||||
##### Result
|
||||
```sh
|
||||
$ node test.js -x 3 -y 4 -ab --beep=boop foo bar
|
||||
{ _: [ 'foo', 'bar' ],
|
||||
x: 3,
|
||||
y: 4,
|
||||
a: true,
|
||||
b: true,
|
||||
beep: 'boop' }
|
||||
```
|
||||
|
||||
### Why should i use larg?
|
||||
Larg is very lightweight (3.45kb total) comparing to other packages such as optimist and minimist
|
||||
24
index.js
Normal file
24
index.js
Normal file
@@ -0,0 +1,24 @@
|
||||
module.exports = ( args ) => {
|
||||
let p = {};
|
||||
let l = [];
|
||||
|
||||
const rHyphens = ( v ) => v.replace(/^\-+/g, '');
|
||||
const cApplicable = ( v ) => ( isNaN( v ) ? ( v.toString().toLowerCase() === 'true' ? true : ( v.toString().toLowerCase() === 'false' ? false : v ) ) : Number( v ) );
|
||||
|
||||
for ( let i = 0; i < args.length; i++ ) {
|
||||
const e = args[ i ].indexOf( '=' );
|
||||
const r = args[ i ].charAt( 0 ) === '-' && args.length - 1 >= i + 1 && args[ i + 1 ].indexOf( '=' ) === -1 && args[ i + 1 ].charAt( 0 ) !== '-';
|
||||
const n = e === -1 ? rHyphens( args[ i ] ) : rHyphens( args[ i ].slice( 0, e ) );
|
||||
|
||||
if ( e !== -1 ) p[ n ] = cApplicable( args[ i ].slice( e + 1 ) );
|
||||
else if ( r ) {
|
||||
p[ n ] = cApplicable( args[ i + 1 ] );
|
||||
i++;
|
||||
} else if ( args[ i ].charAt( 0 ) === '-' ) {
|
||||
if ( args[ i ].charAt( 1 ) === '-' ) p[ n ] = true;
|
||||
else for ( let b = 0; b < n.length; b++ ) p[ n.charAt( b ) ] = true;
|
||||
} else l.push( cApplicable( n ) );
|
||||
}
|
||||
|
||||
return Object.assign( p, { '_': l } );
|
||||
};
|
||||
16
package.json
Normal file
16
package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "larg",
|
||||
"version": "0.0.1",
|
||||
"description": "👾 A very lightweight Node.js argument parser",
|
||||
"author": "Wessel \"wesselgame\" T <discord@go2it.eu>",
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"files": [ "index.js", "LICENSE" ],
|
||||
"bugs": { "url": "https://www.github.com/PassTheWessel/larg/issues" },
|
||||
"homepage": "https://www.github.com/PassTheWessel/larg#readme",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://www.github.com/PassTheWessel/larg"
|
||||
},
|
||||
"keywords": [ "argument", "args", "arg", "argv", "parse", "cli", "command-line", "parser", "command", "lightweight" ]
|
||||
}
|
||||
Reference in New Issue
Block a user