universal.go

Overview

The universal.go file implements the universal package, which provides an abstraction layer over multiple launchers (specifically sublaunchers such as console and web launchers). This package allows the selection of one launcher based on command-line parameters and then delegates argument parsing and execution responsibilities to the chosen launcher. It acts as a router that dispatches command-line arguments to the appropriate sublauncher.

Types and Functions

uniLauncher (struct)

The core struct in this package, uniLauncher, maintains:

NewLauncher

func NewLauncher(sublaunchers ...launcher.SubLauncher) launcher.Launcher

(l *uniLauncher) Execute

func (l *uniLauncher) Execute(ctx context.Context, config *launcher.Config, args []string) error

(l *uniLauncher) ParseAndRun

func (l *uniLauncher) ParseAndRun(ctx context.Context, config *launcher.Config, args []string, parseRemaining func([]string) error) error

(l *uniLauncher) parse

func (l *uniLauncher) parse(args []string) ([]string, error)

(l *uniLauncher) run

func (l *uniLauncher) run(ctx context.Context, config *launcher.Config) error

(l *uniLauncher) CommandLineSyntax

func (l *uniLauncher) CommandLineSyntax() string

(l *uniLauncher) simpleDescription

func (l *uniLauncher) simpleDescription() string

ErrorOnUnparsedArgs

func ErrorOnUnparsedArgs(args []string) error

Implementation Details and Algorithms

Interaction with Other System Components

Usage Example

// Create sublaunchers (consoleLauncher, webLauncher are assumed to implement launcher.SubLauncher)
uni := universal.NewLauncher(consoleLauncher, webLauncher)

// Execute with arguments, context and config
err := uni.Execute(context.Background(), config, os.Args[1:])
if err != nil {
    fmt.Printf("Error: %v\n", err)
}

Class Diagram

classDiagram
class uniLauncher {
-chosenLauncher: SubLauncher
-sublaunchers: []SubLauncher
+Execute(ctx, config, args) error
+ParseAndRun(ctx, config, args, parseRemaining) error
+parse(args) ([]string, error)
+run(ctx, config) error
+CommandLineSyntax() string
-simpleDescription() string
}
uniLauncher ..> launcher.SubLauncher : uses
uniLauncher ..> launcher.Launcher : implements