implemented specific help
This commit is contained in:
parent
51b788bb50
commit
5bce49eed4
@ -1,7 +1,6 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@ -13,6 +12,7 @@ type Program struct {
|
|||||||
Name string
|
Name string
|
||||||
Function ProgramFunction
|
Function ProgramFunction
|
||||||
ShortDescription string
|
ShortDescription string
|
||||||
|
LongDescription string
|
||||||
Arguments []Argument
|
Arguments []Argument
|
||||||
}
|
}
|
||||||
type Argument struct {
|
type Argument struct {
|
||||||
@ -23,12 +23,13 @@ type Argument struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var HelpHeader = `This is the help page of transfem-startpage.
|
var HelpHeader = `This is the help page of transfem-startpage.
|
||||||
` + color.Bold + `transfem-startpage {program} {...args}` + color.Reset + `
|
` + color.Purple + `transfem-startpage {program} {...args}` + color.Reset + `
|
||||||
The following Programs are available:`
|
The following Programs are available:`
|
||||||
var Programs = []Program{
|
var Programs = []Program{
|
||||||
{
|
{
|
||||||
Name: "help",
|
Name: "help",
|
||||||
ShortDescription: "get more information on how the cli in general or a specific program works",
|
ShortDescription: "get more information on how the cli in general or a specific program works",
|
||||||
|
LongDescription: "What did you expect to find here?",
|
||||||
Arguments: []Argument{
|
Arguments: []Argument{
|
||||||
{
|
{
|
||||||
Name: "program",
|
Name: "program",
|
||||||
@ -42,6 +43,9 @@ var Programs = []Program{
|
|||||||
Name: "start",
|
Name: "start",
|
||||||
Function: Start,
|
Function: Start,
|
||||||
ShortDescription: "start the webserver",
|
ShortDescription: "start the webserver",
|
||||||
|
LongDescription: `The start program starts the webserver.
|
||||||
|
It loads the config file of the according profile.
|
||||||
|
It uses the default values if no config file was found.`,
|
||||||
Arguments: []Argument{
|
Arguments: []Argument{
|
||||||
{
|
{
|
||||||
Name: "profile",
|
Name: "profile",
|
||||||
@ -55,6 +59,9 @@ var Programs = []Program{
|
|||||||
Name: "cache",
|
Name: "cache",
|
||||||
Function: Cache,
|
Function: Cache,
|
||||||
ShortDescription: "do something with the cache",
|
ShortDescription: "do something with the cache",
|
||||||
|
LongDescription: `Does something with the cache.
|
||||||
|
- clear: delete the whole cache
|
||||||
|
- clean: delete all files that aren't used by any program.`,
|
||||||
Arguments: []Argument{
|
Arguments: []Argument{
|
||||||
{
|
{
|
||||||
Name: "action",
|
Name: "action",
|
||||||
@ -66,9 +73,18 @@ var Programs = []Program{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func Cli() {
|
func GetProgram(programName string) Program {
|
||||||
fmt.Println("running transfem startpage")
|
for i, p := range Programs {
|
||||||
|
if p.Name == programName {
|
||||||
|
return Programs[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Panicln("couldn't find program", programName, ". EXITING")
|
||||||
|
return Program{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Cli() {
|
||||||
// getting around initialization cycle
|
// getting around initialization cycle
|
||||||
Programs[0].Function = Help
|
Programs[0].Function = Help
|
||||||
|
|
||||||
@ -77,18 +93,9 @@ func Cli() {
|
|||||||
programName = os.Args[1]
|
programName = os.Args[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("running program", programName)
|
var selectedProgram Program = GetProgram(programName)
|
||||||
|
err := selectedProgram.Function()
|
||||||
var selectedProgram *Program = nil
|
if err != nil {
|
||||||
for i, p := range Programs {
|
log.Panicln(err)
|
||||||
if p.Name == programName {
|
|
||||||
selectedProgram = &Programs[i]
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if selectedProgram == nil {
|
|
||||||
log.Panicln("couldn't find program", programName, ". EXITING")
|
|
||||||
}
|
|
||||||
|
|
||||||
selectedProgram.Function()
|
|
||||||
}
|
|
||||||
|
@ -2,7 +2,8 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/TwiN/go-color"
|
"github.com/TwiN/go-color"
|
||||||
)
|
)
|
||||||
@ -20,6 +21,22 @@ func padString(s string, n int) string {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSingleArgumentString(a Argument) string {
|
||||||
|
requiredString := ""
|
||||||
|
if a.Required {
|
||||||
|
requiredString = "*"
|
||||||
|
}
|
||||||
|
return requiredString + a.Name + ":" + a.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func getArgumentString(arguments []Argument) string {
|
||||||
|
argumentString := color.Blue
|
||||||
|
for _, a := range arguments {
|
||||||
|
argumentString = argumentString + " [" + getSingleArgumentString(a) + "]"
|
||||||
|
}
|
||||||
|
return argumentString + color.Reset
|
||||||
|
}
|
||||||
|
|
||||||
func generalHelp() error {
|
func generalHelp() error {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println(HelpHeader)
|
fmt.Println(HelpHeader)
|
||||||
@ -27,24 +44,46 @@ func generalHelp() error {
|
|||||||
|
|
||||||
for _, p := range Programs {
|
for _, p := range Programs {
|
||||||
fmt.Print(color.Bold + padString(p.Name, 7) + color.Reset)
|
fmt.Print(color.Bold + padString(p.Name, 7) + color.Reset)
|
||||||
|
fmt.Print(padString(getArgumentString(p.Arguments), 40) + p.ShortDescription + "\n")
|
||||||
argumentString := color.Purple
|
|
||||||
for _, a := range p.Arguments {
|
|
||||||
requiredString := ""
|
|
||||||
if a.Required {
|
|
||||||
requiredString = "*"
|
|
||||||
}
|
|
||||||
argumentString = argumentString + " [" + requiredString + a.Name + ":" + a.Type + "]"
|
|
||||||
}
|
|
||||||
argumentString = argumentString + color.Reset
|
|
||||||
|
|
||||||
fmt.Print(padString(argumentString, 40) + p.ShortDescription + "\n")
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func specificHelp(programName string) error {
|
||||||
|
program := GetProgram(programName)
|
||||||
|
|
||||||
|
fmt.Println(color.Bold + "MAN PAGE FOR " + strings.ToUpper(programName) + color.Reset)
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
fmt.Println(color.Purple + "transfem-startpage " + programName + color.Reset + getArgumentString(program.Arguments))
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
fmt.Println(color.Bold + "arguments" + color.Reset)
|
||||||
|
|
||||||
|
argumentStrings := make([]string, len(program.Arguments))
|
||||||
|
maxArgumentString := 0
|
||||||
|
for i, a := range program.Arguments {
|
||||||
|
s := getSingleArgumentString(a)
|
||||||
|
argumentStrings[i] = s
|
||||||
|
if len(s) > maxArgumentString {
|
||||||
|
maxArgumentString = len(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, a := range program.Arguments {
|
||||||
|
fmt.Println(padString(argumentStrings[i], maxArgumentString+4) + a.Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println(program.LongDescription)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Help() error {
|
func Help() error {
|
||||||
log.Println("running help")
|
if len(os.Args) > 2 {
|
||||||
|
return specificHelp(os.Args[2])
|
||||||
|
}
|
||||||
|
|
||||||
return generalHelp()
|
return generalHelp()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user