Skip to content

Commit 23b1eda

Browse files
committed
Rough pencil in of how app should work
1 parent 2e2b6f4 commit 23b1eda

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

cmd.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ import (
1818
"github.com/xo/ox/text"
1919
)
2020

21+
// ParseCommand parses a command's doc comment with reflect, building
22+
func ParseCommand(v any) ([]Option, error) {
23+
var opts []Option
24+
// build sub commands
25+
if c, ok := v.(interface{ Sub() []any }); ok {
26+
for _, s := range c.Sub() {
27+
o, err := ParseCommand(s)
28+
if err != nil {
29+
return nil, err
30+
}
31+
opts = append(opts, Sub(o...))
32+
}
33+
}
34+
return opts, nil
35+
}
36+
2137
// Command is a command.
2238
type Command struct {
2339
// Parent is the command's parent.

ox.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,11 @@ func Run(opts ...Option) {
301301

302302
// RunAppContext
303303
func RunAppContext[T *E, E any](ctx context.Context, v T, opts ...Option) {
304-
/*
305-
o, err := NewApp(v)
306-
if err != nil && c.Handler(err) {
307-
return
308-
}
309-
RunContext(ctx, prepend(opts, o...)...)
310-
*/
304+
c, o, err := NewApp(v)
305+
if err != nil && c.Handler(err) {
306+
return
307+
}
308+
RunContext(ctx, prepend(opts, o...)...)
311309
}
312310

313311
// RunApp
@@ -517,6 +515,20 @@ func (ctx *Context) Expand(v any) (any, error) {
517515
return v, nil
518516
}
519517

518+
// NewApp creates a execution context and a app command tree by parsing the doc
519+
// comment of v.
520+
func NewApp[T *E, E any](v T) (*Context, []Option, error) {
521+
c, err := NewContext()
522+
if err != nil {
523+
return nil, nil, err
524+
}
525+
opts, err := ParseCommand(v)
526+
if err != nil {
527+
return c, nil, err
528+
}
529+
return c, opts, nil
530+
}
531+
520532
// contextKey is the context key type.
521533
type contextKey int
522534

0 commit comments

Comments
 (0)