-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,82 @@ | ||
import { program, command } from '../src' | ||
|
||
const syncOk = command('sync', 'Print sync message').action(function () { | ||
console.log('ok/sync') | ||
}) | ||
const asyncOk = command('async', 'Print sync message').action( | ||
async function () { | ||
const app = program() | ||
|
||
const syncOk = command('sync') | ||
.description('Print sync message') | ||
.action(function () { | ||
console.log('ok/sync') | ||
}) | ||
|
||
const asyncOk = command('async') | ||
.description('Print sync message') | ||
.action(async function () { | ||
console.log('ok/async') | ||
} | ||
) | ||
const syncNok = command('sync', 'Throw sync error').action(function () { | ||
throw new Error('nok/sync') | ||
}) | ||
const asyncNok = command('async', 'Throw async error').action( | ||
async function () { | ||
}) | ||
|
||
const syncNok = command('sync') | ||
.description('Throw sync error') | ||
.action(function () { | ||
throw new Error('nok/sync') | ||
}) | ||
|
||
const asyncNok = command('async') | ||
.description('Throw async error') | ||
.action(async function () { | ||
throw new Error('nok/async') | ||
} | ||
}) | ||
|
||
const syncValidation = command('sync') | ||
.description('Test validation error with sync handler') | ||
.argument('required') | ||
.action(function () { | ||
console.log('call without arguments') | ||
}) | ||
|
||
const asyncValidation = command('async') | ||
.description('Test validation error with async handler') | ||
.argument('required') | ||
.action(async function () { | ||
console.log('call without arguments') | ||
}) | ||
|
||
const noHandler = command('no_handler').description( | ||
'Test missing command handler' | ||
) | ||
|
||
// Say bye | ||
const printBye = () => console.log('Bye!') | ||
const success = (resolved: unknown) => console.log('[success]', resolved) | ||
|
||
// Print error message only (omit stack trace) and exit with a meaningful status | ||
const printError = (error: any) => { | ||
console.error(String(error)) | ||
process.exit(42) | ||
const fail = (error: any) => { | ||
console.error('[failed]', String(error)) | ||
|
||
if (!app.isRepl()) { | ||
process.exit(42) | ||
} | ||
} | ||
|
||
program() | ||
.add(command('ok', 'Print various messages').add(syncOk).add(asyncOk)) | ||
.add(command('nok', 'Throw various errors').add(syncNok).add(asyncNok)) | ||
app | ||
.add( | ||
command('ok') | ||
.description('Print message to stdout from handler') | ||
.add(syncOk) | ||
.add(asyncOk) | ||
) | ||
.add( | ||
command('nok') | ||
.description('Throw various errors') | ||
.add(syncNok) | ||
.add(asyncNok) | ||
) | ||
.add( | ||
command('validation') | ||
.description('Validation errors') | ||
.add(syncValidation) | ||
.add(asyncValidation) | ||
) | ||
.add(noHandler) | ||
.withHelp() | ||
.runOrRepl() | ||
.then(printBye) | ||
.catch(printError) | ||
.then(success) | ||
.catch(fail) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { program, command } from '../src' | ||
|
||
const string = command('string') | ||
.argument('arg', 'Required string argument') | ||
.option('opt', 'Optional string option', { type: 'string' }) | ||
.action((args) => { | ||
console.log('Args are', args) | ||
}) | ||
|
||
const app = program('All argument and option types').add(string) | ||
|
||
app.withHelp().runOrRepl() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters