-
Notifications
You must be signed in to change notification settings - Fork 0
Gettting Started
This article assumes you have a proper Go enviorment setup , as well as a good understanding of the go directory structure.
To obtain raspberry , all one must do is a simple go get github.com/pandademic/raspberry
while in a go source directory.
A raspberry program has the following structure
program function defined or imported
cli definition
cli setup called
cli handler(s) set
this is where you would define the functions the your cli calls each time it receives a command.
A cli definition is when you define a variable of type raspberry.Cli
Example:
cli := rasberry.Cli{AcceptedCommands:[]string{"-v","version","-h","help","hi","hello"},HelpMsg:"Available commands:\n-h\nhelp\n-v\nversion\nhi\nhello",Version:0.1}
This is probably the easiest one.
This is where you call the Setup()
method of your cli variable.
Example:
cli.Setup()
This is where you define handlers to run your functions(that you defined earlier) on the vent of a command , with the following syntax
cli.SetHandler("command name here , case sensitive",name_of_the_function_without_parenthesis)
Some functionality , like -h
or -v
does not need to defined (See the whole list)
Keep in mind however , that they still need to be added to available commands(Unless for some reason you don't want them to be available). This is NOT a bug , it's by design , so that if for some reason you don't want it , you don't have to take it.
Do you remember the small greeting program in the readme , that we essentially replicated above?
Let's make that a little more advanced!
You might be wondering , "Hmmmmm.... most CLI's need arguments. Where are here?"
Well of course , raspberry has support for them!
Raspberry provides an array called Args
with the argument provided to your program , excluding the name of it and the command provided
You can use it like so:
// the cli was called like so:
// greeter hello bob tom tim
raspberry.Args[0] // bob
raspberry.Args[1] // tom
raspberry.Args[2] // tim
Now I have a nice challenge for you!
Based on what you learnt above , can you edit the greeter program to greet by name?
I'll call it on the command line , like this:
greeter hello atha
and it should respond like so:
Hello atha!
Hint: the related source code is linked below....
Happy coding!
Thanks for reading!