energy/common/flag.go

125 lines
2.2 KiB
Go
Raw Normal View History

2022-10-04 13:21:05 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
2022-10-04 13:21:05 +08:00
//
//----------------------------------------
package common
2022-10-04 13:21:05 +08:00
import (
"bytes"
"fmt"
2022-10-04 22:34:57 +08:00
"github.com/energye/energy/logger"
2022-10-04 13:21:05 +08:00
"os"
"strings"
)
var Args = argsParse()
type _args struct {
isMain bool
size int
value map[string]PRCESS_TYPE
commandLine string
}
func argsParse() *_args {
var commandBuf = bytes.Buffer{}
var args = &_args{value: make(map[string]PRCESS_TYPE, len(os.Args))}
args.size = len(os.Args)
for i, v := range os.Args {
a := strings.Split(v, "=")
if len(a) == 2 {
args.value[strings.Replace(a[0], "--", "", 1)] = PRCESS_TYPE(a[1])
}
if i > 0 {
commandBuf.WriteString("|,|")
}
commandBuf.WriteString(v)
}
args.commandLine = commandBuf.String()
if v, ok := args.value["type"]; ok {
args.isMain = v == PT_MAIN
} else {
args.isMain = true
}
return args
}
2022-10-04 22:34:57 +08:00
func (m *_args) CommandLine() string {
return m.commandLine
}
2022-10-04 13:21:05 +08:00
func (m *_args) Size() int {
return m.size
}
func (m *_args) ProcessType() PRCESS_TYPE {
if v, ok := m.value["type"]; ok {
return v
}
return PT_MAIN
}
func (m *_args) IsMain() bool {
return m.isMain
}
func (m *_args) IsRender() bool {
if v, ok := m.value["type"]; ok {
return v == PT_RENDERER
2022-10-04 13:21:05 +08:00
}
return false
}
func (m *_args) IsGPU() bool {
if v, ok := m.value["type"]; ok {
return v == PT_GPU
}
return false
}
func (m *_args) IsUtility() bool {
if v, ok := m.value["type"]; ok {
return v == PT_UTILITY
}
return false
}
func (m *_args) IsSubprocess() bool {
_, ok := m.value["type"]
return ok
}
2022-10-04 22:34:57 +08:00
func (m *_args) SetArgs(name, value string) {
m.value[name] = PRCESS_TYPE(value)
}
2022-10-04 13:21:05 +08:00
func (m *_args) Args(name string) string {
if v, ok := m.value[name]; ok {
return string(v)
}
return ""
}
func (m *_args) Print() {
2022-10-05 20:43:25 +08:00
logger.Debug("command line:", m.size)
2022-10-04 13:21:05 +08:00
for key, value := range m.value {
fmt.Printf("_args[%v = %v]\n", key, value)
}
}
type PRCESS_TYPE string
const (
PT_MAIN PRCESS_TYPE = ""
PT_GPU = "gpu-process"
PT_UTILITY = "utility"
PT_RENDERER = "renderer"
2022-10-04 13:21:05 +08:00
PT_DEVTOOLS = "devtools"
)