first commit

This commit is contained in:
2026-09-08 20:28:54 +08:00
commit 2ada8d3d5a
37380 changed files with 4886169 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Tiaan <tiaanduplessis@hotmail.com> (tiaanduplessis.co.za)
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.
+95
View File
@@ -0,0 +1,95 @@
# get-them-args
[![package version](https://img.shields.io/npm/v/get-them-args.svg?style=flat-square)](https://npmjs.org/package/get-them-args)
[![package downloads](https://img.shields.io/npm/dm/get-them-args.svg?style=flat-square)](https://npmjs.org/package/get-them-args)
[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![package license](https://img.shields.io/npm/l/get-them-args.svg?style=flat-square)](https://npmjs.org/package/get-them-args)
[![make a pull request](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) [![Greenkeeper badge](https://badges.greenkeeper.io/tiaanduplessis/get-them-args.svg)](https://greenkeeper.io/)
> Parse argument options
## Table of Contents
- [About](#about)
- [Install](#install)
- [Usage](#usage)
- [Contribute](#contribute)
- [License](#License)
## About
Simple CLI argument parser hacked from [minimist](https://github.com/substack/minimist) that adds support for objects and additional initialization options.
## Install
```sh
$ npm install --save get-them-args
# Or
$ yarn add get-them-args
```
## Usage
To use, provide arguments as argument:
```js
const parse = require('get-them-args')
const options = {} // Options to be passed. CURRENTLY NONE AVAILABLE
// $ node ./example.js --dir . --command foo
console.log(parse(process.argv.slice(2) ))
// { unknown: [], dir: '.', command: 'foo' }
console.log(parse(process.argv))
// { unknown: [], dir: '.', command: 'foo' }
console.log(parse())
// { unknown: [], dir: '.', command: 'foo' }
```
For example, if the arguments provided are `--hello world --parse=all --no-drugs --make-friends -n 4 -t 5`, the function will return:
```js
{ unknown: [],
hello: 'world',
parse: 'all',
drugs: false,
'make-friends': true,
n: 4,
t: 5
}
```
There is also support for parsing objects:
```sh
$ node example.js --headers={"Foo": "5", "bar": "6"}
# { unknown: [], headers: { Foo: 5, bar: 6 } }
```
All unparsed arguments will end up in the `unknown` array. The following types of arguments are supported:
```sh
--key=value
--key value
--key # true
--no-key # false
-key=value
-key value
```
## Contribute
1. Fork it and create your feature branch: git checkout -b my-new-feature
2. Commit your changes: git commit -am 'Add some feature'
3. Push to the branch: git push origin my-new-feature
4. Submit a pull request
## License
MIT
+97
View File
@@ -0,0 +1,97 @@
'use strict'
/**
* Properly parse the given array in regards to object strings
* e.g. [ '--headers={Foo:', '5,', 'bar:', '6}' ] -> [ '--headers={"Foo": 5, "bar": 6}' ]
* @param {Array} args
*/
function splitArgObjects (args) {
const newArgs = []
let index = 0
while (index < args.length) {
const arg = args[index]
if (arg.indexOf('{') !== -1) {
const temp = []
while (args[index].indexOf('}') === -1) {
temp.push(args[index])
index++
}
temp.push(args[index])
newArgs.push(temp.join(' ').replace(/([\w\d-]+):\s*([\w\d-]*)/g, '"$1": "$2"'))
} else {
newArgs.push(arg)
}
index++
}
return newArgs
}
const parse = function parse (args = [], options = {}) {
if (!args.length) {
args = process.argv.slice(2)
}
if (args[0] && args[0].match(/node$/)) {
args = args.slice(2)
}
const newArgs = splitArgObjects(args)
function parseArgs (args, obj) {
// Check if end reached
if (!args.length) {
return obj
}
const arg = args[0]
// if statement match conditions:
// 1. --key=value || -key=value
// 2. --no-key
// 3. --key value|nothing
// else add to unknown arr
if (/^(--|-).+=/.test(arg)) {
const match = arg.match(/^(--|-)([^=]+)=([\s\S]*)$/)
// Set key(match[2]) = value(match[3])
obj[match[2]] = match[3]
} else if (/^--no-.+/.test(arg)) {
// Set key = true
obj[arg.match(/^--no-(.+)/)[1]] = false
} else if (/^(--|-).+/.test(arg)) {
const key = arg.match(/^(--|-)(.+)/)[2]
const next = args[1]
// If next value exist and not prefixed with - or --
if (next && !/^(-|--)/.test(next)) {
obj[key] = next
return parseArgs(args.slice(2), obj)
} else {
obj[key] = true
}
} else {
obj.unknown.push(arg)
}
return parseArgs(args.slice(1), obj)
}
const parseResult = parseArgs(newArgs, { unknown: [] })
// Covert to proper type
for (let prop in parseResult) {
try {
parseResult[prop] = JSON.parse(parseResult[prop])
} catch (e) {
continue
}
}
return parseResult
}
module.exports = parse
+36
View File
@@ -0,0 +1,36 @@
{
"name": "get-them-args",
"version": "1.3.2",
"description": "Parse argument options",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/tiaanduplessis/get-them-args.git"
},
"homepage": "https://github.com/tiaanduplessis/get-them-args",
"bugs": "https://github.com/tiaanduplessis/get-them-args/issues",
"author": "Tiaan du Plessis",
"scripts": {
"test": "jest",
"lint": "standard --fix",
"coverage": "jest --coverage"
},
"files": [
"index.js"
],
"keywords": [
"args",
"get-them-args",
"parser",
"arguments"
],
"devDependencies": {
"jest": "^23.4.1",
"standard": "^11.0.1"
},
"standard": {
"env": {
"jest": true
}
}
}