Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
cleanup options parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 committed Nov 29, 2023
1 parent 2b5fceb commit 0249133
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 371 deletions.
12 changes: 7 additions & 5 deletions src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,18 +398,20 @@ export const parse: {
export const repeated: <A>(self: Options<A>) => Options<ReadonlyArray<A>> = InternalOptions.repeated

/**
* Parses the provided command-line arguments looking for the specified options,
* and returns an `Option<ValidationError>`, any leftover arguments, and the
* Processes the provided command-line arguments, searching for the specified
* `Options`.
*
* Returns an `Option<ValidationError>`, any leftover arguments, and the
* constructed value of type `A`. The possible error inside
* `Option<ValidationError>` would only be triggered if there is an error when
* parsing the command-line arguments. This is because `ValidationError`s are
* also used internally to control the end of the command-line arguments (i.e.
* the command-line symbol `-`) corresponding to options.
* the command-line symbol `--`) corresponding to options.
*
* @since 1.0.0
* @category combinators
*/
export const validate: {
export const processCommandLine: {
(
args: ReadonlyArray<string>,
config: CliConfig
Expand All @@ -429,7 +431,7 @@ export const validate: {
ValidationError,
[Option<ValidationError>, ReadonlyArray<string>, A]
>
} = InternalOptions.validate
} = InternalOptions.processCommandLine

/**
* @since 1.0.0
Expand Down
19 changes: 10 additions & 9 deletions src/internal/commandDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,14 +593,15 @@ const parseInternal = (
const help = getHelpInternal(self)
const usage = getUsageInternal(self)
const options = InternalBuiltInOptions.builtInOptions(self, usage, help)
return InternalOptions.validate(options, ReadonlyArray.drop(args, 1), config).pipe(
Effect.flatMap((tuple) => tuple[2]),
Effect.catchTag("NoSuchElementException", () => {
const error = InternalHelpDoc.p("No built-in option was matched")
return Effect.fail(InternalValidationError.noBuiltInMatch(error))
}),
Effect.map(InternalCommandDirective.builtIn)
)
return InternalOptions.processCommandLine(options, ReadonlyArray.drop(args, 1), config)
.pipe(
Effect.flatMap((tuple) => tuple[2]),
Effect.catchTag("NoSuchElementException", () => {
const error = InternalHelpDoc.p("No built-in option was matched")
return Effect.fail(InternalValidationError.noBuiltInMatch(error))
}),
Effect.map(InternalCommandDirective.builtIn)
)
}
}
const error = InternalHelpDoc.p(`Missing command name: '${self.name}'`)
Expand All @@ -615,7 +616,7 @@ const parseInternal = (
> =>
parseCommandLine(args).pipe(Effect.flatMap((commandOptionsAndArgs) => {
const [optionsAndArgs, forcedCommandArgs] = splitForcedArgs(commandOptionsAndArgs)
return InternalOptions.validate(self.options, optionsAndArgs, config).pipe(
return InternalOptions.processCommandLine(self.options, optionsAndArgs, config).pipe(
Effect.flatMap(([error, commandArgs, optionsType]) =>
InternalArgs.validate(
self.args,
Expand Down
Loading

0 comments on commit 0249133

Please sign in to comment.