Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding yargs-parser functionality #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": ["plugin:eslint-plugin-node-core/recommended"],
"env": {
"node": true,
"es6": true
},
"rules": {
"linebreak-style": 0
},
"ignorePatterns": ["README.md"]
}
13 changes: 0 additions & 13 deletions .eslintrc.json

This file was deleted.

17 changes: 0 additions & 17 deletions .prettierrc.js

This file was deleted.

121 changes: 121 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { parseArgs } from '@pkgjs/parseargs';

// Take an un-split argv string and tokenize it.
export function tokenizeArgString(argString) {
if (Array.isArray(argString)) {
return argString.map((e) => (typeof e !== 'string' ? e + '' : e));
}

argString = argString.trim();

let i = 0;
let prevC = null;
let c = null;
let opening = null;
const args = [];

for (let ii = 0; ii < argString.length; ii++) {
prevC = c;
c = argString.charAt(ii);

// Split on spaces unless we're in quotes.
if (c === ' ' && !opening) {
if (!(prevC === ' ')) {
i++;
}
continue;
}

// Don't split the string if we're in matching
// opening or closing single and double quotes.
if (c === opening) {
opening = null;
} else if ((c === "'" || c === '"') && !opening) {
opening = c;
}

if (!args[i]) args[i] = '';
args[i] += c;
}

return args;
}

function YargsParser(
args,
opts
) {
if (typeof args === 'string') {
args = tokenizeArgString(args);
}
const result = YargsParser.detailed(args.slice(), opts);
return result.argv;
}
YargsParser.detailed = function(
args,
opts
) {
const configuration = {
'boolean-negation': false,
'camel-case-expansion': false,
'combine-arrays': false,
'dot-notation': false,
'duplicate-arguments-array': false,
'flatten-duplicate-arrays': false,
'greedy-arrays': false,
'halt-at-non-option': false,
'nargs-eats-options': false,
'negation-prefix': 'no',
'parse-positional-numbers': false,
'parse-numbers': true,
'populate--': false,
'set-placeholder-key': false,
'short-option-groups': false,
'strip-aliased': false,
'strip-dashed': false,
'unknown-options-as-args': false,
...opts?.configuration
};
if (typeof args === 'string') {
args = tokenizeArgString(args);
}
const parsed = parseArgs(args, opts);
const argv = {
_: parsed.positionals,
$0: '',
...parsed.flags
};
for (const [key, value] of Object.entries(parsed.values)) {
if (value !== undefined) {
let mungedValue = value;
const shouldCoerceNumber = looksLikeNumber(value) &&
configuration['parse-numbers'] && (
Number.isSafeInteger(Math.floor(parseFloat(`${value}`))));
if (shouldCoerceNumber) {
mungedValue = Number(value);
}
argv[key] = mungedValue;
}
}
return {
argv,
error: null,
aliases: {},
newAliases: {},
configuration
};
};
YargsParser.camelCase = (str) => str;
YargsParser.decamelize = (str, joinString) => str;
function looksLikeNumber(x) {
if (x === null || x === undefined) return false;
// If loaded from config, may already be a number.
if (typeof x === 'number') return true;
// hexadecimal.
if (/^0x[0-9a-f]+$/i.test(x)) return true;
// Don't treat 0123 as a number; as it drops the leading '0'.
if (/^0[^.]/.test(x)) return false;
return /^[-]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
}
YargsParser.looksLikeNumber = looksLikeNumber;
export default YargsParser;
74 changes: 0 additions & 74 deletions index.ts

This file was deleted.

Loading