-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: example lines, help style updates
- Loading branch information
Showing
7 changed files
with
291 additions
and
160 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,34 @@ | ||
import { massarg } from '.' | ||
import MassargCommand from './command' | ||
import { ParseError } from './error' | ||
import z from 'zod' | ||
import { ValidationError } from './error' | ||
|
||
type A = { test: boolean } | ||
const echoCmd = massarg<any>({ | ||
name: 'echo', | ||
description: 'Echo back the arguments', | ||
aliases: ['e'], | ||
run: (opts) => { | ||
console.log('Echoing back', opts) | ||
}, | ||
export const ExampleConfig = z.object({ | ||
description: z.string().optional(), | ||
input: z.string().optional(), | ||
output: z.string().optional(), | ||
}) | ||
const addCmd = massarg<{ component: string }>({ | ||
name: 'add', | ||
description: 'Add a component', | ||
aliases: ['a'], | ||
run: (opts, parser) => { | ||
parser.printHelp() | ||
console.log('Adding component', opts.component) | ||
}, | ||
}) | ||
.option({ | ||
name: 'component', | ||
description: | ||
'Component to add. Ut consectetur eu et occaecat enim magna amet eiusmod laboris deserunt proident culpa nulla ipsum adipiscing ullamco laboris sed est', | ||
aliases: ['c'], | ||
// aliases: "" as never, | ||
}) | ||
.option({ | ||
name: 'classes', | ||
description: 'Classes to add', | ||
aliases: ['l'], | ||
array: true, | ||
}) | ||
.option({ | ||
name: 'custom', | ||
description: 'Custom option', | ||
aliases: ['x'], | ||
parse: (value) => { | ||
const asNumber = Number(value) | ||
if (isNaN(asNumber)) { | ||
throw new ParseError({ | ||
path: ['custom'], | ||
message: 'Custom option must be a number', | ||
code: 'invalid_number', | ||
}) | ||
} | ||
return { | ||
value: asNumber, | ||
half: asNumber / 2, | ||
double: asNumber * 2, | ||
} | ||
}, | ||
}) | ||
|
||
const removeCmd = new MassargCommand<{ component: string }>({ | ||
name: 'remove', | ||
description: 'Remove a component', | ||
aliases: ['r'], | ||
run: (opts) => { | ||
console.log('Removing component', opts.component) | ||
}, | ||
}).option({ | ||
name: 'component', | ||
description: 'Component to remove', | ||
aliases: ['c'], | ||
}) | ||
|
||
const args = massarg<A>({ | ||
name: 'my-cli', | ||
description: 'This is an example CLI', | ||
bindHelpOption: true, | ||
bindHelpCommand: true, | ||
}) | ||
.main((opts, parser) => { | ||
console.log('Main command - printing all opts') | ||
console.log(opts, '\n') | ||
parser.printHelp() | ||
}) | ||
.command(echoCmd) | ||
.command(addCmd) | ||
.command(removeCmd) | ||
.flag({ | ||
name: 'bool', | ||
description: 'Example boolean option', | ||
aliases: ['b'], | ||
}) | ||
.option({ | ||
name: 'number', | ||
description: 'Example number option', | ||
aliases: ['n'], | ||
type: 'number', | ||
}) | ||
export type ExampleConfig = z.infer<typeof ExampleConfig> | ||
|
||
// console.log("Opts:", args.getArgs(process.argv.slice(2)), "\n") | ||
export default class MassargExample { | ||
description: string | undefined | ||
input: string | undefined | ||
output: string | undefined | ||
|
||
args.parse(process.argv.slice(2)) | ||
constructor(config: ExampleConfig) { | ||
ExampleConfig.parse(config) | ||
if ( | ||
config.description === undefined && | ||
config.input === undefined && | ||
config.output === undefined | ||
) { | ||
throw new ValidationError({ | ||
code: 'invalid_example', | ||
message: 'Example must have at least one of description, input, or output', | ||
path: ['example'], | ||
}) | ||
} | ||
this.description = config.description | ||
this.input = config.input | ||
this.output = config.output | ||
} | ||
} | ||
export { MassargExample } |
Oops, something went wrong.