-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
98 lines (83 loc) · 2.8 KB
/
cmd.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2023-2024 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.
package cliargs
import (
"fmt"
"os"
"path"
)
// Cmd is the structure that parses command line arguments and stores them.
// The results of parsing are stored by separating into command name, command arguments, options,
// and option arguments.
// And this provides methods to check if they are specified and to retrieve them.
type Cmd struct {
Name string
Args []string
OptCfgs []OptCfg
opts map[string][]string
isAfterEndOpt bool
_args []string
}
// NewCmd is the function that creates a Cmd instance iwth command line arguments obtained from
// os.Args.
func NewCmd() Cmd {
var name string
if len(os.Args) > 0 {
name = path.Base(os.Args[0])
}
var args []string
if len(os.Args) > 1 {
args = os.Args[1:]
}
return Cmd{Name: name, Args: []string{}, opts: make(map[string][]string), _args: args}
}
func (cmd Cmd) subCmd(fromIndex int, isAfterEndOpt bool) Cmd {
var name string
if len(cmd._args) > fromIndex {
name = cmd._args[fromIndex]
}
var args []string
if len(cmd._args) > fromIndex+1 {
args = cmd._args[fromIndex+1:]
}
return Cmd{
Name: name,
Args: []string{},
opts: make(map[string][]string),
isAfterEndOpt: isAfterEndOpt,
_args: args,
}
}
// HasOpt is the method that checks whether an option with the specified name exists.
func (cmd Cmd) HasOpt(name string) bool {
_, exists := cmd.opts[name]
return exists
}
// OptArg is the method that returns the option argument with the specified name.
// If the option has multiple arguments, this method returns the first argument.
// If the option is a boolean flag, the method returns an empty string.
// If the option is not specified in the command line arguments, the return value
// of this method is an empty string.
func (cmd Cmd) OptArg(name string) string {
arr := cmd.opts[name]
// If no entry, map returns a nil slice.
// len() methods of both a nil slice and a empty slice return zero in common.
if len(arr) == 0 {
return ""
} else {
return arr[0]
}
}
// OptArgs is the method that returns the option arguments with the specified name.
// If the option has one or multiple arguments, this method returns an array of the arguments.
// If the option is a boolean flag, the method returns an empty slice.
// If the option is not specified in the command line arguments, the return value
// of this method is a nil slice.
func (cmd Cmd) OptArgs(name string) []string {
return cmd.opts[name]
}
// String is the method that returns the string which represents the content of this instance.
func (cmd Cmd) String() string {
return fmt.Sprintf("Cmd { Name: %s, Args: %v, Opts: %v }", cmd.Name, cmd.Args, cmd.opts)
}