Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: append context to default
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 3, 2018
1 parent 543d588 commit 50bf003
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ export type ParserInput<TFlags extends flags.Output> = {
flags?: flags.Input<TFlags>
args?: args.Input
strict?: boolean
context?: any
}

export function parse<TFlags, TArgs extends {[name: string]: string}>(argv: string[], options: ParserInput<TFlags>): ParserOutput<TFlags, TArgs> {
const input = {
argv,
context,
args: (options.args || []).map((a: any) => deps.args.newArg(a as any)),
flags: {
color: deps.flags.defaultFlags.color,
Expand Down
5 changes: 4 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ export interface ParserInput {
flags: Flags.Input<any>
args: Arg<any>[]
strict: boolean
context: any
}

export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']>, TArgs extends OutputArgs<T['args']>> {
private readonly argv: string[]
private readonly raw: ParsingToken[] = []
private readonly booleanFlags: { [k: string]: Flags.IBooleanFlag<any> }
private readonly context: any
constructor(private readonly input: T) {
this.context = input.context || {}
this.argv = input.argv.slice(0)
this._setNames()
this.booleanFlags = _.pickBy(input.flags, f => f.type === 'boolean') as any
Expand Down Expand Up @@ -179,7 +182,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
}
if (!flags[k] && flag.default) {
if (typeof flag.default === 'function') {
flags[k] = flag.default({options: flag, flags})
flags[k] = flag.default({options: flag, flags, ...this.context})
} else {
flags[k] = flag.default
}
Expand Down

0 comments on commit 50bf003

Please sign in to comment.