Skip to content

Commit

Permalink
improve cli help
Browse files Browse the repository at this point in the history
  • Loading branch information
Glowman554 committed May 27, 2024
1 parent 965590f commit 75f3808
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 20 deletions.
32 changes: 25 additions & 7 deletions fire/arguments/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ type Node struct {
Value *string
}

type Allowed struct {
Name string
Description string
}

type Parser struct {
Allowed []string
Allowed []Allowed
Nodes []Node
}

func NewParser() *Parser {
return &Parser{
Allowed: []string{},
Allowed: []Allowed{},
Nodes: []Node{},
}
}
Expand Down Expand Up @@ -51,17 +56,30 @@ func (p *Parser) Parse(args []string) error {

func (p *Parser) isValid(arg string) bool {
for i := range p.Allowed {
if p.Allowed[i] == arg {
if p.Allowed[i].Name == arg {
return true
}
}
return false
}

func (p *Parser) help() {
maxLen := 0
for _, entry := range p.Allowed {
if len(entry.Name) > maxLen {
maxLen = len(entry.Name)
}
}

fmt.Println("Valid options: ")
for i := range p.Allowed {
fmt.Println("> --" + p.Allowed[i])
for _, entry := range p.Allowed {
paddingAmount := maxLen - len(entry.Name)
padding := ""
for range paddingAmount {
padding += " "
}

fmt.Println("> --" + entry.Name + padding + " - " + entry.Description)
}
}

Expand Down Expand Up @@ -95,8 +113,8 @@ func (p *Parser) Has(name string) bool {
return false
}

func (p *Parser) Allow(a string) {
p.Allowed = append(p.Allowed, a)
func (p *Parser) Allow(a string, des string) {
p.Allowed = append(p.Allowed, Allowed{Name: a, Description: des})
}

func remove(s []Node, index int) []Node {
Expand Down
1 change: 1 addition & 0 deletions fire/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "fire/arguments"
type Command interface {
PopulateParser(parser *arguments.Parser)
Execute(parser *arguments.Parser) error
Description() string
}
4 changes: 4 additions & 0 deletions fire/command/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ func (Build) Execute(parser *arguments.Parser) error {

return nil
}

func (Build) Description() string {
return "Build a project"
}
56 changes: 56 additions & 0 deletions fire/command/commands/compile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package commands

import (
"fire/arguments"
"fire/firestorm"
"strings"
)

type Compile struct{}

func (Compile) PopulateParser(parser *arguments.Parser) {
parser.Allow("input", "Input file")
parser.Allow("outpu", "Output file")
parser.Allow("target", "Compilation target")
parser.Allow("include", "Add file to include path")
}

func (Compile) Execute(parser *arguments.Parser) error {
input, err := parser.Consume("input", nil)
if err != nil {
return err
}

defaultOutput := "a." + firestorm.DetectExtension()
output, err := parser.Consume("output", &defaultOutput)
if err != nil {
return err
}

defaultTarget := firestorm.DetectTarget()
target, err := parser.Consume("target", &defaultTarget)
if err != nil {
return err
}

includes := []string{}
for parser.Has("include") {
include, err := parser.Consume("include", nil)
if err != nil {
return nil
}
if !strings.HasSuffix(*include, "/") {
includes = append(includes, *include+"/")
} else {
includes = append(includes, *include)
}
}

firestorm.Compile(*input, *output, *target, includes)

return nil
}

func (Compile) Description() string {
return "Compile a file withouth using the build system"
}
8 changes: 6 additions & 2 deletions fire/command/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
type Create struct{}

func (Create) PopulateParser(parser *arguments.Parser) {
parser.Allow("username")
parser.Allow("password")
parser.Allow("username", "Account username")
parser.Allow("password", "Password for account")
}

func (Create) Execute(parser *arguments.Parser) error {
Expand Down Expand Up @@ -40,3 +40,7 @@ func (Create) Execute(parser *arguments.Parser) error {
err = storage.StoreToken(token.Token)
return err
}

func (Create) Description() string {
return "Create a user account"
}
10 changes: 7 additions & 3 deletions fire/command/commands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
type Delete struct{}

func (Delete) PopulateParser(parser *arguments.Parser) {
parser.Allow("account")
parser.Allow("package")
parser.Allow("version")
parser.Allow("account", "Set to delete account")
parser.Allow("package", "Package to delete")
parser.Allow("version", "Package version to delete")
}

func (Delete) Execute(parser *arguments.Parser) error {
Expand Down Expand Up @@ -57,3 +57,7 @@ func (Delete) Execute(parser *arguments.Parser) error {

return nil
}

func (Delete) Description() string {
return "Delete a package / version or user account"
}
6 changes: 5 additions & 1 deletion fire/command/commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Deploy struct{}

func (Deploy) PopulateParser(parser *arguments.Parser) {
parser.Allow("init")
parser.Allow("init", "Set to remote init package")
}

func (Deploy) Execute(parser *arguments.Parser) error {
Expand Down Expand Up @@ -66,3 +66,7 @@ func (Deploy) Execute(parser *arguments.Parser) error {

return nil
}

func (Deploy) Description() string {
return "Deploy a project to the package registry"
}
4 changes: 4 additions & 0 deletions fire/command/commands/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func (Executable) Execute(parser *arguments.Parser) error {

return project.Save(proj)
}

func (Executable) Description() string {
return "Convert a project to be executable"
}
8 changes: 6 additions & 2 deletions fire/command/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
type Get struct{}

func (Get) PopulateParser(parser *arguments.Parser) {
parser.Allow("package")
parser.Allow("version")
parser.Allow("package", "Package name")
parser.Allow("version", "Package version")
}

func (Get) Execute(parser *arguments.Parser) error {
Expand Down Expand Up @@ -63,3 +63,7 @@ func (Get) Execute(parser *arguments.Parser) error {

return nil
}

func (Get) Description() string {
return "Get a project from the package registry"
}
8 changes: 6 additions & 2 deletions fire/command/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
type Init struct{}

func (Init) PopulateParser(parser *arguments.Parser) {
parser.Allow("name")
parser.Allow("executable")
parser.Allow("name", "Project name")
parser.Allow("executable", "Set if project is executable")
}

func (Init) Execute(parser *arguments.Parser) error {
Expand All @@ -22,3 +22,7 @@ func (Init) Execute(parser *arguments.Parser) error {

return project.Save(project.NewProject(*name, parser.Has("executable")))
}

func (Init) Description() string {
return "Init a new project"
}
8 changes: 6 additions & 2 deletions fire/command/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
type Login struct{}

func (Login) PopulateParser(parser *arguments.Parser) {
parser.Allow("username")
parser.Allow("password")
parser.Allow("username", "Account username")
parser.Allow("password", "Password for account")
}

func (Login) Execute(parser *arguments.Parser) error {
Expand Down Expand Up @@ -40,3 +40,7 @@ func (Login) Execute(parser *arguments.Parser) error {
err = storage.StoreToken(token.Token)
return err
}

func (Login) Description() string {
return "Login with an user account"
}
4 changes: 4 additions & 0 deletions fire/command/commands/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ func (Validate) Execute(parser *arguments.Parser) error {
}
return err
}

func (Validate) Description() string {
return "Run the tests"
}
16 changes: 15 additions & 1 deletion fire/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var AvailableCommands = map[string]command.Command{
"validate": commands.Validate{},
"get": commands.Get{},
"executable": commands.Executable{},
"compile": commands.Compile{},
}

func main() {
Expand All @@ -28,9 +29,22 @@ func main() {

subcommand := os.Args[1]
if subcommand == "help" {
maxLen := 0
for key := range AvailableCommands {
if len(key) > maxLen {
maxLen = len(key)
}
}

fmt.Println("Available commands:")
for key := range AvailableCommands {
fmt.Println("> " + key)
paddingAmount := maxLen - len(key)
padding := ""
for range paddingAmount {
padding += " "
}

fmt.Println("> " + key + padding + " - " + AvailableCommands[key].Description())
}
return
}
Expand Down

0 comments on commit 75f3808

Please sign in to comment.