Skip to content

Instantly share code, notes, and snippets.

@GZGavinZhao
Last active August 9, 2021 23:53
Show Gist options
  • Save GZGavinZhao/a3fd25d1fe8560bbe9f768a9b63e902a to your computer and use it in GitHub Desktop.
Save GZGavinZhao/a3fd25d1fe8560bbe9f768a9b63e902a to your computer and use it in GitHub Desktop.
Blueprint of what an example cli in V should be.
module main
import cli, os
fn main(
mut app := cli.Command{
name: 'pizzahut'
description: 'Cli for ordering pizza from Pizza Hut'
// execute: // Without specifying the `execute` parameter, it will just output the usage message
commands: [
cli.Command{
name: 'order'
description: 'Order food'
flags: [
Flag<string>{
name: 'delivery'
abbrev: 'd'
description: 'Delivery options'
// If set to false, capitalization won't matter
// This will only work for string
strict: false
options: [
'pickup'
'delivery'
]
}
]
pre_execute: fn (cmd cli.Command) {
assert cmd.rest.len != 0 or {
panic('You ordered nothing!')
}
}
execute: fn (cmd cli.Command) {
if cmd.results['verbose'] as bool {
println('You ordered ${cmd.rest}')
}
}
}
]
flags: [
Flag<bool>{
name: 'verbose'
abbrev: 'v'
description: 'Output more verbose results'
default: false
global: true
}
]
}
app.run(os.args) or {
println('${err}\n')
println(app.usage())
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment