Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sad-areas-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/cli": patch
---

print primitive default values in cli help output
13 changes: 7 additions & 6 deletions packages/cli/src/internal/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,16 +535,17 @@ const getHelpInternal = (self: Instruction): HelpDoc.HelpDoc => {
return InternalHelpDoc.mapDescriptionList(
getHelpInternal(self.args as Instruction),
(span, block) => {
const defaultDescription = (value: unknown) => {
const inspectableValue = Predicate.isObject(value) ? value : String(value)
const displayValue = Inspectable.toStringUnknown(inspectableValue, 0)
return InternalHelpDoc.p(`This setting is optional. Defaults to: ${displayValue}`)
}
const optionalDescription = Option.isOption(self.fallback)
? Option.match(self.fallback, {
onNone: () => InternalHelpDoc.p("This setting is optional."),
onSome: (fallbackValue) => {
const inspectableValue = Predicate.isObject(fallbackValue) ? fallbackValue : String(fallbackValue)
const displayValue = Inspectable.toStringUnknown(inspectableValue, 0)
return InternalHelpDoc.p(`This setting is optional. Defaults to: ${displayValue}`)
}
onSome: defaultDescription
})
: InternalHelpDoc.p("This setting is optional.")
: defaultDescription(self.fallback)
return [span, InternalHelpDoc.sequence(block, optionalDescription)]
}
)
Expand Down
13 changes: 7 additions & 6 deletions packages/cli/src/internal/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,16 +789,17 @@ const getHelpInternal = (self: Instruction): HelpDoc.HelpDoc => {
return InternalHelpDoc.mapDescriptionList(
getHelpInternal(self.options as Instruction),
(span, block) => {
const defaultDescription = (value: unknown) => {
const inspectableValue = Predicate.isObject(value) ? value : String(value)
const displayValue = Inspectable.toStringUnknown(inspectableValue, 0)
return InternalHelpDoc.p(`This setting is optional. Defaults to: ${displayValue}`)
}
const optionalDescription = Option.isOption(self.fallback)
? Option.match(self.fallback, {
onNone: () => InternalHelpDoc.p("This setting is optional."),
onSome: (fallbackValue) => {
const inspectableValue = Predicate.isObject(fallbackValue) ? fallbackValue : String(fallbackValue)
const displayValue = Inspectable.toStringUnknown(inspectableValue, 0)
return InternalHelpDoc.p(`This setting is optional. Defaults to: ${displayValue}`)
}
onSome: defaultDescription
})
: InternalHelpDoc.p("This setting is optional.")
: defaultDescription(self.fallback)
return [span, InternalHelpDoc.sequence(block, optionalDescription)]
}
)
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/test/Args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,13 @@ describe("Args", () => {
const helpDoc = Args.getHelp(option)
yield* Effect.promise(() => expect(helpDoc).toMatchFileSnapshot("./snapshots/help-output/args-no-default"))
}).pipe(runEffect))

it("displays default value in help when default is a primitive value", () =>
Effect.gen(function*() {
const args = Args.withDefault(Args.integer({ name: "count" }), 42)
const helpDoc = Args.getHelp(args)
yield* Effect.promise(() =>
expect(helpDoc).toMatchFileSnapshot("./snapshots/help-output/args-default-primitive-value")
)
}).pipe(runEffect))
})
21 changes: 21 additions & 0 deletions packages/cli/test/Options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,27 @@ describe("Options", () => {
yield* Effect.promise(() => expect(helpDoc).toMatchFileSnapshot("./snapshots/help-output/options-no-default"))
}).pipe(runEffect))

it("displays default value in help when default is a primitive value", () =>
Effect.gen(function*() {
const option = Options.withDefault(Options.integer("count"), 42)
const helpDoc = Options.getHelp(option)
yield* Effect.promise(() =>
expect(helpDoc).toMatchFileSnapshot("./snapshots/help-output/options-default-primitive-value")
)
}).pipe(runEffect))

it("displays default value in help when default is a primitive boolean", () =>
Effect.gen(function*() {
const option = Options.withDefault(
Options.choiceWithValue("uppercase", [["true", true], ["false", false]]),
false
)
const helpDoc = Options.getHelp(option)
yield* Effect.promise(() =>
expect(helpDoc).toMatchFileSnapshot("./snapshots/help-output/options-default-primitive-boolean")
)
}).pipe(runEffect))

describe("options after positional arguments", () => {
it("parses a text option that appears after positional args", () =>
Effect.gen(function*() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"_tag": "DescriptionList",
"definitions": [
[
{
"_tag": "Weak",
"value": {
"_tag": "Text",
"value": "<count>",
},
},
{
"_tag": "Sequence",
"left": {
"_tag": "Paragraph",
"value": {
"_tag": "Text",
"value": "An integer.",
},
},
"right": {
"_tag": "Paragraph",
"value": {
"_tag": "Text",
"value": "This setting is optional. Defaults to: 42",
},
},
},
],
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"_tag": "DescriptionList",
"definitions": [
[
{
"_tag": "Sequence",
"left": {
"_tag": "Text",
"value": "--uppercase",
},
"right": {
"_tag": "Sequence",
"left": {
"_tag": "Text",
"value": " ",
},
"right": {
"_tag": "Text",
"value": "true | false",
},
},
},
{
"_tag": "Sequence",
"left": {
"_tag": "Paragraph",
"value": {
"_tag": "Text",
"value": "One of the following: true, false",
},
},
"right": {
"_tag": "Paragraph",
"value": {
"_tag": "Text",
"value": "This setting is optional. Defaults to: false",
},
},
},
],
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"_tag": "DescriptionList",
"definitions": [
[
{
"_tag": "Sequence",
"left": {
"_tag": "Text",
"value": "--count",
},
"right": {
"_tag": "Sequence",
"left": {
"_tag": "Text",
"value": " ",
},
"right": {
"_tag": "Text",
"value": "integer",
},
},
},
{
"_tag": "Sequence",
"left": {
"_tag": "Paragraph",
"value": {
"_tag": "Text",
"value": "An integer.",
},
},
"right": {
"_tag": "Paragraph",
"value": {
"_tag": "Text",
"value": "This setting is optional. Defaults to: 42",
},
},
},
],
],
}