Documentation
¶
Overview ¶
* Copyright (C) 2017 eschao <esc.chao@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
* Copyright (C) 2017 eschao <esc.chao@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
* Copyright (C) 2017 eschao <esc.chao@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
* Copyright (C) 2017 eschao <esc.chao@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
Index ¶
- Constants
- Variables
- func ParseCli(out interface{}, name string, args []string) ([]string, error)
- func ParseConfig(out interface{}, configFlag string) error
- func ParseConfigFile(out interface{}, configFile string) error
- func ParseDefault(i interface{}) error
- func ParseEnv(out interface{}) error
- func ParseEnvWith(out interface{}, prefix string, parser ParseValueEnvFunc) error
- func UsageEnv(out io.Writer, in interface{}) error
- type Command
- type ParseValueEnvFunc
- type UsageFunc
Constants ¶
const ( Float32Size = 32 Float64Size = 64 Int8Size = 8 Int16Size = 16 Int32Size = 32 Int64Size = 64 Uint8Size = 8 Uint16Size = 16 Uint32Size = 32 UintSize = 32 Uint64Size = 64 )
const ( DefaultJSONConfig = "config.json" DefaultYamlConfig = "config.yaml" DefaultPropConfig = "config.properties" )
Default configuration file
const ( JSONConfigType = "json" YamlConfigType = "yaml" PropConfigType = "properties" )
Variables ¶
var ErrInternalError = errors.New("internal error")
Functions ¶
func ParseConfig ¶
ParseConfig parses given structure interface and set it with default configuration file. The configFlag is a command line flag to tell where to locate configure file. If the config file doesn't exist, the default config fill will be searched under the same folder with the fixed order: config.json, config.yaml and config.properties
func ParseConfigFile ¶
ParseConfigFile parses given structure interface and set its value with the specified configuration file
func ParseDefault ¶
func ParseDefault(i interface{}) error
ParseDefault parses the given structure, extract default value from its tag and set structure with these values. Normally, ParseDefault should be called before any other parsing functions to set default values for structure.
func ParseEnv ¶
func ParseEnv(out interface{}) error
Parse parses given structure interface, extracts environment definitions from its tag and sets structure with defined environment variables
func ParseEnvWith ¶
func ParseEnvWith(out interface{}, prefix string, parser ParseValueEnvFunc) error
type Server struct { Server string `env:"SERVER"` DB Database `env:"DB_"` }
The Server.DB.Host will be mapped to environment variable: DB_HOST which is concatenated from DB tag in Server struct and Host tag in Database struct
Types ¶
type Command ¶
type Command struct { Name string // Command name FlagSet *flag.FlagSet // Command arguments Usage string // Command usage description SubCommands map[string]*Command // Sub-commands Args []string // Rest of args after parsing // contains filtered or unexported fields }
Command defines a command line structure
func NewCLI ¶
NewCLI creates a command with given name, the command will use default ErrorHandling: flag.ExitOnError and default usage function: flag.Usage
func NewCliWith ¶ added in v0.3.0
func NewCliWith( name string, errHandling flag.ErrorHandling, usageHandling UsageFunc, ) *Command
NewCliWith creates a command with given name, error handling and customized usage function
func (*Command) Init ¶
Init analyzes the given structure interface, extracts cli definitions from its tag and installs command flagset by flag APIs. The interface must be a structure pointer, otherwise will return an error
type ParseValueEnvFunc ¶ added in v0.3.0
ParseValueEnvFunc func to parse env value
Directories
¶
Path | Synopsis |
---|---|
* Copyright (C) 2017 eschao <esc.chao@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
|
* Copyright (C) 2017 eschao <esc.chao@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. |