All Projects → cosiner → Flag

cosiner / Flag

Licence: mit
Flag is a simple but powerful command line option parsing library for Go support infinite level subcommand

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Flag

Clipp
easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation
Stars: ✭ 687 (+502.63%)
Mutual labels:  commandline, option-parser
Appicon
AppIcon generates *.appiconset contains each resolution image for iOS
Stars: ✭ 1,454 (+1175.44%)
Mutual labels:  commandline
Autopair.fish
Auto-complete matching pairs in the Fish command line.
Stars: ✭ 63 (-44.74%)
Mutual labels:  commandline
Catcli
The command line catalog tool for your offline data
Stars: ✭ 87 (-23.68%)
Mutual labels:  commandline
Slackcat
CLI utility to post files and command output to slack
Stars: ✭ 1,127 (+888.6%)
Mutual labels:  commandline
Rsfetch
Fast (~1ms execution time) and somewhat(?) minimal fetch program written in Rust.
Stars: ✭ 103 (-9.65%)
Mutual labels:  commandline
Ntutils
Various Command Line Utilities Ported to Windows NT
Stars: ✭ 58 (-49.12%)
Mutual labels:  commandline
Programoptions.hxx
Single-header program options parsing library for C++11
Stars: ✭ 112 (-1.75%)
Mutual labels:  option-parser
Clikt
Multiplatform command line interface parsing for Kotlin
Stars: ✭ 1,658 (+1354.39%)
Mutual labels:  option-parser
Jaromail
A commandline tool to easily and privately handle your e-mail
Stars: ✭ 86 (-24.56%)
Mutual labels:  commandline
Fileinfo
📄Get information on over 10,000 file extensions right from the terminal
Stars: ✭ 86 (-24.56%)
Mutual labels:  commandline
Pgen
Command-line passphrase generator
Stars: ✭ 68 (-40.35%)
Mutual labels:  commandline
Cordless
The Discord terminal client you never knew you wanted.
Stars: ✭ 1,391 (+1120.18%)
Mutual labels:  commandline
Gifline
The fast way to put gif in or emails
Stars: ✭ 63 (-44.74%)
Mutual labels:  commandline
Console Logging
Better, prettier commandline logging for Python--with colors! 👻
Stars: ✭ 111 (-2.63%)
Mutual labels:  commandline
Commandline
A pure Swift library for creating command-line interfaces
Stars: ✭ 1,099 (+864.04%)
Mutual labels:  commandline
Minimal git command
🌈 This is a repository that summarizes the Git commands and tips that you should at least know about development.
Stars: ✭ 78 (-31.58%)
Mutual labels:  commandline
Lc
licensechecker (lc) a command line application which scans directories and identifies what software license things are under producing reports as either SPDX, CSV, JSON, XLSX or CLI Tabular output. Dual-licensed under MIT or the UNLICENSE.
Stars: ✭ 93 (-18.42%)
Mutual labels:  commandline
Macos Patcher
Command line tool for running macOS on unsupported Macs
Stars: ✭ 114 (+0%)
Mutual labels:  commandline
Manage
Command Line Manager + Interactive Shell for Python Projects
Stars: ✭ 111 (-2.63%)
Mutual labels:  commandline

Flag

GoDoc

Flag is a simple but powerful commandline flag parsing library for Go.

Documentation

Documentation can be found at Godoc

Supported features

  • bool
    • -f, -f=false, -f=true, there is no -f true and -f false to avoid conflicting with positional flag and non-flag values
  • string,number
    • -f a.go -n 100
  • slice:
    • -f a.go -f b.go -f c.go
  • hint flag as value
    • -- to hint next argument is value: rm -- -a.go, rm -- -a.go -b.go will throws error for -b.go is invalid flag
    • --* to hint latter all arguments are value: rm -- -a.go -b.go -c.go
  • useful tricks:
    • -f a.go, -f=a.go, --file=a.go
    • -zcf=a.go, -zcf a.go
    • -I/usr/include: only works for -[a-zA-Z][^a-zA-Z].+
  • catch non-flag arguments:
    • rm -rf a.go b.go c.go, catchs [a.go, b.go, c.go]
  • positional flag:
    • cp -f src.go dst.go, catchs SOURCE=a.go DESTINATION=dst.go
    • This is implemented as a special case of non-flag arguments, positional flags will be applied first, and remain values
  • value apply/checking
    • default value
    • environment value
    • value list for user selecting
  • multiple flag names for one flag
  • subcommand.

