Skip to content

Gettting Started

atha G edited this page May 27, 2022 · 5 revisions

This article assumes you have a proper Go enviorment setup , as well as a good understanding of the go directory structure.

Obtaining the framework

To obtain raspberry , all one must do is a simple go get github.com/pandademic/raspberry while in a go source directory.

Getting started

A raspberry program has the following structure

program function defined or imported
cli definition
cli setup called
cli handler(s) set

Explanation of the structure

program functionality defined or imported

this is where you would define the functions the your cli calls each time it receives a command.

cli definition

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}
cli Setup

This is probably the easiest one. This is where you call the Setup() method of your cli variable.

Example:

cli.Setup()
cli handler(s) set

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)

A note about Handlers and program functionality

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.

A little more advanced: Handling arguments

Do you remember the small greeting program in the readme , that we essentially replicated above?

Let's make that a little more advanced!

Arguments

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

A challenger approaches!

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....

Source code link #1

Source code link #2

Happy coding!