Skip to content

Commit 1100d31

Browse files
committed
feat: parse string defaults
- also fixes storing of default command-argument values Signed-off-by: Lexus Drumgold <[email protected]>
1 parent ba1eac4 commit 1100d31

File tree

12 files changed

+297
-181
lines changed

12 files changed

+297
-181
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,14 +2874,13 @@ Data used to create parse candidates (TypeScript interface).
28742874
#### Properties
28752875
28762876
- `choices?` ([`List<string>`](#list), optional)
2877-
— list of option choices
2877+
the list of allowed choices
28782878
- `default?` ([`DefaultInfo`](#defaultinfo), optional)
2879-
— default value configuration
2880-
> 👉 **note**: the option-argument `parser` will not be called.
2879+
the default value configuration
2880+
> 👉 **note**: the argument `parser` is called **if the default value is a string**.
28812881
- `parser?` ([`ParseArg<any, string> | ParseArg<any, string[]>`](#parseargt), optional)
2882-
— handler used to parse arguments. the handler receives two parameters, the raw, unparsed argument (or
2883-
*arguments* for variadic candidates), and the default value for the argument.
2884-
it should return the new value for the argument
2882+
— the handler used to parse arguments. the handler receives two parameters, the raw, unparsed argument, and the
2883+
previous value for the argument. it should return the new value for the argument
28852884
28862885
### `ParseableMetadata`
28872886

__fixtures__/commands/clamp.mts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ export default {
3232
parser: bool()
3333
},
3434
{
35-
default: {
36-
description: 'Number.MAX_SAFE_INTEGER',
37-
value: Number.MAX_SAFE_INTEGER
38-
},
35+
default: { value: 'MAX_SAFE_INTEGER' },
3936
description: 'upper bound (inclusive)',
4037
flags: '-M --max <n>',
4138
parser: number

__fixtures__/commands/distinct.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { SubcommandInfo as CommandInfo } from '@flex-development/kronk'
1515
*/
1616
export default {
1717
arguments: {
18+
default: { value: new Set() },
1819
description: 'the list of numbers',
1920
parser: unique,
2021
syntax: sfmt.optional({ id: 'numbers', variadic: true })

src/enums/keid.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const enum keid {
2626
missing_argument = 'kronk/missing-argument',
2727
missing_mandatory_option = 'kronk/missing-mandatory-option',
2828
no_flags = 'kronk/no-flags',
29+
required_argument_after_optional = 'kronk/required-argument-after-optional',
2930
unknown_implied_option = 'kronk/unknown-implied-option',
3031
unknown_option = 'kronk/unknown-option'
3132
}

src/interfaces/__tests__/kronk-error.map.spec-d.mts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import type TestSubject from '#interfaces/kronk-error.map'
77
import type { CommandError, KronkError } from '@flex-development/kronk'
88

99
describe('unit-d:interfaces/KronkErrorMap', () => {
10-
it('should match ["argument-after-variadic": CommandError]', () => {
10+
it('should match ["argument-after-variadic": KronkError]', () => {
1111
expectTypeOf<TestSubject>()
1212
.toHaveProperty('argument-after-variadic')
13-
.toEqualTypeOf<CommandError>()
13+
.toEqualTypeOf<KronkError>()
1414
})
1515

1616
it('should match ["conflicting-option": CommandError]', () => {
@@ -79,6 +79,12 @@ describe('unit-d:interfaces/KronkErrorMap', () => {
7979
.toEqualTypeOf<KronkError>()
8080
})
8181

82+
it('should match ["required-argument-after-optional": KronkError]', () => {
83+
expectTypeOf<TestSubject>()
84+
.toHaveProperty('required-argument-after-optional')
85+
.toEqualTypeOf<KronkError>()
86+
})
87+
8288
it('should match ["unknown-implied-option": KronkError]', () => {
8389
expectTypeOf<TestSubject>()
8490
.toHaveProperty('unknown-implied-option')

src/interfaces/kronk-error.map.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type { CommandError, KronkError } from '@flex-development/kronk'
2121
* }
2222
*/
2323
interface KronkErrorMap {
24-
'argument-after-variadic': CommandError
24+
'argument-after-variadic': KronkError
2525
'conflicting-option': CommandError
2626
'duplicate-option': CommandError
2727
'duplicate-subcommand': CommandError
@@ -33,6 +33,7 @@ interface KronkErrorMap {
3333
'missing-argument': CommandError
3434
'missing-mandatory-option': CommandError
3535
'no-flags': KronkError
36+
'required-argument-after-optional': KronkError
3637
'unknown-implied-option': KronkError
3738
'unknown-option': CommandError
3839
error: KronkError

src/interfaces/parseable.info.mts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ import type {
1919
*/
2020
interface ParseableInfo extends HelpableInfo {
2121
/**
22-
* List of allowed choices.
22+
* The list of allowed choices.
2323
*
2424
* @see {@linkcode List}
2525
*/
2626
choices?: List<string> | null | undefined
2727

2828
/**
29-
* Default value configuration.
29+
* The default value configuration.
3030
*
31-
* > 👉 **Note**: The argument {@linkcode parser} will not be called.
31+
* > 👉 **Note**: The argument {@linkcode parser} is called
32+
* > **if the default value is a string**.
3233
*
3334
* @see {@linkcode DefaultInfo}
3435
*/

src/lib/__snapshots__/command.functional.snap

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ exports[`functional:lib/Command > errors > should error on excess command or opt
193193
"command": {
194194
"command": "copy",
195195
"ancestors": [],
196-
"args": [],
196+
"args": [
197+
undefined,
198+
".",
199+
],
197200
"argv": [
198201
"./index.mjs",
199202
"./dist",
@@ -756,7 +759,10 @@ exports[`functional:lib/Command > errors > should error on missing command or op
756759
"command": {
757760
"command": "copy",
758761
"ancestors": [],
759-
"args": [],
762+
"args": [
763+
undefined,
764+
".",
765+
],
760766
"argv": [],
761767
"optionValueSources": {},
762768
"opts": {},
@@ -779,7 +785,10 @@ exports[`functional:lib/Command > errors > should error on missing command or op
779785
"command": {
780786
"command": "copy",
781787
"ancestors": [],
782-
"args": [],
788+
"args": [
789+
undefined,
790+
".",
791+
],
783792
"argv": [],
784793
"optionValueSources": {},
785794
"opts": {},
@@ -1174,7 +1183,10 @@ exports[`functional:lib/Command > errors > should error on unknown option (0) 1`
11741183
"command": {
11751184
"command": "copy",
11761185
"ancestors": [],
1177-
"args": [],
1186+
"args": [
1187+
undefined,
1188+
".",
1189+
],
11781190
"argv": [
11791191
"./index.mts",
11801192
"--debug",
@@ -1546,6 +1558,7 @@ exports[`functional:lib/Command > parsing > should run command (5) 1`] = `
15461558
"ancestors": [],
15471559
"args": [
15481560
"./dist/index.mjs",
1561+
".",
15491562
],
15501563
"argv": [
15511564
"./dist/index.mjs",
@@ -1590,6 +1603,7 @@ exports[`functional:lib/Command > parsing > should run command (7) 1`] = `
15901603
"ancestors": [],
15911604
"args": [
15921605
"./src/main.mjs",
1606+
".",
15931607
],
15941608
"argv": [
15951609
"./src/main.mjs",
@@ -1632,7 +1646,9 @@ exports[`functional:lib/Command > parsing > should run command (9) 1`] = `
16321646
{
16331647
"command": "distinct",
16341648
"ancestors": [],
1635-
"args": [],
1649+
"args": [
1650+
Set {},
1651+
],
16361652
"argv": [],
16371653
"optionValueSources": {},
16381654
"opts": {},
@@ -1649,7 +1665,9 @@ exports[`functional:lib/Command > parsing > should run command (10) 1`] = `
16491665
{
16501666
"command": "distinct",
16511667
"ancestors": [],
1652-
"args": [],
1668+
"args": [
1669+
Set {},
1670+
],
16531671
"argv": [],
16541672
"optionValueSources": {},
16551673
"opts": {},

src/lib/__snapshots__/command.snap

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`unit:lib/Command > #addArgument > should throw if argument is added aft
44
{
55
"code": 1,
66
"id": "kronk/argument-after-variadic",
7-
"message": "Cannot have argument after variadic argument (<numbers...>)",
7+
"message": "Cannot have argument after variadic argument",
88
"additional": [],
99
"cause": {
1010
"argument": "<...>",
@@ -13,6 +13,19 @@ exports[`unit:lib/Command > #addArgument > should throw if argument is added aft
1313
}
1414
`;
1515

16+
exports[`unit:lib/Command > #addArgument > should throw if required argument is added after optional 1`] = `
17+
{
18+
"code": 1,
19+
"id": "kronk/required-argument-after-optional",
20+
"message": "Cannot have required argument after optional argument",
21+
"additional": [],
22+
"cause": {
23+
"argument": "<>",
24+
"last": "[artifact]",
25+
},
26+
}
27+
`;
28+
1629
exports[`unit:lib/Command > #addCommand > should throw on duplicate subcommand alias 1`] = `
1730
{
1831
"code": 1,

0 commit comments

Comments
 (0)