Definition via structure field tag

  • names: flag/command names, comma-speparated, default uses camelCase of field name(with a - prefix for flag)

    • - to skip this field
    • @ to indicate that this is a positional flag
    • support multiple name and formats: eg: `-f, --file, -file'
  • arglist: argument list for command or argument name for flag

    • for positional flag, this will also be used as it's display name if defined, otherwise field name is used.
    • command example: eg: [OPTION]... SOURCE DESTINATION, or [FLAG]... FILE [ARG}...
    • flag example: eg: 'FILE', 'DESTINATION'
  • version: version message for command

  • usage: short description

  • desc: long description

  • env: environment name for flag, if user doesn't passed this flag, environment value will be used

  • default: default value for flag, if user doesn't passed this flag and environment value not defined, it will be used

  • args: used to catching non-flag arguments, it's type must be []string

  • special cases

    • Enable, there must be a Enable field inside command to indicate whether user are using this command.
    • Args: this field will be used to store non-flag arguments if args tag is not defined
    • Metadata: structure could implement this interface to override settings defined by tags
      type Metadata interface {
          // the key is comma-separated flag name to find command/flag,
          // each component represents a level, "" represents root command
          Metadata() map[string]Flag
      }
    

Example

Flags

package flag

import "fmt"

type Tar struct {
	GZ          bool     `names:"-z, --gz" usage:"gzip format"`
	BZ          bool     `names:"-j, --bz" usage:"bzip2 format"`
	XZ          bool     `names:"-J, --xz" usage:"xz format"`
	Create      bool     `names:"-c" usage:"create tar file"`
	Extract     bool     `names:"-x" usage:"extract tar file"`
	File        string   `names:"-f" usage:"output file for create or input file for extract"`
	Directory   string   `names:"-C" usage:"extract directory"`
	SourceFiles []string `args:"true"`
}

func (t *Tar) Metadata() map[string]Flag {
	const (
		usage   = "tar is a tool for manipulate tape archives."
		version = `
			version: v1.0.0
			commit: 10adf10dc10
			date:   2017-01-01 10:00:01
		`
		desc = `
		tar creates and manipulates streaming archive files.  This implementation can extract
		from tar, pax, cpio, zip, jar, ar, and ISO 9660 cdrom images and can create tar, pax,
		cpio, ar, and shar archives.
		`
	)
	return map[string]Flag{
		"": {
			Usage:   usage,
			Version: version,
			Desc:    desc,
		},
		"--gz": {
			Desc: "use gzip format",
		},
	}
}

func ExampleFlagSet_ParseStruct() {
	var tar Tar

	NewFlagSet(Flag{}).ParseStruct(&tar, "tar", "-zcf", "a.tgz", "a.go", "b.go")
	fmt.Println(tar.GZ)
	fmt.Println(tar.Create)
	fmt.Println(tar.File)
	fmt.Println(tar.SourceFiles)

	// Output:
	// true
	// true
	// a.tgz
	// [a.go b.go]
}

Help message

tar is a tool for manipulate tape archives.

Usage:
      flag.test [FLAG]...

Version:
      version: v1.0.0
      commit: 10adf10dc10
      date:   2017-01-01 10:00:01

Description:
      tar creates and manipulates streaming archive files.  This implementation can extract
      from tar, pax, cpio, zip, jar, ar, and ISO 9660 cdrom images and can create tar, pax,
      cpio, ar, and shar archives.

Flags:
      -z, --gz     gzip format (bool)
            use gzip format
      -j, --bz     bzip2 format (bool)
      -J, --xz     xz format (bool)
      -c           create tar file (bool)
      -x           extract tar file (bool)
      -f           output file for create or input file for extract (string)
      -C           extract directory (string)

FlagSet


type GoCmd struct {
	Build struct {
		Enable  bool
		Already bool   `names:"-a" important:"1" desc:"force rebuilding of packages that are already up-to-date."`
		Race    bool   `important:"1" desc:"enable data race detection.\nSupported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64."`
		Output  string `names:"-o" arglist:"output" important:"1" desc:"only allowed when compiling a single package"`

		LdFlags  string   `names:"-ldflags" arglist:"'flag list'" desc:"arguments to pass on each go tool link invocation."`
		Packages []string `args:"true"`
	} `usage:"compile packages and dependencies"`
	Clean struct {
		Enable bool
	} `usage:"remove object files"`
	Doc struct {
		Enable bool
	} `usage:"show documentation for package or symbol"`
	Env struct {
		Enable bool
	} `usage:"print Go environment information"`
	Bug struct {
		Enable bool
	} `usage:"start a bug report"`
	Fix struct {
		Enable bool
	} `usage:"run go tool fix on packages"`
	Fmt struct {
		Enable bool
	} `usage:"run gofmt on package sources"`
}

func (*GoCmd) Metadata() map[string]Flag {
	return map[string]Flag{
		"": {
			Usage:   "Go is a tool for managing Go source code.",
			Arglist: "command [argument]",
		},
		"build": {
			Arglist: "[-o output] [-i] [build flags] [packages]",
			Desc: `
		Build compiles the packages named by the import paths,
		along with their dependencies, but it does not install the results.
		...
		The build flags are shared by the build, clean, get, install, list, run,
		and test commands:
			`,
		},
	}
}

func TestSubset(t *testing.T) {
	var g GoCmd

	set := NewFlagSet(Flag{})
	set.StructFlags(&g)
	set.Help(false)
	fmt.Println()
	build, _ := set.FindSubset("build")
	build.Help(false)
}

##Help Message

Go is a tool for managing Go source code.

Usage:
      flag.test command [argument]

Sets:
      build        compile packages and dependencies
      clean        remove object files
      doc          show documentation for package or symbol
      env          print Go environment information
      bug          start a bug report
      fix          run go tool fix on packages
      fmt          run gofmt on package sources
compile packages and dependencies

Usage:
      build [-o output] [-i] [build flags] [packages]

Description:
      Build compiles the packages named by the import paths,
      along with their dependencies, but it does not install the results.
      ...
      The build flags are shared by the build, clean, get, install, list, run,
      and test commands:

Flags:
      -a                  
            force rebuilding of packages that are already up-to-date.
      -race               
            enable data race detection.
            Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.
      -o output           
            only allowed when compiling a single package

      -ldflags 'flag list'
            arguments to pass on each go tool link invocation.

LICENSE

MIT.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].