Skip to content

Commit 87f7881

Browse files
committed
WIP: echo-help.ts and HelpError progress
1 parent 1bdf999 commit 87f7881

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

commands/echo-help.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class UsageError extends HelpError {
1111
override help = [
1212
'USAGE:',
1313
'`echo-help.ts --test`',
14+
'`echo-help.ts --help`',
1415
'`echo-help.ts [...<styled error messages>] < template.txt`',
1516
'',
1617
'NOTE:',
@@ -21,16 +22,14 @@ class UsageError extends HelpError {
2122
// Execute
2223
async function main(...args: string[]) {
2324
const help = await readStdinWhole()
24-
class CustomError extends HelpError {
25-
override help: help
26-
override code: 0
27-
}
28-
return new CustomError(...args)
25+
throw new HelpError({help, code: 0}, ...args)
2926
}
3027

3128
// invoke command or tests
3229
try {
33-
if (Deno.args.join('') == '--test' ) {
30+
if (Deno.args.join('') == '--help' ) {
31+
throw new UsageError()
32+
} else if (Deno.args.join('') == '--test' ) {
3433
await exec(['eval-tester', '--fixture=echo-help', '--', 'echo-help.ts'])
3534
} else {
3635
await main(...Deno.args)

commands/eval-tester

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ function eval_tester() (
222222
if [[ -z $fixture_option_close ]]; then
223223
if [[ "${fixture_content:i:split_length}" == "$split" ]]; then
224224
i=$((i + split_length - 2)) # include the trailing newline in the next match
225-
fixture_count="$((fixture_count + 1))"
226225
if [[ ${#fixture_options[@]} -ne 0 ]]; then
226+
fixture_count="$((fixture_count + 1))"
227227
if [[ -z $fixture_name ]]; then
228228
if [[ -n $option_name ]]; then
229229
fixture_name="${option_name}: fixture #$fixture_count"

sources/ts.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,21 @@ export class CodeError extends Error {
721721
export class HelpError extends Error {
722722
readonly help: string = ''
723723
readonly messages: string[] = []
724-
readonly code = 22
725-
constructor(...messages: string[]) {
724+
readonly code: number = 22
725+
constructor(config: { help?: string; code?: number }, ...messages: string[])
726+
constructor(...messages: string[])
727+
constructor(
728+
first?: string | { help?: string; code?: number },
729+
...rest: string[]
730+
) {
726731
super()
727-
this.messages = messages
732+
if (typeof first === 'object' && first !== null) {
733+
if (first.help != null) this.help = first.help
734+
if (first.code != null) this.code = first.code
735+
this.messages = rest
736+
} else if (typeof first === 'string') {
737+
this.messages = [first, ...rest]
738+
}
728739
}
729740
public override toString(): string {
730741
if (this.messages.length) {
@@ -758,7 +769,7 @@ export async function exec(cmd: string[]): Promise<void> {
758769
}
759770
}
760771

761-
export type AssertValues<T extends readonly unknown[]> = T[number]
772+
export type AssertFactoryValues<T extends readonly unknown[]> = T[number]
762773
export function assertFactory<T extends readonly unknown[]>(
763774
values: T,
764775
typeName: string,
@@ -807,7 +818,7 @@ export function asError(value: unknown): Error {
807818

808819
export async function exitWithError(error?: unknown, status?: number) {
809820
if (status == null || status < 0) {
810-
if (error instanceof CodeError) {
821+
if (error instanceof CodeError || error instanceof HelpError) {
811822
status = error.code ?? 1
812823
} else {
813824
status = 1

0 commit comments

Comments
 (0)