UNPKG

cli-reap

Version:

CLI and ENV parser; indifferent to argument order or runtime

80 lines 4.02 kB
import { ENV, ARGV } from 'globables'; export type NonEmptyString = string & { length: number; }; export type CliReap = Readonly<{ /** finds any value, in order: argv > environment > globalThis > default; removes from 'cur' argv if present */ any: <R = string>(keys: string | string[], defaultValue?: R) => R extends undefined ? string | true | null : string | true | R; /** command portion of argv (executable and script name) */ cmd: () => string[]; /** current un-consumed argv */ cur: () => string[]; /** if end-of-options/double-dash (--) delimiter is present in argv */ end: () => boolean; /** value from environment variables or globalThis; does not mutate */ env: (keys: string | string[]) => string | null; /** checks for flag presence and removes it from 'cur' argv */ flag: (keys: string | string[]) => true | null; /** retrieves operand value and removes it from 'cur' argv */ opt: <R extends NonEmptyString>(key: string | string[]) => R | null; /** remaining positional arguments (typically called last) */ pos: () => string[]; }>; /** * checks for presence of key (flags) in argv * @example --example -flag * @param {string | string[]} keys - argv key/id (--key, -key) * @param {string[]} [argv=ARGV] - command-line argv * @return {boolean} */ export declare const hasArgv: (keys: string | string[], argv?: string[]) => boolean; /** * normalizes and removes matching quotes from string values * @param {string} val - string value to normalize * @return {string} */ export declare const quoteNorm: (val: string) => string; /** * checks if value is a flag (starts with dashes and is not a number) * @param {string} val - value to check * @return {boolean} */ export declare const isFlag: (val?: string) => boolean; /** * creates CLI argument parser that consumes flags, options, and positionals * @param {string[]} argv=ARGV - command-line arguments array * @param {NodeJS.ProcessEnv} env=ENV - process environment variables * @param {typeof globalThis} gthis=GLOBAL_THIS - global object for runtime-set/fallback values * @param {boolean} strict=false - enable strict matching no (case/hyphen/underscore insensitive) * @return {CliReap} */ export declare const cliReap: (argv?: string[], env?: NodeJS.ProcessEnv, gthis?: typeof globalThis, strict?: boolean) => CliReap; /** * creates CLI argument parser with strict matching enabled, case-sensitive and * must match hyphen/underscore * @param {string[]} argv=ARGV - command-line arguments array * @param {NodeJS.ProcessEnv} procEnv=ENV - process environment variables * @param {typeof globalThis} gthis=GLOBAL_THIS - global object for runtime-set/fallback values * @return {CliReap} */ export declare const cliReapStrict: (argv?: string[], procEnv?: NodeJS.ProcessEnv, gthis?: typeof globalThis) => Readonly<{ /** finds any value, in order: argv > environment > globalThis > default; removes from 'cur' argv if present */ any: <R = string>(keys: string | string[], defaultValue?: R) => R extends undefined ? string | true | null : string | true | R; /** command portion of argv (executable and script name) */ cmd: () => string[]; /** current un-consumed argv */ cur: () => string[]; /** if end-of-options/double-dash (--) delimiter is present in argv */ end: () => boolean; /** value from environment variables or globalThis; does not mutate */ env: (keys: string | string[]) => string | null; /** checks for flag presence and removes it from 'cur' argv */ flag: (keys: string | string[]) => true | null; /** retrieves operand value and removes it from 'cur' argv */ opt: <R extends NonEmptyString>(key: string | string[]) => R | null; /** remaining positional arguments (typically called last) */ pos: () => string[]; }>; export default cliReap; export { ENV, ARGV, }; //# sourceMappingURL=index.d.ts.map