Skip to content

Commit 322f512

Browse files
committed
feat(cli): add --author/--name flags and reorder args
1 parent 024a6c2 commit 322f512

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ scdl <soundcloud-url> [options]
2929
### Options
3030

3131
- `-o, --output`: Specify the output directory (defaults to current directory).
32+
- `-a, --author`: Override artist/author name.
33+
- `-n, --name`: Override track title.
3234

3335
## Examples
3436

@@ -44,6 +46,12 @@ Download to a specific directory:
4446
scdl https://soundcloud.com/cowboyclicker/stay --output ~/Music/
4547
```
4648

49+
Download with custom artist and track name:
50+
51+
```bash
52+
scdl https://soundcloud.com/cowboyclicker/stay --author "Custom Artist" --name "Custom Title"
53+
```
54+
4755
## Library Usage
4856

4957
### Installation
@@ -75,6 +83,10 @@ if err != nil {
7583
return err
7684
}
7785

86+
// Optionally override artist/title before downloading
87+
track.Artist = "Custom Artist"
88+
track.Title = "Custom Title"
89+
7890
// Download the track to the current directory
7991
path, err := client.Download(ctx, track, ".", nil) // progress callback is optional
8092
if err != nil {

cmd/scdl/main.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ func main() {
3535
Usage: "Output directory",
3636
Value: ".",
3737
},
38+
&cli.StringFlag{
39+
Name: "author",
40+
Aliases: []string{"a"},
41+
Usage: "Override artist/author name",
42+
},
43+
&cli.StringFlag{
44+
Name: "name",
45+
Aliases: []string{"n"},
46+
Usage: "Override track title",
47+
},
3848
},
3949
Action: func(c *cli.Context) error {
4050
if c.NArg() < 1 {
@@ -64,6 +74,13 @@ func main() {
6474
return fmt.Errorf("get track: %w", err)
6575
}
6676

77+
if author := c.String("author"); author != "" {
78+
track.Artist = author
79+
}
80+
if name := c.String("name"); name != "" {
81+
track.Title = name
82+
}
83+
6784
if _, err := client.Download(ctx, track, outputDir, nil); err != nil {
6885
return fmt.Errorf("download: %w", err)
6986
}
@@ -73,8 +90,34 @@ func main() {
7390
},
7491
}
7592

76-
if err := app.Run(os.Args); err != nil {
93+
if err := app.Run(reorderArgs(os.Args)); err != nil {
7794
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
7895
os.Exit(1)
7996
}
8097
}
98+
99+
// reorderArgs moves flags and their values before positional arguments so that
100+
// urfave/cli parses them correctly regardless of where the user places them.
101+
func reorderArgs(args []string) []string {
102+
if len(args) < 2 {
103+
return args
104+
}
105+
106+
flags := []string{args[0]}
107+
var positional []string
108+
109+
for i := 1; i < len(args); i++ {
110+
if strings.HasPrefix(args[i], "-") {
111+
flags = append(flags, args[i])
112+
// If the flag doesn't use --flag=value form and has a next arg, treat it as the flag's value.
113+
if !strings.Contains(args[i], "=") && i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") {
114+
i++
115+
flags = append(flags, args[i])
116+
}
117+
} else {
118+
positional = append(positional, args[i])
119+
}
120+
}
121+
122+
return append(flags, positional...)
123+
}

0 commit comments

Comments
 (0)