clef-parse
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -0,6 +1,8 @@ | ||
export declare const Path: unique symbol; | ||
export declare type Hint = typeof String | typeof Boolean | typeof Number | typeof Path; | ||
export declare function parseArgv(argv?: Array<string>, hints?: { | ||
[key: string]: StringConstructor | BooleanConstructor | NumberConstructor; | ||
[key: string]: Hint | undefined | null; | ||
}): { | ||
options: any; | ||
positionalArgs: any[]; | ||
positionalArgs: Array<string>; | ||
}; |
@@ -21,5 +21,24 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseArgv = void 0; | ||
exports.parseArgv = exports.Path = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
const changeCase = __importStar(require("change-case")); | ||
exports.Path = Symbol("Path"); | ||
function bestGuess(nextValue) { | ||
if (nextValue === "true" || nextValue === "false") { | ||
return Boolean; | ||
} | ||
else if (nextValue == null || nextValue.startsWith("-")) { | ||
return Boolean; | ||
} | ||
else if (nextValue === String(Number(nextValue))) { | ||
return Number; | ||
} | ||
else { | ||
return String; | ||
} | ||
} | ||
function parseArgv(argv = process.argv.slice(2), hints = {}) { | ||
@@ -53,30 +72,42 @@ const options = {}; | ||
let propertyValue; | ||
const propertyHint = hints[propertyName]; | ||
let propertyHint = hints[propertyName]; | ||
const nextValue = argv[0]; | ||
if (nextValue === "true" || nextValue === "false") { | ||
argv.shift(); | ||
propertyValue = nextValue === "true"; | ||
if (propertyHint == null) { | ||
propertyHint = bestGuess(nextValue); | ||
} | ||
else if (propertyHint === Boolean || | ||
nextValue == null || | ||
nextValue.startsWith("-")) { | ||
propertyValue = true; | ||
switch (propertyHint) { | ||
case Boolean: { | ||
if (nextValue === "false") { | ||
argv.shift(); | ||
propertyValue = false; | ||
} | ||
else { | ||
if (nextValue === "true") { | ||
argv.shift(); | ||
} | ||
propertyValue = true; | ||
} | ||
break; | ||
} | ||
case Number: { | ||
argv.shift(); | ||
propertyValue = Number(nextValue); | ||
break; | ||
} | ||
case String: { | ||
argv.shift(); | ||
propertyValue = nextValue; | ||
break; | ||
} | ||
case exports.Path: { | ||
argv.shift(); | ||
propertyValue = path_1.default.isAbsolute(nextValue) | ||
? nextValue | ||
: path_1.default.resolve(process.cwd(), nextValue); | ||
break; | ||
} | ||
default: { | ||
throw new Error(`Invalid option hint: ${propertyHint}`); | ||
} | ||
} | ||
else if (nextValue === "null") { | ||
argv.shift(); | ||
propertyValue = null; | ||
} | ||
else if (nextValue === "undefined") { | ||
argv.shift(); | ||
propertyValue = undefined; | ||
} | ||
else if (propertyHint === Number || | ||
(propertyHint == null && nextValue === String(Number(nextValue)))) { | ||
argv.shift(); | ||
propertyValue = Number(nextValue); | ||
} | ||
else { | ||
argv.shift(); | ||
propertyValue = nextValue; | ||
} | ||
options[propertyName] = propertyValue; | ||
@@ -83,0 +114,0 @@ } |
{ | ||
"name": "clef-parse", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Simple, lightweight argv parser. Powers the cleffa and clefairy packages", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
7543
153