Last active
August 9, 2021 07:39
-
-
Save gamemaker1/f79664c196b229cee84fb8dc8e0db604 to your computer and use it in GitHub Desktop.
Suggestion for V CLI module usage
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
// Example of creating a CLI app | |
// Imports | |
import os | |
import cli | |
// Types | |
type FlagType = string | int | bool | string[] | int[] | |
type ArgumentValueType = string | int | bool | string[] | int[] | |
// == The struct way == | |
// Easier to program, but less convenient for the CLI developer (in my opinion) | |
app := cli.App { | |
name: 'My Amazing Program', | |
desc: 'An amazing program', | |
version: '0.1.0', | |
} | |
app.commands = [ | |
cli.Command { | |
name: 'order', | |
arguments: { | |
pizza_type: cli.Argument { | |
desc: 'Type of pizza' | |
required: true | |
} | |
}, | |
flags: { | |
small: cli.Flag { | |
short: 's', long: 'small' | |
desc: 'Order a small pizza', | |
required: false, | |
} | |
large: cli.Flag { | |
short: 'l', long: 'large' | |
desc: 'Order a large pizza', | |
required: false, | |
} | |
}, | |
execute: fn (arguments map[string]cli.ArgumentValueType{}, options map[string]cli.FlagType{}) { | |
println(`Ordering a $arguments.pizza_type pizza...`) | |
println(`Is it small: $options.small`) | |
println(`Is it large: $options.large`) | |
} | |
} | |
] | |
app.run(os.args) | |
// == The functions way == | |
// May be a bit more difficult to program, but IMO more convenient for the CLI developer | |
app := cli.App {} | |
app.name('My Amazing Program') | |
.description('An amazing program') | |
.version('0.1.0') | |
.command('order') | |
.arguments('<pizza_type>', 'Type of pizza') | |
.options('-s, --small', 'Order a small pizza') | |
.options('-l, --large', 'Order a large pizza') | |
.execute(fn (arguments map[string]cli.ArgumentValueType{}, options map[string]cli.FlagType{}) { | |
println(`Ordering a $arguments.pizza_type pizza...`) | |
println(`Is it small: $options.small`) | |
println(`Is it large: $options.large`) | |
}) | |
app.run(os.args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment