Categorygithub.com/otiai10/largo
modulepackage
0.0.0-20211018055848-29754d2f6231
Repository: https://github.com/otiai10/largo.git
Documentation: pkg.go.dev

# README

largo - Yet another arg parser of Go in a flexible way

Go codecov Go Report Card Maintainability Go Reference

Motivation

# Let's say your are building `greet`, and can receive any command arg as message.
% greet hello
hello
% greet thanks
thanks

What if greet can accept some flags, but would receive unordered way like below.

# 1) BSD style
% greet -count 3 -upper hello
# 2) GNU style
% greet hello -count 3 -upper
# 3) Mixed...
% greet -count 3 hello -upper

# All output should be
HELLO HELLO HELLO

The problem is that the standard flag package ignores all flags of case (2) and -upper flag of case (3). How can we keep flexibility to parse those flags, even if they are NOT urdered in BSD-style?

Idea

Basically flag.FlagSet works fine, however, we like to keep parsing all given flags until the end of line, and retrieve all remaining args as Rest().

greet -count 3 hello -upper
-----
 cmd
      --------       ------
      int flag      bool flag
               -----
               rest

Usage

import (
  "github.com/otiai10/largo"
)

var (
  count int
  upper bool
)

func main() {
  fset := largo.NewFlaggSet("greet")
  fset.IntVar(&count, "count", 1, "Number of count to say it").Alias("c")
  fset.BoolVar(&upper, "upper", false, "Say it in upper cases").Alias("u")

  // In most cases, it is given as os.Args[1:] or something.
  fset.Parse([]string{"greet", "-c", "3", "hello", "-upper"})

  fmt.Println("count:", count)   // count: 3
  fmt.Println("upper:", upper)   // upper: true
  fmt.Println("rest:", f.Rest()) // rest: [hello]
}

Reference projects using largo

Issues & Feedbacks

Any feedbacks will be welcomed!

# Packages

No description provided by the author

# Functions

No description provided by the author
Parse ...
No description provided by the author

# Constants

Return a descriptive error.
Call os.Exit(2) or for -h/-help Exit(0).
Call panic with a descriptive error.

# Variables

No description provided by the author

# Structs

No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author