123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package mtool
- // The package implements fast way
- // to make multitool CLI applications.
- import (
- "flag"
- "os"
- "time"
- )
- type Flags struct {
- *flag.FlagSet
- tool *Tool
- args []string
- parsedArgs []string
- customArgs []any
- envNameMap map[string][]string
- envMap map[string]string
- }
- type ToolMap map[string]*Tool
- func (flags *Flags) CustomArgs() []any {
- return flags.customArgs
- }
- func (flags *Flags) wasPassed(name string) bool {
- found := false
- flags.Visit(func(f *flag.Flag) {
- if f.Name == name {
- found = true
- }
- })
- return found
- }
- func (flags *Flags) setEnv(
- name string,
- env []string,
- ) bool {
- flags.envNameMap[name] = env
- for _, k := range env {
- value, has := os.LookupEnv(k)
- if !has {
- continue
- }
- flags.envMap[name] = value
- return true
- }
- return false
- }
- // Set new string variable
- // to parse.
- func (flags *Flags) StringVar(
- p *string,
- name string,
- value string,
- usage string,
- env ...string,
- ) {
- flags.FlagSet.StringVar(p, name, value, usage)
- flags.setEnv(name, env)
- }
- // Set new int variable to parse.
- func (flags *Flags) IntVar(
- p *int,
- name string,
- value int,
- usage string,
- env ...string,
- ) {
- flags.FlagSet.IntVar(p, name, value, usage)
- flags.setEnv(name, env)
- }
- // Set new int64 variable to parse.
- func (flags *Flags) Int64Var(
- p *int64,
- name string,
- value int64,
- usage string,
- env ...string,
- ) {
- flags.FlagSet.Int64Var(p, name, value, usage)
- flags.setEnv(name, env)
- }
- // Set new bool variable to parse.
- func (flags *Flags) BoolVar(
- p *bool,
- name string,
- value bool,
- usage string,
- env ...string,
- ) {
- flags.FlagSet.BoolVar(p, name, value, usage)
- flags.setEnv(name, env)
- }
- // Set new float64 variable to parse.
- func (flags *Flags) Float64Var(
- p *float64,
- name string,
- value float64,
- usage string,
- env ...string,
- ) {
- flags.FlagSet.Float64Var(p, name, value, usage)
- flags.setEnv(name, env)
- }
- // Set new duration variable to parse.
- func (flags *Flags) DurationVar(
- p *time.Duration,
- name string,
- value time.Duration,
- usage string,
- env ...string,
- ) {
- flags.FlagSet.DurationVar(p, name, value, usage)
- flags.setEnv(name, env)
- }
- func (flags *Flags) Parse() []string {
- flags.FlagSet.Parse(flags.args)
- for name, v := range flags.envMap {
- if !flags.wasPassed(name) {
- flags.FlagSet.Set(name, v)
- }
- }
- flags.parsedArgs = flags.FlagSet.Args()
- return flags.parsedArgs
- }
- // Get all the arguments, including the
- // parsed ones.
- func (flags *Flags) AllArgs() []string {
- return flags.args
- }
- // Get all the arguments going after options.
- func (flags *Flags) Args() []string {
- return flags.parsedArgs
- }
- // Get the tool currently called.
- func (flags *Flags) Tool() *Tool {
- return flags.tool
- }
|