File: args.go

package info (click to toggle)
golang-github-juju-cmd 3.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: makefile: 7
file content (59 lines) | stat: -rw-r--r-- 1,698 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.

package cmd

import (
	"strings"

	"github.com/juju/gnuflag"
)

// StringsValue implements gnuflag.Value for a comma separated list of
// strings.  This allows flags to be created where the target is []string, and
// the caller is after comma separated values.
type StringsValue []string

var _ gnuflag.Value = (*StringsValue)(nil)

// NewStringsValue is used to create the type passed into the gnuflag.FlagSet Var function.
// f.Var(cmd.NewStringsValue(defaultValue, &someMember), "name", "help")
func NewStringsValue(defaultValue []string, target *[]string) *StringsValue {
	value := (*StringsValue)(target)
	*value = defaultValue
	return value
}

// Implements gnuflag.Value Set.
func (v *StringsValue) Set(s string) error {
	*v = strings.Split(s, ",")
	return nil
}

// Implements gnuflag.Value String.
func (v *StringsValue) String() string {
	return strings.Join(*v, ",")
}

// AppendStringsValue implements gnuflag.Value for a value that can be set
// multiple times, and it appends each value to the slice.
type AppendStringsValue []string

var _ gnuflag.Value = (*AppendStringsValue)(nil)

// NewAppendStringsValue is used to create the type passed into the gnuflag.FlagSet Var function.
// f.Var(cmd.NewAppendStringsValue(&someMember), "name", "help")
func NewAppendStringsValue(target *[]string) *AppendStringsValue {
	return (*AppendStringsValue)(target)
}

// Implements gnuflag.Value Set.
func (v *AppendStringsValue) Set(s string) error {
	*v = append(*v, s)
	return nil
}

// Implements gnuflag.Value String.
func (v *AppendStringsValue) String() string {
	return strings.Join(*v, ",")
}