commandline

package
v0.0.50 Latest Latest
Warning

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

Go to latest
Published: May 1, 2023 License: MIT Imports: 14 Imported by: 21

README

Command Line

The commandline package provides utilities for creating an interactive shell command for a cli using the go-prompt package.

Usage

To use the commandline package, first import the package:

import "github.com/synapsecns/sanguine/core/commandline"

Then, create a new commandline.CommandLine with the desired value type:

shellCommands := []*cli.Command{
	{
		Name:  "example",
		Usage: "example command",
		Action: func(c *cli.Context) error {
      return nil
    },
  }
}
app := &cli.App{
  Name:    "example",
  Usage:   "example interactive command line",
  Commands: shellCommands,
}
shellCommand := commandline.GenerateShellCommand(shellCommands)
app.Commands = append(app.Commands, shellCommand)

Interactive Shell

The commandline package provides an interactive shell for the cli application. The shell can be started by running the run command:

$ example shell

Autocompletion

Autocompletion is supported for both commands and flags. To see the available commands, enter help. To see help for a specific command, enter help .

Autocompletion for flags will only suggest flags that are valid for the current command. For example, if the current command is example, then the flag --example will be suggested, but not --example2.

Exiting

To exit the shell, you can type quit, q or exit.

Documentation

Overview

Package commandline contains common utilities for use across clis

Index

Examples

Constants

This section is empty.

Variables

View Source
var LogLevel = cli.StringFlag{
	Name:        "log-level",
	Usage:       fmt.Sprintf("set the log level for the application to one of %s", LogLevelOptions()),
	Value:       zapcore.WarnLevel.String(),
	DefaultText: "",
}

LogLevel sets the log level for the node.

Functions

func GenerateShellCommand

func GenerateShellCommand(shellCommands []*cli.Command) *cli.Command

GenerateShellCommand generates the shell command with a list of commands that the shell should take. TODO: this needs a more comprehensive test suite.

func IsValidLogLevel

func IsValidLogLevel(level string) bool

IsValidLogLevel determines if a log level is valid.

func LogLevelOptions

func LogLevelOptions() (res string)

LogLevelOptions generates log level options and returns them as a string.

func SetLogLevel

func SetLogLevel(c *cli.Context)

SetLogLevel parses the cli context and sets the global log level accordingly. Note: for this to work the log level has to be set in the command.

Example
package main

import (
	"fmt"
	"github.com/ipfs/go-log"
	"github.com/synapsecns/sanguine/core/commandline"
	"github.com/urfave/cli/v2"
	"go.uber.org/zap/zapcore"
	"os"
)

var logger = log.Logger("synapse-logger-example")

func main() {
	app := cli.NewApp()
	app.Commands = cli.Commands{
		{
			Name:        "test",
			Description: "I'm a test command",
			Flags: []cli.Flag{
				&commandline.LogLevel,
			},
			Action: func(c *cli.Context) error {
				commandline.SetLogLevel(c)
				logger.Debug("I won't be shown if level is set to warn")
				logger.Warn("I will be shown if level is set to warn")
				return nil
			},
		},
	}
	fmt.Printf("running ./example %s --%s %s", app.Commands[0].Name, commandline.LogLevel.Name, zapcore.WarnLevel.String())

	err := app.Run([]string{os.Args[0], app.Commands[0].Name, commandline.LogLevel.Name, zapcore.WarnLevel.String()})
	if err != nil {
		panic(err)
	}

}
Output:

running ./example test --log-level warn

Types

This section is empty.

Jump to

Keyboard shortcuts

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