UNPKG

args-tokens

Version:

parseArgs tokens compatibility and more high-performance parser

43 lines (41 loc) 1.37 kB
import { parseArgs } from "./parser-M-ayhS1h.js"; import "./utils-1LQrGCWG.js"; import { ArgResolveError, resolveArgs } from "./resolver-D0hj6HpX.js"; //#region src/parse.ts const DEFAULT_OPTIONS = { help: { type: "boolean", short: "h" }, version: { type: "boolean", short: "v" } }; /** * Parse command line arguments. * * This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal. * * @typeParam A - {@link Args | Arguments schema}, which is an object that defines the command line arguments. * * @param args - command line arguments * @param options - parse options, about details see {@link ParseOptions} * @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}. * * @example * ```js * import { parse } from 'args-tokens' * * const { values, positionals } = parse(process.argv.slice(2)) * console.log('values', values) * console.log('positionals', positionals) * ``` */ function parse(args, options = {}) { const { args: _args, allowCompatible = false } = options; const tokens = parseArgs(args, { allowCompatible }); return Object.assign(Object.create(null), resolveArgs(_args || DEFAULT_OPTIONS, tokens), { tokens }); } //#endregion export { ArgResolveError, parse, parseArgs, resolveArgs };