cli
Command line programs with several commands, like git commit, and asking the user questions.
import cli
Many tools have several commands: salam build, salam run. The cli package registers the commands, works out from the first argument which one was asked for, and builds the overall help text. For each command's own flags, use the flag package alongside it.
Prompt and Confirm ask the user something in the terminal; their example is not run here because it would wait for an answer.
Types
App
struct App| Name | Type | Description |
|---|---|---|
name | str = "" | The program's name. |
desc | str = "" | The program's description. |
version | str = "" | The program's version. |
cmd_names | Vector | The names of the commands. |
cmd_descs | Vector | The descriptions of the commands. |
import cli
import str
func run(app &: cli.App, line: str):
args := str.Split(line, " ")
command := cli.Dispatch(app, args)
if command == cli.CmdHelp:
println "help was asked for"
else command == cli.CmdVersion:
println "version:", app.version
else command == cli.CmdUnknown:
println "unknown command"
else:
println "running:", command, cli.RemainingArgs(args)
end
args.free()
end
func main:
mut app := cli.New("salamctl", "project management", "1.0.0")
cli.Register(app, "build", "build the project")
cli.Register(app, "test", "run the tests")
println cli.HasCommand(app, "build"), cli.CommandDesc(app, "test")
each line in ["build --fast", "--help", "--version", "deploy"]:
run(app, line)
end
print cli.Usage(app)
endConstants and variables
CmdHelp
const CmdHelp: str = "__help__"Dispatch returns when help was asked for, or no argument was given.The example for this item is shown with the ones above: cli-Dispatch.salam
CmdVersion
const CmdVersion: str = "__version__"Dispatch returns when the version was asked for.The example for this item is shown with the ones above: cli-Dispatch.salam
CmdUnknown
const CmdUnknown: str = "__unknown__"Dispatch returns for a command that was never registered.The example for this item is shown with the ones above: cli-Dispatch.salam
Functions
New
func New(name: str, desc: str, version: str): AppThe example for this item is shown with the ones above: cli-Dispatch.salam
Register
func Register(app &: App, name: str, desc: str)The example for this item is shown with the ones above: cli-Dispatch.salam
HasCommand
func HasCommand(app &: App, name: str): boolThe example for this item is shown with the ones above: cli-Dispatch.salam
CommandDesc
func CommandDesc(app &: App, name: str): strThe example for this item is shown with the ones above: cli-Dispatch.salam
Dispatch
func Dispatch(app &: App, argv: Vector<str>): str--help and --version are recognised automatically.The example for this item is shown with the ones above: cli-Dispatch.salam
RemainingArgs
func RemainingArgs(argv: Vector<str>): Vector<str>The example for this item is shown with the ones above: cli-Dispatch.salam
Usage
func Usage(app &: App): strThe example for this item is shown with the ones above: cli-Dispatch.salam
RequireFlags
func RequireFlags(fs &: flag.FlagSet, names: Vector<str>): Vector<str>import cli
import flag
func main:
mut f := flag.New("deploy", "")
defer flag.Free(f)
flag.AddStr(f, "host", "h", "", "the server")
flag.AddStr(f, "user", "u", "", "the user")
mut args := Vector {} as Vector<str>
defer args.free()
args.push("--host=example.com")
flag.Parse(f, args)
mut required := Vector {} as Vector<str>
defer required.free()
required.push("host")
required.push("user")
println "missing:", cli.RequireFlags(f, required)
endConfirm
func Confirm(question: str): booly or yes is true and anything else is false.import cli
func main:
name := cli.Prompt("project name: ")
language := cli.PromptOr("language", "english")
if cli.Confirm("create it?"):
println "creating", name, "in", language
end
endPrompt
func Prompt(question: str): strThe example for this item is shown with the ones above: cli-Prompt.salam
PromptOr
func PromptOr(question: str, def: str): strPrompt, but returns the default when the user types nothing.The example for this item is shown with the ones above: cli-Prompt.salam