cmd

package
v0.0.0-...-0795abe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Example (FormatTable)
package main

import (
	"fmt"

	"github.com/internet-computer/oko/internal/cmd"
)

func main() {
	fmt.Println(cmd.FormatTable([][]string{
		{"_", "__", "___"},
		{"a", "b", "c"},
		{"____", "d", "_", "e"},
	}, " | ", "\n", "| "))
}
Output:

| _    | __ | ___
| a    | b  | c
| ____ | d  | _   | e

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ask

func Ask(q string) string

func AskForConfirmation

func AskForConfirmation(q string) bool

func FormatTable

func FormatTable(table [][]string, sepColumn, sepRow, prefixRow string) string

func Manual

func Manual(commands []Command) string

Returns a MD styled docs for the given list of commands.

Types

type Command

type Command struct {
	// The name of the command.
	Name string
	// Aliases of the name.
	// e.g. version -> v
	Aliases []string
	// A summary explaining the function of the command.
	Summary string
	// A longer description of the command.
	Description string

	// A list of sub commands.
	Commands []Command

	// A list of arguments.
	Args []string
	// Options of the command.
	// e.g. --all, etc.
	Options []Option
	// The method corresponding with a list of arguments.
	Method func(args []string, options map[string]string) error
}

A command with either sub-commands or a list of arguments.

func (Command) Call

func (c Command) Call(args ...string) error

func (Command) Help

func (c Command) Help()
Example
package main

import (
	"fmt"

	"github.com/internet-computer/oko/internal/cmd"
)

var (
	s = cmd.Command{
		Name: "sub",
		Args: []string{"c"},
		Options: []cmd.Option{
			{"all", "", false},
			{"v", "", true},
		},
		Method: func(args []string, options map[string]string) error {
			fmt.Println(args, options)
			return nil
		},
	}
	c = cmd.Command{
		Name:     "test",
		Aliases:  []string{"t"},
		Commands: []cmd.Command{s},
	}
)

func main() {
	c.Help()
}
Output:

Usage:
	test <command>

Commands:
	<sub>
Example (Sub)
package main

import (
	"fmt"

	"github.com/internet-computer/oko/internal/cmd"
)

var (
	s = cmd.Command{
		Name: "sub",
		Args: []string{"c"},
		Options: []cmd.Option{
			{"all", "", false},
			{"v", "", true},
		},
		Method: func(args []string, options map[string]string) error {
			fmt.Println(args, options)
			return nil
		},
	}
	c = cmd.Command{
		Name:     "test",
		Aliases:  []string{"t"},
		Commands: []cmd.Command{s},
	}
)

func main() {
	_ = c.Call("sub", "help")
}
Output:

Usage:
	sub <c>

Optional arguments:
	all
	v  	<value>
Example (SubC)
package main

import (
	"fmt"

	"github.com/internet-computer/oko/internal/cmd"
)

var (
	s = cmd.Command{
		Name: "sub",
		Args: []string{"c"},
		Options: []cmd.Option{
			{"all", "", false},
			{"v", "", true},
		},
		Method: func(args []string, options map[string]string) error {
			fmt.Println(args, options)
			return nil
		},
	}
	c = cmd.Command{
		Name:     "test",
		Aliases:  []string{"t"},
		Commands: []cmd.Command{s},
	}
)

func main() {
	_ = c.Call("sub", "c")
	_ = c.Call("sub", "--all", "c")
	_ = c.Call("sub", "c", "--all")
	_ = c.Call("sub", "c", "--v=0")
	_ = c.Call("sub", "--v", "0", "c")
}
Output:

[c] map[]
[c] map[all:]
[c] map[all:]
[c] map[v:0]
[c] map[v:0]

type CommandNotFoundError

type CommandNotFoundError struct{}

func NewCommandNotFoundError

func NewCommandNotFoundError() *CommandNotFoundError

func (CommandNotFoundError) Error

func (e CommandNotFoundError) Error() string

type InvalidArgumentsError

type InvalidArgumentsError struct {
	Message string
}

func NewInvalidArgumentsError

func NewInvalidArgumentsError(msg string) *InvalidArgumentsError

func (InvalidArgumentsError) Error

func (e InvalidArgumentsError) Error() string

type Option

type Option struct {
	Name     string
	Summary  string
	HasValue bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL