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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
package model
import (
"fmt"
"regexp"
"strings"
)
// OptionType describes the type of the option, possible values are 'Argument', 'Flag' or 'KeyValue'
type OptionType string
const (
// Argument ...
Argument OptionType = "Argument"
// Flag ...
Flag OptionType = "Flag"
// KeyValue ...
KeyValue OptionType = "KeyValue"
)
// Option describes an option of a autorest command line
type Option interface {
// Type returns the type of this option
Type() OptionType
// Format returns the actual option in string
Format() string
}
// ArgumentOption is an option not starting with '--' or '-'
type ArgumentOption interface {
Option
Argument() string
}
// FlagOption is an option that starts with '--' but do not have a value
type FlagOption interface {
Option
Flag() string
}
// KeyValueOption is an option that starts with '--' and have a value
type KeyValueOption interface {
Option
Key() string
Value() string
}
// NewOption returns an Option from a formatted string. We should hold this result using this function: input == result.Format()
func NewOption(input string) (Option, error) {
if strings.HasPrefix(input, "--") {
// this option is either a flag or a key value pair option
return parseFlagOrKeyValuePair(strings.TrimPrefix(input, "--"))
}
// this should be an argument
return argument{value: input}, nil
}
var flagRegex = regexp.MustCompile(`^[a-zA-Z]`)
func parseFlagOrKeyValuePair(input string) (Option, error) {
if !flagRegex.MatchString(input) {
return nil, fmt.Errorf("cannot parse flag '%s', a flag or option should only start with letters", input)
}
segments := strings.Split(input, "=")
if len(segments) <= 1 {
// this should be a flag
return flagOption{value: input}, nil
}
return keyValueOption{
key: segments[0],
value: strings.Join(segments[1:], "="),
}, nil
}
type argument struct {
value string
}
func (f argument) Type() OptionType {
return Argument
}
// Format ...
func (f argument) Format() string {
return f.value
}
func (f argument) Argument() string {
return f.value
}
func (f argument) String() string {
return f.Format()
}
var _ ArgumentOption = (*argument)(nil)
type flagOption struct {
value string
}
func (f flagOption) Type() OptionType {
return Flag
}
// Format ...
func (f flagOption) Format() string {
return fmt.Sprintf("--%s", f.value)
}
func (f flagOption) String() string {
return f.Format()
}
func (f flagOption) Flag() string {
return f.value
}
var _ FlagOption = (*flagOption)(nil)
type keyValueOption struct {
key string
value string
}
func (f keyValueOption) Type() OptionType {
return KeyValue
}
// Format ...
func (f keyValueOption) Format() string {
return fmt.Sprintf("--%s=%s", f.key, f.value)
}
func (f keyValueOption) String() string {
return f.Format()
}
func (f keyValueOption) Key() string {
return f.key
}
func (f keyValueOption) Value() string {
return f.value
}
var _ KeyValueOption = (*keyValueOption)(nil)
// NewArgument returns a new argument option (without "--")
func NewArgument(value string) Option {
return argument{
value: value,
}
}
// NewFlagOption returns a flag option (with "--", without value)
func NewFlagOption(flag string) Option {
return flagOption{
value: flag,
}
}
// NewKeyValueOption returns a key-value option like "--tag=something"
func NewKeyValueOption(key, value string) Option {
return keyValueOption{
key: key,
value: value,
}
}
|