Skip to content

add support for custom flag types via flag.Value #213

Description

@mvdan

A number of flag types are supported:

go-ipfs-cmds/option.go

Lines 178 to 198 in 4ade007

func BoolOption(names ...string) Option {
return NewOption(Bool, names...)
}
func IntOption(names ...string) Option {
return NewOption(Int, names...)
}
func UintOption(names ...string) Option {
return NewOption(Uint, names...)
}
func Int64Option(names ...string) Option {
return NewOption(Int64, names...)
}
func Uint64Option(names ...string) Option {
return NewOption(Uint64, names...)
}
func FloatOption(names ...string) Option {
return NewOption(Float, names...)
}
func StringOption(names ...string) Option {
return NewOption(String, names...)
}

This mostly mirrors what https://pkg.go.dev/flag supports, with one notable exception: flag.Value via https://pkg.go.dev/flag#Var.

This is very useful for custom types which know how to be used as flags. For example: https://pkg.go.dev/github.com/multiformats/go-multicodec@master#Code.Set

Right now, if I want a go-ipfs tool to have a multicodec.Code flag, I have to use StringOption, and then separately write about 4 lines of extra code to translate that via a Code.Set call. It would be much easier to be able to use it directly, via a go-ipfs-cmds API like VarOption or FlagValueOption.

Note that this does not require using the flag package for parsing arguments or flags. flag.Value is an interface, so it should be compatible with the current logic: https://pkg.go.dev/flag#Value

cc @willscott @masih

Metadata

Metadata

Assignees

No one assigned

    Labels

    need/triageNeeds initial labeling and prioritization

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions