Skip to content

Commit

Permalink
Merge pull request #6 from athos/feature/main-args
Browse files Browse the repository at this point in the history
Pass command-line args to -main
  • Loading branch information
athos authored Aug 24, 2021
2 parents 369f1d1 + 7a716bd commit 53bc550
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Trenchman does not have `readline` support at this time. If you want to use feat
## Usage

```
usage: trench [<flags>]
usage: trench [<flags>] [<args>...]
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
Expand All @@ -75,6 +75,9 @@ Flags:
--init-ns=NAMESPACE Initialize REPL with the specified namespace. Defaults to "user".
-C, --color=auto When to use colors. Possible values: always, auto, none. Defaults to auto.
--version Show application version.
Args:
[<args>] Arguments to pass to -main. These will be ignored unless -m is specified.
```

### Connecting to a server
Expand Down
13 changes: 12 additions & 1 deletion cmd/trench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type cmdArgs struct {
mainNS *string
initNS *string
colorOption *string
args *[]string
}

type errorHandler struct {
Expand Down Expand Up @@ -60,6 +61,7 @@ var args = cmdArgs{
mainNS: kingpin.Flag("main", "Call the -main function for a namespace.").Short('m').PlaceHolder("NAMESPACE").String(),
initNS: kingpin.Flag("init-ns", "Initialize REPL with the specified namespace. Defaults to \"user\".").PlaceHolder("NAMESPACE").String(),
colorOption: kingpin.Flag("color", "When to use colors. Possible values: always, auto, none. Defaults to auto.").Default(COLOR_AUTO).Short('C').Enum(COLOR_NONE, COLOR_AUTO, COLOR_ALWAYS),
args: kingpin.Arg("args", "Arguments to pass to -main. These will be ignored unless -m is specified.").Strings(),
}

func colorized(colorOption string) bool {
Expand All @@ -77,6 +79,15 @@ func colorized(colorOption string) bool {
return false
}

func buildMainInvocation(mainNS string, args []string) string {
quotedArgs := []string{}
for _, arg := range args {
quotedArgs = append(quotedArgs, fmt.Sprintf("%q", arg))
}
argStr := strings.Join(quotedArgs, " ")
return fmt.Sprintf("(do (require '%s) (%s/-main %s) nil)", mainNS, mainNS, argStr)
}

func main() {
kingpin.Version("Trenchman " + version)
kingpin.Parse()
Expand Down Expand Up @@ -105,7 +116,7 @@ func main() {
return
}
if mainNS != "" {
repl.Eval(fmt.Sprintf("(do (require '%s) (%s/-main))", mainNS, mainNS))
repl.Eval(buildMainInvocation(mainNS, *args.args))
return
}
if code != "" {
Expand Down

0 comments on commit 53bc550

Please sign in to comment.