-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add arg group factories
c.bigint
and c.bigintArray
Closes #203
- Loading branch information
Showing
30 changed files
with
309 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
16 | ||
18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** src/multiply.ts */ | ||
import { c } from '@carnesen/cli'; | ||
|
||
/** A command for multiplying large integers as JavaScript {@link BigInt}s */ | ||
export const multiplyIntegersCommand = c.command({ | ||
name: 'multiply-integers', | ||
description: 'Multiply integers and print the result', | ||
positionalArgGroup: c.bigintArray(), | ||
action({ positionalValue: bigints }) { | ||
return bigints.reduce((a, b) => a * b, 1n); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/cli/src/arg-groups/__tests__/c-cli-bigint-arg-group.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { runAndCatch } from '@carnesen/run-and-catch'; | ||
import { CCliUsageError } from '../../c-cli-usage-error'; | ||
import { CCliBigintArgGroup } from '../c-cli-bigint-arg-group'; | ||
|
||
const description = 'foo bar baz'; | ||
const hidden = true; | ||
const placeholder = '<special>'; | ||
const optional = true; | ||
|
||
const argGroup = CCliBigintArgGroup.create({ | ||
optional, | ||
description, | ||
hidden, | ||
placeholder, | ||
}); | ||
|
||
describe(CCliBigintArgGroup.name, () => { | ||
it('returns `undefined` if args is `undefined` and no defaultValue has been provided', () => { | ||
expect(argGroup.parse(undefined)).toBe(undefined); | ||
}); | ||
|
||
it('parse returns the zeroth element of args', () => { | ||
expect(argGroup.parse(['1'])).toBe(1n); | ||
}); | ||
|
||
it('throws UsageError "expected just one" if args has more than one element', async () => { | ||
const exception = await runAndCatch(argGroup.parse, ['0', '1']); | ||
expect(exception).toBeInstanceOf(CCliUsageError); | ||
expect(exception.message).toMatch(/expected a single/i); | ||
expect(exception.message).toMatch(placeholder); | ||
}); | ||
|
||
it('throws UsageError "expected a" if args is an empty array', async () => { | ||
const exception = await runAndCatch(argGroup.parse, []); | ||
expect(exception).toBeInstanceOf(CCliUsageError); | ||
expect(exception.message).toMatch(/expected a/i); | ||
expect(exception.message).toMatch(placeholder); | ||
}); | ||
|
||
it('attaches config properties', () => { | ||
expect(argGroup.description).toBe(description); | ||
expect(argGroup.hidden).toBe(hidden); | ||
expect(argGroup.placeholder).toBe(placeholder); | ||
expect(argGroup.optional).toBe(optional); | ||
}); | ||
|
||
it('config is optional', () => { | ||
CCliBigintArgGroup.create(); | ||
}); | ||
|
||
it('throws UsageError "expected a" if args is an empty array', async () => { | ||
const exception = await runAndCatch(argGroup.parse, ['1n']); | ||
expect(exception).toBeInstanceOf(CCliUsageError); | ||
expect(exception.message).toBe('"1n" is not an integer'); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
packages/cli/src/arg-groups/__tests__/c-cli-bigint-array-arg-group.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { runAndCatch } from '@carnesen/run-and-catch'; | ||
import { CCliUsageError } from '../../c-cli-usage-error'; | ||
import { CCliBigintArrayArgGroup } from '../c-cli-bigint-array-arg-group'; | ||
|
||
const description = 'foo bar baz'; | ||
const hidden = true; | ||
const placeholder = '<special>'; | ||
const optional = true; | ||
|
||
const argGroup = CCliBigintArrayArgGroup.create({ | ||
description, | ||
hidden, | ||
placeholder, | ||
optional, | ||
}); | ||
|
||
describe(CCliBigintArrayArgGroup.name, () => { | ||
it('parse returns is args converted to numbers', () => { | ||
const parsed = argGroup.parse(['0', '1', '2']); | ||
if (!parsed) { | ||
throw new Error('Expected parsed to return a value'); | ||
} | ||
expect(parsed[0] === 0n).toBe(true); | ||
expect(parsed[1] === 1n).toBe(true); | ||
expect(parsed[2] === 2n).toBe(true); | ||
}); | ||
|
||
it('parse returns `undefined` if args is', () => { | ||
expect(argGroup.parse(undefined)).toBe(undefined); | ||
}); | ||
|
||
it('parse throws USAGE error "expected one or more" if args is an empty array', async () => { | ||
const exception = await runAndCatch(argGroup.parse, []); | ||
expect(exception).toBeInstanceOf(CCliUsageError); | ||
expect(exception.message).toMatch(/expected one or more/i); | ||
expect(exception.message).toMatch(placeholder); | ||
}); | ||
|
||
it('attaches config properties', () => { | ||
expect(argGroup.description).toBe(description); | ||
expect(argGroup.hidden).toBe(hidden); | ||
expect(argGroup.placeholder).toBe(placeholder); | ||
expect(argGroup.optional).toBe(optional); | ||
}); | ||
|
||
it('config is optional', () => { | ||
CCliBigintArrayArgGroup.create(); | ||
}); | ||
|
||
it('has a _suggest method always returning []', () => { | ||
expect(argGroup._suggest([])).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
CCliArgGroup, | ||
CCliArgGroupOptions, | ||
CCliParseArgs, | ||
} from '../c-cli-arg-group'; | ||
import { CCliConditionalValue } from '../c-cli-conditional-value'; | ||
import { convertToBigint } from '../convert-to-bigint'; | ||
|
||
/** Options for {@link CCliBigintArgGroup} */ | ||
export type CCliBigintArgGroupOptions<Optional extends boolean> = | ||
CCliArgGroupOptions<Optional>; | ||
|
||
export type CCliBigintArgGroupValue<Optional extends boolean> = | ||
CCliConditionalValue<bigint, Optional>; | ||
|
||
/** `number`-valued command-line argument group */ | ||
export class CCliBigintArgGroup<Optional extends boolean> extends CCliArgGroup< | ||
CCliBigintArgGroupValue<Optional>, | ||
Optional | ||
> { | ||
public parse( | ||
args: CCliParseArgs<Optional>, | ||
): CCliBigintArgGroupValue<Optional> { | ||
if (!args) { | ||
return this.undefinedAsValue(); | ||
} | ||
|
||
this.assertSingleArg(args); | ||
|
||
return convertToBigint(args[0]); | ||
} | ||
|
||
/** {@link CCliBigintArgGroup} factory function */ | ||
public static create<Optional extends boolean>( | ||
options: CCliBigintArgGroupOptions<Optional> = {}, | ||
): CCliBigintArgGroup<Optional> { | ||
return new CCliBigintArgGroup<Optional>({ | ||
placeholder: '<integer>', | ||
...options, | ||
}); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/cli/src/arg-groups/c-cli-bigint-array-arg-group.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
CCliArgGroup, | ||
CCliArgGroupOptions, | ||
CCliParseArgs, | ||
} from '../c-cli-arg-group'; | ||
import { CCliConditionalValue } from '../c-cli-conditional-value'; | ||
import { convertToBigint } from '../convert-to-bigint'; | ||
|
||
/** Options for {@link CCliBigintArrayArgGroup} a.k.a `ccli.bigintArray` */ | ||
export type CCliBigintArrayArgGroupOptions<Optional extends boolean = boolean> = | ||
CCliArgGroupOptions<Optional>; | ||
|
||
export type CCliNumberArrayArgGroupValue<Optional extends boolean> = | ||
CCliConditionalValue<bigint[], Optional>; | ||
|
||
/** `number[]`-valued argument group */ | ||
export class CCliBigintArrayArgGroup< | ||
Optional extends boolean, | ||
> extends CCliArgGroup<CCliNumberArrayArgGroupValue<Optional>, Optional> { | ||
public parse( | ||
args: CCliParseArgs<Optional>, | ||
): CCliNumberArrayArgGroupValue<Optional> { | ||
if (!args) { | ||
return this.undefinedAsValue(); | ||
} | ||
|
||
this.assertOneOrMoreArgs(args); | ||
|
||
return args.map(convertToBigint); | ||
} | ||
|
||
/** {@link CCliBigintArrayArgGroup} factory function */ | ||
public static create<Optional extends boolean>( | ||
options: CCliBigintArrayArgGroupOptions<Optional> = {}, | ||
): CCliBigintArrayArgGroup<Optional> { | ||
return new CCliBigintArrayArgGroup<Optional>({ | ||
placeholder: '<integer0> [...]', | ||
...options, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.