Files
obitools4/pkg/obioptions/options.go
Eric Coissac d88de15cdc Refactoring codes for removing buffer size options. An some other changes...
Former-commit-id: 10b57cc1a27446ade3c444217341e9651e89cdce
2023-03-07 11:12:13 +07:00

108 lines
2.5 KiB
Go

package obioptions
import (
"fmt"
"os"
"runtime"
log "github.com/sirupsen/logrus"
"github.com/DavidGamba/go-getoptions"
)
var _Debug = false
var _ParallelWorkers = runtime.NumCPU()*2 - 1
var _MaxAllowedCPU = runtime.NumCPU()
var _BatchSize = 5000
type ArgumentParser func([]string) (*getoptions.GetOpt, []string)
func GenerateOptionParser(optionset ...func(*getoptions.GetOpt)) ArgumentParser {
options := getoptions.New()
options.SetMode(getoptions.Bundling)
options.SetUnknownMode(getoptions.Fail)
options.Bool("help", false, options.Alias("h", "?"))
options.BoolVar(&_Debug, "debug", false)
options.IntVar(&_ParallelWorkers, "workers", _ParallelWorkers,
options.Alias("w"),
options.Description("Number of parallele threads computing the result"))
options.IntVar(&_MaxAllowedCPU, "max-cpu", _MaxAllowedCPU,
options.GetEnv("OBIMAXCPU"),
options.Description("Number of parallele threads computing the result"))
for _, o := range optionset {
o(options)
}
return func(args []string) (*getoptions.GetOpt, []string) {
remaining, err := options.Parse(args[1:])
if err != nil {
log.Fatalf("Error on the commande line : %v",err)
}
// Setup the maximum number of CPU usable by the program
runtime.GOMAXPROCS(_MaxAllowedCPU)
if options.Called("max-cpu") {
log.Printf("CPU number limited to %d", _MaxAllowedCPU)
if !options.Called("workers") {
_ParallelWorkers = _MaxAllowedCPU*2 - 1
log.Printf("Number of workers set %d", _ParallelWorkers)
}
}
if options.Called("no-singleton") {
log.Printf("No singleton option set")
}
if options.Called("help") {
fmt.Fprint(os.Stderr, options.Help())
os.Exit(1)
}
log.SetLevel(log.InfoLevel)
if options.Called("debug") {
log.SetLevel(log.DebugLevel)
log.Debugln("Switch to debug level logging")
}
return options, remaining
}
}
// Predicate indicating if the debug mode is activated.
func CLIIsDebugMode() bool {
return _Debug
}
// CLIParallelWorkers returns the number of parallel workers requested by
// the command line option --workers|-w.
func CLIParallelWorkers() int {
return _ParallelWorkers
}
// CLIParallelWorkers returns the number of parallel workers requested by
// the command line option --workers|-w.
func CLIMaxCPU() int {
return _MaxAllowedCPU
}
// CLIBatchSize returns the expeted size of the sequence batches
func CLIBatchSize() int {
return _BatchSize
}
// DebugOn sets the debug mode on.
func DebugOn() {
_Debug = true
}
// DebugOff sets the debug mode off.
func DebugOff() {
_Debug = false
}