Skip to content

Commit

Permalink
refactor: argument meta => argument state
Browse files Browse the repository at this point in the history
  • Loading branch information
nullishamy committed Aug 30, 2023
1 parent e8ccce1 commit 1a62da2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export class Args<TArgTypes extends DefaultArgTypes = DefaultArgTypes> {
}
}

if (positionals.filter(p => p.inner._meta.isMultiType).length > 1) {
if (positionals.filter(p => p.inner._state.isMultiType).length > 1) {
return Err(new SchemaError('multiple multi-type positionals found'))
}
return Ok(this)
Expand Down
8 changes: 4 additions & 4 deletions src/builder/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type CoercionResult<T> = CoercionResultOk<T> | CoercionResultErr

export type ArgumentType = string

interface ArgumentMeta<T> {
interface ArgumentState<T> {
specifiedDefault: T | undefined
dependencies: string[]
requiredUnlessPresent: string[]
Expand All @@ -28,7 +28,7 @@ interface ArgumentMeta<T> {
otherParsers: Array<MinimalArgument<CoercedValue>>
}

export type MinimalArgument<T> = Pick<Argument<T>, '_meta' | 'coerce' | 'type' | 'negate'>
export type MinimalArgument<T> = Pick<Argument<T>, '_state' | 'coerce' | 'type' | 'negate'>

export abstract class Argument<T> {
protected _specifiedDefault: T | undefined = undefined
Expand All @@ -44,8 +44,8 @@ export abstract class Argument<T> {
protected _negated: boolean = false

// Internal getter to avoid cluttering completion with ^ our private fields that need to be accessed by other internal APIs
// Conveniently also means we encapsulate our data, so it cannot be easily tampered with by outside people (assuming they do not break type safety)
get _meta (): ArgumentMeta<T> {
// Conveniently also means we encapsulate our data, so it cannot be easily tampered with by consumers
get _state (): ArgumentState<T> {
return {
specifiedDefault: this._specifiedDefault,
dependencies: this._dependencies,
Expand Down
16 changes: 8 additions & 8 deletions src/internal/parse/coerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface CoercedSingleValue {
}

async function coerceSingleArgument (inputValue: string, argument: InternalArgument): Promise<Result<CoercedSingleValue, CoercionError[]>> {
const parsers = [argument.inner, ...argument.inner._meta.otherParsers]
const parsers = [argument.inner, ...argument.inner._state.otherParsers]
const results = await Promise.all(parsers.map(async parser => [parser, await parser.coerce(inputValue)] as const))

const errors: Array<{
Expand Down Expand Up @@ -121,7 +121,7 @@ async function resolveArgumentDefault (
value: {
isMulti: false,
raw: `<default value for ${getArgDenotion(argument)}>`,
coerced: argument.inner._meta.unspecifiedDefault
coerced: argument.inner._state.unspecifiedDefault
}
})
}
Expand All @@ -146,7 +146,7 @@ async function resolveArgumentDefault (
value: {
isMulti: false,
raw: `<default value for group member '${argument.longFlag}' of '${userArgument.rawInput}'`,
coerced: argument.inner._meta.specifiedDefault
coerced: argument.inner._state.specifiedDefault
}
})
}
Expand All @@ -166,7 +166,7 @@ async function resolveArgumentDefault (
value: {
isMulti: false,
raw: `<default value for ${getArgDenotion(argument)}`,
coerced: argument.inner._meta.specifiedDefault
coerced: argument.inner._state.specifiedDefault
}
})
}
Expand All @@ -181,7 +181,7 @@ async function resolveArgumentDefault (

function handleAdditionalArgumentDefinition (argument: InternalArgument, opts: StoredParserOpts): Result<'overwrite' | 'skip' | 'append', CoercionError> {
const { arrayMultipleDefinitions, tooManyDefinitions } = opts
if (argument.inner._meta.isMultiType) {
if (argument.inner._state.isMultiType) {
if (arrayMultipleDefinitions === 'append') {
return Ok('append')
} else if (arrayMultipleDefinitions === 'throw') {
Expand Down Expand Up @@ -314,7 +314,7 @@ export async function coerce (
}

// Collate all positionals together into this one, if the positional is a multi-type
if (!Array.isArray(userArgument) && argument.inner._meta.isMultiType) {
if (!Array.isArray(userArgument) && argument.inner._state.isMultiType) {
const positionalValues = [...positionals.values()].flatMap(p => p.values)
userArgument = {
type: 'positional',
Expand All @@ -334,7 +334,7 @@ export async function coerce (
}

// User passed more than one argument, and this is not a multi type
if (!argument.inner._meta.isMultiType && inputValues.length > 1) {
if (!argument.inner._state.isMultiType && inputValues.length > 1) {
// Throw if appropriate, slice off the other arguments if not (acts as a skip)
const { tooManyArgs } = opts
if (tooManyArgs === 'throw') {
Expand All @@ -350,7 +350,7 @@ export async function coerce (
}

let coercionResult
if (argument.inner._meta.isMultiType) {
if (argument.inner._state.isMultiType) {
coercionResult = await coerceMultiType(inputValues, argument)
} else {
if (inputValues.length !== 1) {
Expand Down
6 changes: 3 additions & 3 deletions src/internal/parse/schematic-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function validateFlagSchematically (
}
}

let { specifiedDefault, unspecifiedDefault, optional, dependencies, conflicts, exclusive, requiredUnlessPresent } = argument.inner._meta
let { specifiedDefault, unspecifiedDefault, optional, dependencies, conflicts, exclusive, requiredUnlessPresent } = argument.inner._state

// Test our resolvers to see if any of them have a value, so we know whether to reject below
let resolversHaveValue = false
Expand Down Expand Up @@ -102,7 +102,7 @@ export function validatePositionalSchematically (
middlewares: Resolver[]
): Result<ParsedPositionalArgument | undefined, CoercionError> {
const foundFlag = positionals.get(argument.index)
const { unspecifiedDefault, optional } = argument.inner._meta
const { unspecifiedDefault, optional } = argument.inner._state

// Test our middlewares to see if any of them have a value, so we know whether to reject below
let middlewaresHaveValue = false
Expand All @@ -124,7 +124,7 @@ export async function coerceMultiType (inputValues: string[], argument: Internal
const eachParserResults: Map<MinimalArgument<CoercedValue>, Array<CoercionResult<CoercedValue>>> = new Map()

for (const value of inputValues) {
for (const parser of [argument.inner, ...argument.inner._meta.otherParsers]) {
for (const parser of [argument.inner, ...argument.inner._state.otherParsers]) {
const parsed = await parser.coerce(value)
const alreadyParsed = eachParserResults.get(parser) ?? []

Expand Down
2 changes: 1 addition & 1 deletion src/util/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function generateHelp (parser: Args<{}>): string {
const { opts } = parser

const renderArgument = (value: InternalArgument): string => {
const { optional, isMultiType } = value.inner._meta
const { optional, isMultiType } = value.inner._state
if (optional) {
if (value.type === 'positional') {
return `[<${value.key.toUpperCase()}>]`
Expand Down

0 comments on commit 1a62da2

Please sign in to comment.