Skip to content

Commit

Permalink
Fix the parsed letter case for variadic and key/value CLI flags (#1963)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 authored Jan 23, 2024
1 parent 84da31f commit de4cb04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-deers-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/cli": patch
---

fix the parsed letter case for variadic and key/value flags
21 changes: 11 additions & 10 deletions packages/cli/src/internal/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,7 @@ const parseCommandLine = (
onNonEmpty: (head, tail) => {
const normalize = (value: string) => InternalCliConfig.normalizeCase(config, value)
const normalizedHead = normalize(head)
const normalizedNames = ReadonlyArray.map(getNames(self), normalize)
const normalizedNames = ReadonlyArray.map(getNames(self), (name) => normalize(name))
if (ReadonlyArray.contains(normalizedNames, normalizedHead)) {
if (InternalPrimitive.isBool(self.primitiveType)) {
return ReadonlyArray.matchLeft(tail, {
Expand Down Expand Up @@ -1727,7 +1727,7 @@ const parseCommandLine = (
))
}
case "KeyValueMap": {
const singleNames = ReadonlyArray.map(
const normalizedNames = ReadonlyArray.map(
getNames(self.argumentOption),
(name) => InternalCliConfig.normalizeCase(config, name)
)
Expand All @@ -1740,10 +1740,10 @@ const parseCommandLine = (
let keyValues = ReadonlyArray.empty<string>()
let leftover = args as ReadonlyArray<string>
while (ReadonlyArray.isNonEmptyReadonlyArray(leftover)) {
const name = leftover[0].trim()
const name = ReadonlyArray.headNonEmpty(leftover).trim()
const normalizedName = InternalCliConfig.normalizeCase(config, name)
// Can be in the form of "--flag key1=value1 --flag key2=value2"
if (leftover.length >= 2 && ReadonlyArray.contains(singleNames, normalizedName)) {
if (leftover.length >= 2 && ReadonlyArray.contains(normalizedNames, normalizedName)) {
const keyValue = leftover[1].trim()
const [key, value] = keyValue.split("=")
if (key !== undefined && value !== undefined && value.length > 0) {
Expand All @@ -1765,26 +1765,27 @@ const parseCommandLine = (
}
return [keyValues, leftover]
}
const name = InternalCliConfig.normalizeCase(config, head)
if (ReadonlyArray.contains(singleNames, name)) {
const normalizedName = InternalCliConfig.normalizeCase(config, head)
if (ReadonlyArray.contains(normalizedNames, normalizedName)) {
const [values, leftover] = loop(tail)
return Effect.succeed({ parsed: Option.some({ name, values }), leftover })
return Effect.succeed({ parsed: Option.some({ name: head, values }), leftover })
}
return Effect.succeed<ParsedCommandLine>({ parsed: Option.none(), leftover: args })
}
})
}
case "Variadic": {
const singleNames = ReadonlyArray.map(
const normalizedNames = ReadonlyArray.map(
getNames(self.argumentOption),
(name) => InternalCliConfig.normalizeCase(config, name)
)
let optionName: string | undefined = undefined
let values = ReadonlyArray.empty<string>()
let leftover = args as ReadonlyArray<string>
while (ReadonlyArray.isNonEmptyReadonlyArray(leftover)) {
const name = InternalCliConfig.normalizeCase(config, ReadonlyArray.headNonEmpty(leftover))
if (leftover.length >= 2 && ReadonlyArray.contains(singleNames, name)) {
const name = ReadonlyArray.headNonEmpty(leftover)
const normalizedName = InternalCliConfig.normalizeCase(config, name)
if (leftover.length >= 2 && ReadonlyArray.contains(normalizedNames, normalizedName)) {
if (optionName === undefined) {
optionName = name
}
Expand Down

0 comments on commit de4cb04

Please sign in to comment.