- v0.31.0: Breaking Changes
- v0.30.0: New Features
- v0.29.0: New Features
- v0.28.0: Extend ZSHELL support
- v0.27.0: Breaking changes
- v0.26.0: New ErrorParsing and minor fixes
- v0.25.3: Minor Fixes
- v0.25.2: Minor Fixes
- v0.25.1: Minor Fixes
- v0.25.0: Major Refactor
- v0.24.0: New Features
- v0.23.0: Feature Updates
- v0.22.0: Breaking Change
- v0.21.0: Breaking Change
- v0.20.2: Feature Update
- v0.20.1: Feature Update
- v0.20.0: Breaking Change
- v0.19.0: Feature update
- v0.18.0: Feature release
- v0.17.0: Breaking Changes
- v0.16.0 Feature release
- v0.15.0 Feature release
- v0.14.1 Golang Bug fix: module install error
- v0.14.0 Breaking Changes
- v0.13.0 Feature release
- v0.12.0 Breaking Changes
- v0.11.0 Breaking Changes
As the releases before, this release has 100% test coverage. Tested with Go 1.16, 1.17, 1.18, 1.19, 1.20, 1.21 and 1.22.
-
Add
opt.SetValue
to allow setting the value of an option. -
Add
opt.SuggestedValues
ModifyFn to allow setting autocompletion suggestions for an option.Works just like the existing
opt.ValidValues
but it doesn’t error out if the value is not in the list of suggestions. -
Add
opt.GetRequiredArg
,opt.GetRequiredArgInt
andopt.GetRequiredArgFloat64
to simplify handling required arguments and providing error messages.For example:
opt := getoptions.New() opt.SetCommandFn(Run) opt.HelpSynopsisArg("<arg1>", "arg1 desc") opt.HelpSynopsisArg("<arg2>", "arg2 desc") ... func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error { i, args, err := opt.GetRequiredArgInt(args) if err != nil { return err } ... return nil }
If the argument is not provided, the error message will be:
ERROR: Missing required argument: <arg1>
If the argument is provided but it is not an integer, the error message will be:
ERROR: Argument error: Can't convert string to int: 'x'
As the releases before, this release has 100% test coverage. Tested with Go 1.16, 1.17, 1.18, 1.19, 1.20 and 1.21.
-
Add
opt.SetCalled
ModifyFn to allow setting an option as called.Useful when calling
CommandFn
directly and not throughopt.Dispatch
and without a call toopt.Parse
. It allows building aopt
object using defaults and marking the options as called.For example:
func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error { password := opt.Value("password").(string) nopt := getoptions.New() nopt.String("password", password, opt.SetCalled(opt.Called("password"))) nopt.Int("number", 123, opt.SetCalled(true)) nopt.Float64("float", 3.14) // opt.Called("float") is false but its value is set to 3.14 err := CommandFn(ctx, nopt, []string{})
As the releases before, this release has 100% test coverage. Tested with Go 1.16, 1.17, 1.18, 1.19, 1.20 and 1.21.
-
Extend support for ZSHELL when setting
ZSHELL=true
in your environment.ZSHELL completion works by using
bashcompinit
using:autoload bashcompinit bashcompinit complete -o default -C <tool> <tool>
Before this release, completions would stop after the
=
symbol because the completion system is targeting bash by default and bash handles=
as a divider for completions. By settingZSHELL=true
in your environment, the completion system will target zsh and not split completions on=
.NoteI couldn’t find a non-explicit reliable way to auto-detect zsh.
As the releases before, this release has 100% test coverage. Tested with Go 1.14, 1.15, 1.16, 1.17, 1.18, 1.19 and 1.20.
Refactor and rename HelpSynopsisArgs
-
Rename and allow description:
- opt.HelpSynopsisArgs("name")
+ opt.HelpSynopsisArg("name", "description")
-
Allow it to be called multiple times.
-
Add ARGUMENTS section in help.
As the releases before, this release has 100% test coverage. Tested with Go 1.14, 1.15, 1.16, 1.17, 1.18 and 1.19.
-
Expose ErrorParsing as a generic error
The ErrorParsing error indicates there was a problem parsing the cli args.
This can be used for example, to print the help only in cases where the user didn’t enter valid cli args.
-
Refactor dag package tests to remove most sleeps.
-
Bug fix: Don’t check for required options when the help option is passed
As the releases before, this release has 100% test coverage. Tested with Go 1.14, 1.15, 1.16 and Go 1.17.
As the releases before, this release has 100% test coverage. Tested with Go 1.14, 1.15, 1.16 and Go 1.17.
-
When
HelpCommand
is declared, inject ahelp
subcommand to all commands, not just the ones that have children. This really simplifies discoverabilty for new users.
As the releases before, this release has 100% test coverage. Tested with Go 1.14, 1.15, 1.16 and Go 1.17.
This refactor brings major benefits in both parsing and autocompletion. In the initial implementation, completion was added as side logic to the existing parser code. In this implementation, completions are a first class citizen and share the same parsing tree structure that the rest of the library is using.
The parsing tree was refactored from the ground up to better accommodate commands and subcommands and also the extra use cases that have been popping up ever since autocompletion support was added.
The major user facing change is that instead of providing building blocks to build a command and subcommand experience, a single opt.Parse
and opt.Dispatch
call is required to handle options for commands and subcommands at all levels.
-
The HelpCommand signature has changed. The name of the "help" command is configurable. Additionally, when defining
opt.HelpCommand
there is no need to define a help option as it also declares one.- opt.Bool("help", false, opt.Alias("?")) - opt.HelpCommand("") + opt.HelpCommand("help", opt.Alias("?"))
-
The Dispatch signature has changed. There is no need to define the name of the help command at this level anymore since it has been moved to the
HelpCommand
declaration.- err = opt.Dispatch(ctx, "help", remaining) + err = opt.Dispatch(ctx, remaining)
-
Move
InterruptContext
into a package level function and not a method of GetOpt.- ctx, cancel, done := opt.InterruptContext() + ctx, cancel, done := getoptions.InterruptContext()
-
Write
io.Writer used to write warnings and errors (which defaults to os.Stderr) has been made into a package level variable and not a method of GetOpt. -
CommandFn
is no longer an exported field ofGetOpt
. If this was ever used, now the canonical way to execute a command function is throughopt.Dispatch
. -
Remove
opt.Option
, this was used in test code to return the internal representation of an option and shouldn’t be accessed directly by an end user. -
Remove
opt.Stringer
, this was used to print a text representation of the parsed structure but other than in test code there is little value for it. -
Moved exported packages that this library uses into the
internal
directory so they can’t be imported by other projects by mistake. -
Change
opt.CustomCompletion
signature:- func (gopt *GetOpt) CustomCompletion(list []string) *GetOpt + func (gopt *GetOpt) CustomCompletion(list ...string) *GetOpt
-
Autocompletion is super useful now.
-
New setting:
opt.UnsetOptions
Since options are automatically inherited to commands and subcommands, in cases where you want to override that inheritance and delete the inherited options use this. This is useful for wrapper commands.
-
When a command doesn’t have a defined command fn but that command has children, a help landing page is displayed automatically.
As the releases before, this release has 100% test coverage. Tested with Go 1.14, 1.15, 1.16 and Go 1.17.
-
Add
SetMaxParallel
method to DAG graph to limit concurrency. -
Add
SetOutputBuffer
method to DAG graph to allow buffering task output in memory and printing it at the end of the task execution for easier debugging. -
Enable completion results after options that require arguments.
As the releases before, this release has 100% test coverage. Tested with Go 1.14 and Go 1.15.
-
Introduce
Float64Optional
andFloat64VarOptional
to have complete method parity for String, Int and Float64 types. -
Support multi-line command descriptions.
-
Add
GetEnv
support for missing single option types:-
Int, IntVar, IntOptional, IntVarOptional
-
StringOptional, StringVarOptional
-
Float64, Float64Var, Float64Optional, Float64VarOptional
-
As the releases before, this release has 100% test coverage. Tested with Go 1.14 and Go 1.15.
Fix completion issues where a completion that works when starting to complete from scratch fails when some args are deleted.
Fixed by changing the exit status when generating completions from 1 to 124. Exit 124 means programmable completion restarts from the beginning, with an attempt to find a new compspec for that command.
Removing negatable flags NBool
and NBoolVar
.
A feature that adds a bunch of complexity for very little value and prevents reading environment variables into booleans.
-
opt.GetEnv
Is now supported when usingopt.Bool
andopt.BoolVar
. Previously onlyopt.String
andopt.StringVar
were supported.When using
opt.GetEnv
withopt.Bool
oropt.BoolVar
, only the words "true" or "false" are valid. They can be provided in any casing, for example: "true", "True" or "TRUE". -
opt.Dispatch
now automatically handles the help flag. The help flag needs to be defined at the top level. When the help flag is called and handled by a commandopt.Dispatch
now returns an error of typegetoptions.ErrorHelpCalled
.For example:
func main() { os.Exit(program()) } func program() int { opt := getoptions.New() opt.Bool("help", false, opt.Alias("?")) // Define the help flag as "--help" with alias "-?" list := opt.NewCommand("list", "list stuff").SetCommandFn(listRun) list.Bool("list-opt", false) opt.HelpCommand("") remaining, err := opt.Parse(os.Args[1:]) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) os.Exit(1) } ctx, cancel, done := opt.InterruptContext() defer func() { cancel(); <-done }() err = opt.Dispatch(ctx, "help", remaining) // Use the same help flag "help". if err != nil { if errors.Is(err, getoptions.ErrorHelpCalled) { return 1 } fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) return 1 } return 0 }
Now, calling
program list --help
orprogram list -?
prints the help for thelist
command as well as callingprogram help list
.
As the releases before, this release has 100% test coverage.
Dropping support for Go 1.10, 1.11, 1.12 and 1.13 to leverage new errors and testing features.
In particular The errors.Is
and errors.As
features greatly simplify error testing and handling and are used in the new DAG build system.
Introduces a new Directed Acyclic Graph Build System.
The build system is a separate import package: import "github.com/DavidGamba/go-getoptions/dag"
Documentation can be found in its own README.
As the releases before, this release has 100% test coverage.
Support for Go 1.10, 1.11 and 1.12 will be dropped in a future release.
The errors.Is
and errors.As
features greatly simplify error testing and handling and will likely be introduced in the near future.
-
Pass autocompletion entries to children.
From v0.20.0 all options starting being passed to children commands. Their completion entries were missing.
-
Separate internal option completion between flags that don’t expect and argument and options that do. When an option that expects an argument is found, the given argument won’t break the completion chain. Only one argument is supported per option.
-
Don’t break autocompletion chain when there is an option in the chain that accepts an argument with
=
. For example:program --profile=dev <tab><tab>
will show completions for program.
As the releases before, this release has 100% test coverage.
-
Improve autocompletion behaviour.
Break words in COMP_LINE by matching against multiple spaces
\s+
instead of a single one.
As the releases before, this release has 100% test coverage.
-
Deprecate
opt.SetOption
Since the introduction of
opt.NewCommand(name, description string)
there is a proper parent child relationship between commands. There is no need to hack passing desired options to the child command, instead, now all options are automatically propagated to the child.This has the side benefit to make the automated help clearer by listing all options that previously where only listed in one of the parent levels.
To update, remove calls to
opt.SetOption
, for example:opt := getoptions.New() opt.Bool("help", false, opt.Alias("?")) opt.Bool("debug", false) opt.SetRequireOrder() opt.SetUnknownMode(getoptions.Pass) list := opt.NewCommand("list", "list stuff") - list.SetOption(opt.Option("help"), opt.Option("debug")).SetCommandFn(listRun) + list.SetCommandFn(listRun) list.Bool("list-opt", false) opt.HelpCommand("") remaining, err := opt.Parse([]string{"list"})
-
Automatically run
opt.Parse
when callingopt.Dispatch
.When defining a new command, we define the function that the command will run with
command.SetCommandFn(commandFunction)
. If the command is passed in the command line,opt.Dispatch
calls the command function. Previously,opt.Dispatch
wasn’t automatically callingopt.Parse
in the command function so the first thing that every command function had to do was a call to parse.For example:
func main() { opt := getoptions.New() list := opt.NewCommand("list", "list stuff") list.SetCommandFn(listRun) opt.HelpCommand("") remaining, err := opt.Parse(os.Args[1:]) if err != nil { ... } err = opt.Dispatch(context.Background(), "help", remaining) if err != nil { ... } } func listRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error { remaining, err := opt.Parse(args) if err != nil { ... } // Function code here }
Now, the call
opt.Parse
is automated byopt.Dispatch
so the command function is simplified to:func listRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error { // Function code here }
Where the received
opt
has the arguments already parsed and the receivedargs
is the remaining arguments that didn’t match any option.
As the releases before, this release has 100% test coverage.
-
opt.GetEnv
now satisfiesopt.Required
:When an environment variable that matches the variable from
opt.GetEnv
is set,opt.GetEnv
will setopt.Called
to true and will setopt.CalledAs
to the name of the environment variable used. In other words, when an option is required,opt.Required
is set,opt.GetEnv
satisfies that requirement. -
opt.GetEnv
environment variable now shows in help output.Example:
REQUIRED PARAMETERS: --access-key-id <string> AWS Access Key ID. (env: AWS_ACCESS_KEY_ID) --role-arn <string> Role ARN. (env: AWS_ROLE_ARN) --secret-access-key <string> AWS Secret Access Key. (env: AWS_SECRET_ACCESS_KEY) OPTIONS: --region <string> Default Region. (default: "us-west-2", env: AWS_DEFAULT_REGION)
As the releases before, this release has 100% test coverage.
This release adds initial support for Environment Variables and adds lots of GoDoc examples.
-
Initial support for environment variables has been added.
Currently, only
opt.String
andopt.StringVar
are supported.To use it, set the option modify function to opt.GetEnv. For example:
var profile string opt.StringVar(&profile, "profile", "default", opt.GetEnv("AWS_PROFILE"))
Or:
profile := opt.String("profile", "default", opt.GetEnv("AWS_PROFILE"))
NoteNon supported option types behave with a No-Op when opt.GetEnv
is defined.
-
Change opt.Dispatch signature to clarify the actual use of the variable. Additionally, actually use the variable, before it was hardcoded to "help".
-func (gopt *GetOpt) Dispatch(ctx context.Context, helpOptionName string, args []string) error +func (gopt *GetOpt) Dispatch(ctx context.Context, helpCommandName string, args []string) error
As the releases before, this release has 100% test coverage.
This release keeps on the work of removing the kinks around subcommands. An example showing subcommands can be found in ./examples/mygit.
It also introduces the use of context to propagate cancelation signals, etc. to the child commands.
Finally, it introduces a new helper that captures interrupts (for example Ctrl-C) and returns a top level context.
-
Refactor
NewCommmand
as a method. This will allow the built-in help to have information about the parent. It might also help with autocompletion. -
Change sigature to
opt.NewCommand(name, description string)
. It takes a name and description now. -
Change signature of
CommandFn
to have acontext
as the first argument. It will allow the parent to propagate cancelation signals, etc. to the child commands. This change goes along a change to the helperopt.Dispatch
to also have acontext
as the first argument.
Updating:
- list := getoptions.NewCommand().Self("list", "list instances").
+ list := opt.NewCommand("list", "list instances").
SetOption(parent.Option("help"), parent.Option("debug")).
SetCommandFn(runInstanceList)
list.StringSlice("tag", 1, 99, opt.Alias("t"),
opt.Description("Any AWS tags you want to list"))
- opt.Command(list)
...
- err = opt.Dispatch("help", remaining)
+ err = opt.Dispatch(context.Background(), "help", remaining)
...
-func runInstanceList(opt *getoptions.GetOpt, args []string) error {
+func runInstanceList(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
-
Introduce
opt.InterruptContext()
, a helper that returns a top level context that captures interrupt signals (os.Interrupt
,syscall.SIGHUP
,syscall.SIGTERM
). An example can be found in ./examples/mygit.
As the releases before, this release has 100% test coverage.
-
Bug Fix: Update
opt.Dispatch
not to handle--help
option. It was preventing the help option to reach the commands.
-
Introduce
opt.HelpSynopsisArgs(string)
method to allow overriding the default args description. The current default description is[<args>]
.
As the releases before, this release has 100% test coverage.
Change workflow to deal with ambiguities between parent and child.
For example, the root may have option --profile
and the command the option --password
with alias -p
. If -p
is passed, the parent would uncorrectly be matched.
For the parent to properly handle ambiguities with its children, it needs to have knowledge of them. A new getoptions.NewCommand
has been introduced.
To help with the verbosity of handling all the commands, a new Dispatch
method is introduced, it will call a command’s function defined with the new SetCommandFn
method.
-
Introduce
getoptions.NewCommand()
to declare commands and clearly separate their role from the maingetoptions.New()
. -
Introduce
command.SetCommandFn(fn CommandFn)
to declare a commands function callback. -
Introduce
opt.Dispatch(helpOptionName string, args []string)
to automatically handle dispatching to theCommandFn
based on the cli input. -
Make options unambiguous with commands. For example, the root may have option
--profile
and the command the option--password
with alias-p
. If-p
is passed, the parent would uncorrectly be matched. -
Introduce new error indicating which options are getting matched with ambiguous options.
-
Add
getoptions.HelpCommand()
to have an automated help command. It adds completions for all other commands automatically.
Bypass double dot golang modules error: golang/go#27299
As the releases before, this release has 100% test coverage.
This release introduces bash completion by default and works out many kinks around subcommands. An example showing subcommands can be found in ./examples/mygit.
-
Remove support for Go < v1.10 (v1.5 - v1.9).
-
Rename
getoptions.Option
togetoptions.Value
.WarningA new getoptions.Option
method is has been introduced, but the new one returns*option.Option
instead. -
Change the argument of
opt.SetMode
andopt.SetUnknownMode
from a string to agetoptions.Mode
andgetoptions.UnknownMode
type. Makes it easier to autodiscover valid arguments for the method. -
Refactor section help methods into the main
opt.Help
one.- opt.HelpName() + opt.Help(getoptions.HelpName) - opt.HelpSynopsis() + opt.Help(getoptions.HelpSynopsis) - opt.HelpCommandList() + opt.Help(getoptions.HelpCommandList) - opt.HelpOptionList() + opt.Help(getoptions.HelpOptionList)
To print all the sections of the automated help, continue to use
opt.Help()
.
-
Implement bash completion by default.
Add the following to your
.bashrc
:
complete -o default -C "/binary/location/myscript" myscript
-
New
getoptions.Option
method that returns*option.Option
. In combination with the newgetoptions.SetOption
it allows to pass options from parent to subcommand. -
Add
getoptions.CustomCompletion
method. Given a list, it will add the elements of the list to the completion alternatives. -
Add
getoptions.StringMapVar
method.
As the releases before, this release has 100% test coverage.
-
Experimental implementation of help messages.
-
Show used alias in errors for single options (not slice or maps).
-
Add opt.CalledAs method to know how the option was called.
As the releases before, this release has 100% test coverage.
Change all function signatures from:
XVar(p *bool, name string, def bool, aliases ...string)
To:
XVar(p *bool, name string, def bool, fns ...ModifyFn)
This change allows to pass different functions to the option that will modify single option behaviour and will allow for multiple features without future breaking changes in the function signature.
As part as this change, a new function, opt.Alias
is added to support
previous functionality.
To update, change the aliases from a list of aliases as the variadic
last argument to a list of aliases passed to the opt.Alias
function.
For example:
- opt.BoolVar(&flag, "flag", false, "f", "alias2")
+ opt.BoolVar(&flag, "flag", false, opt.Alias("f", "alias2"))
As the releases before, this release has 100% test coverage.
-
StringSlice
is redundant withStringSliceMulti
.
Calling:
StringSlice(name, aliases…)
Is the same as Calling:
StringSliceMulti(name, 1, 1, aliases…)
Consolidate API to:
StringSlice(name, min, max, aliases…)
-
StringMap
is redundant withStringMapMulti
.
Calling:
StringMap(name, aliases…)
Is the same as Calling:
StringMapMulti(name, 1, 1, aliases…)
Consolidate API to:
StringMap(name, min, max, aliases…)
-
Rename
IntSliceMulti
toIntSlice
.