Skip to content

Commit 7674771

Browse files
committed
feat: cli args list
1 parent 6cb0a5a commit 7674771

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

args/args.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,14 @@ import (
1313
// Get fetches the remaining arguments after CLI parsing and splits them into
1414
// two groups: the arguments before the double dash (--) and the arguments after
1515
// the double dash.
16-
func Get() ([]string, string, error) {
16+
func Get() ([]string, []string, error) {
1717
args := pflag.Args()
1818
doubleDashPos := pflag.CommandLine.ArgsLenAtDash()
1919

2020
if doubleDashPos == -1 {
21-
return args, "", nil
21+
return args, nil, nil
2222
}
23-
24-
var quotedCliArgs []string
25-
for _, arg := range args[doubleDashPos:] {
26-
quotedCliArg, err := syntax.Quote(arg, syntax.LangBash)
27-
if err != nil {
28-
return nil, "", err
29-
}
30-
quotedCliArgs = append(quotedCliArgs, quotedCliArg)
31-
}
32-
33-
return args[:doubleDashPos], strings.Join(quotedCliArgs, " "), nil
23+
return args[:doubleDashPos], args[doubleDashPos:], nil
3424
}
3525

3626
// Parse parses command line argument: tasks and global variables
@@ -51,6 +41,18 @@ func Parse(args ...string) ([]*task.Call, *ast.Vars) {
5141
return calls, globals
5242
}
5343

44+
func ToQuotedString(args []string) (string, error) {
45+
var quotedCliArgs []string
46+
for _, arg := range args {
47+
quotedCliArg, err := syntax.Quote(arg, syntax.LangBash)
48+
if err != nil {
49+
return "", err
50+
}
51+
quotedCliArgs = append(quotedCliArgs, quotedCliArg)
52+
}
53+
return strings.Join(quotedCliArgs, " "), nil
54+
}
55+
5456
func splitVar(s string) (string, string) {
5557
pair := strings.SplitN(s, "=", 2)
5658
return pair[0], pair[1]

cmd/task/task.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,23 @@ func run() error {
144144
}
145145

146146
// Parse the remaining arguments
147-
argv, cliArgs, err := args.Get()
147+
cliArgsPreDash, cliArgsPostDash, err := args.Get()
148148
if err != nil {
149149
return err
150150
}
151-
calls, globals := args.Parse(argv...)
151+
calls, globals := args.Parse(cliArgsPreDash...)
152152

153153
// If there are no calls, run the default task instead
154154
if len(calls) == 0 {
155155
calls = append(calls, &task.Call{Task: "default"})
156156
}
157157

158-
globals.Set("CLI_ARGS", ast.Var{Value: cliArgs})
158+
cliArgsPostDashQuoted, err := args.ToQuotedString(cliArgsPostDash)
159+
if err != nil {
160+
return err
161+
}
162+
globals.Set("CLI_ARGS", ast.Var{Value: cliArgsPostDashQuoted})
163+
globals.Set("CLI_ARGS_LIST", ast.Var{Value: cliArgsPostDash})
159164
globals.Set("CLI_FORCE", ast.Var{Value: flags.Force || flags.ForceAll})
160165
globals.Set("CLI_SILENT", ast.Var{Value: flags.Silent})
161166
globals.Set("CLI_VERBOSE", ast.Var{Value: flags.Verbose})

website/docs/reference/templating.mdx

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ special variable will be overridden.
102102

103103
| Var | Description |
104104
|--------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
105-
| `CLI_ARGS` | Contain all extra arguments passed after `--` when calling Task through the CLI. |
105+
| `CLI_ARGS` | Contain all extra arguments passed after `--` when calling Task through the CLI as a string. |
106+
| `CLI_ARGS_LIST` | Contain all extra arguments passed after `--` when calling Task through the CLI as a shell parsed list. |
106107
| `CLI_FORCE` | A boolean containing whether the `--force` or `--force-all` flags were set. |
107108
| `CLI_SILENT` | A boolean containing whether the `--silent` flag was set. |
108109
| `CLI_VERBOSE` | A boolean containing whether the `--verbose` flag was set. |

0 commit comments

Comments
 (0)