8000 GitHub - StevenCyb/GoCLI: A simple CLI library for GoLang
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

StevenCyb/GoCLI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Structure

This is a simple CLI tool that can be configured as seen in the chart below.

flowchart LR
CLI[cli] --> COMMAND[commands]
COMMAND --> OPTIONS[options]
COMMAND --> SUBCOMMAND[subcommands 1]
SUBCOMMAND2 --> ARGS
SUBCOMMAND --> ARGS
COMMAND --> ARGS[args]
SUBCOMMAND --> SUBCOMMAND2[subcommands ...N]
SUBCOMMAND --> OPTIONS
SUBCOMMAND2 --> OPTIONS
ARGS --> OPTIONS
CLI --> OPTIONS
CLI --> ARGS
Loading

Installation

go get github.com/StevenCyb/GoCLI

Examples

func main() {
	c := cli.New(
		cli.Name("gurl"),
		cli.Banner(`╔═╗╔═╗┬ ┬┬─┐┬
║ ╦║  │ │├┬┘│
╚═╝╚═╝└─┘┴└─┴─┘`),
		cli.Description("A simple CLI for making HTTP requests"),
		cli.Version("1.0.0"),
		cli.Command(
			"get", cli.Argument(
				"url",
				cli.Validate(regexp.MustCompile(`^https?://[^\s/$.?#].[^\s]*$`)),
				cli.Description("The URL to get"),
				cli.Handler(
					func(ctx *cli.Context) error {
						url := ctx.GetArgument("url")
						fmt.Printf("Perform [GET] %s\n", *url)
						return nil
					},
				),
				cli.Option(
					"verbose",
					cli.Short('v'),
					cli.Default(""),
				),
			),
			cli.Description("Get a resource"),
			cli.Example("cli get http://example.com"),
		),
		cli.Command(
			"post", cli.Argument(
				"url",
				cli.Validate(regexp.MustCompile(`^https?://[^\s/$.?#].[^\s]*$`)),
				cli.Description("The URL to post"),
				cli.Handler(
					func(ctx *cli.Context) error {
						url := ctx.GetArgument("url")
						bodyFile := ctx.GetOption("verbose")
						fmt.Printf("Perform [POST] %s\n", *url)
						if bodyFile != nil {
							fmt.Printf("\t With %s\n", *bodyFile)
						}
						return nil
					},
				),
				cli.Option(
					"body_file",
					cli.Short('b'),
				),
				cli.Option(
					"verbose",
					cli.Short('v'),
					cli.Default(""),
				),
			),
			cli.Description("Get a resource"),
			cli.Example("cli get http://example.com"),
		),
		// ...
		cli.Command(
			"version", cli.Handler(
				func(_ *cli.Context) error {
					fmt.Println("1.0.0")
					return nil
				},
			),
			cli.Description("Get the version of the CLI"),
		),
	)

	_, err := c.RunWith(os.Args)
	if err != nil {
		fmt.Println(err)
		c.PrintHelp()
		os.Exit(1)
	}
}

About

A simple CLI library for GoLang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0